Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/PrasadG193/kyaml2go

K8s Go client code generator from Kubernetes resource yamls
https://github.com/PrasadG193/kyaml2go

client-go client-go-sample code-generator codegen examples go go-client k8s kubernetes kubernetes-resource-yamls

Last synced: 3 months ago
JSON representation

K8s Go client code generator from Kubernetes resource yamls

Awesome Lists containing this project

README

        

# kyaml2go (Pronounced as camel2go :camel:)

[![Build Status](https://travis-ci.org/PrasadG193/kyaml2go.svg?branch=master)](https://travis-ci.org/PrasadG193/kyaml2go)

https://kyaml2go.prasadg.dev

Go client code generator from Kubernetes resource specs yaml

![https://github.com/PrasadG193/kyaml2go](./k2go.png)

## Supported resources

List of supported Kinds and Groups can be found here: https://github.com/PrasadG193/kyaml2go/blob/master/pkg/kube/map.go#L38

kyaml2go also supports Custom Resources compatible with client-go v0.18.5 dep

## Installation

### Docker

```
docker run --rm -i ghcr.io/prasadg193/kyaml2go:latest create < testdata/secrets.yaml
```

### Install From Source

#### Step 1: Clone the repo

```bash
$ git clone https://github.com/PrasadG193/kyaml2go.git
```

#### Step 2: Build binary using make

```bash
$ make
```

## Usage

kyaml2go is available at https://kyaml2go.prasadg.dev

Alternatively, you can also use CLI or docker image to generate code

```bash
$ kyaml2go --help
NAME:
kyaml2go - Generate go code to manage Kubernetes resources using go-client sdks

USAGE:
kyaml2go [global options] command [command options] [arguments...]

VERSION:
0.0.0

COMMANDS:
create Generate code for creating a resource
update Generate code for updating a resource
get Generate code to get a resource object
delete Generate code for deleting a resource
help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:
--help, -h show help
--version, -v print the version

$ kyaml2go create --help
NAME:
kyaml2go create - Generate code for creating a resource

USAGE:
kyaml2go create [command options] [arguments...]

OPTIONS:
--cr Resource is a Custom resource
--apis value Custom resource api def package (without version)
--client value, -c value Custom resource typed client package name
--scheme value, -s value Custom resource scheme package name
```

## Examples

### Generating code for native resources

```bash
$ docker run --rm -i ghcr.io/prasadg193/kyaml2go:latest create < testdata/secrets.yaml

// Auto-generated by kyaml2go - https://github.com/PrasadG193/kyaml2go
package main

import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"os"
"path/filepath"
)

func main() {
// Create client
var kubeconfig string
kubeconfig, ok := os.LookupEnv("KUBECONFIG")
if !ok {
kubeconfig = filepath.Join(homedir.HomeDir(), ".kube", "config")
}

config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
panic(err)
}
client, err := clientset.NewForConfig(config)
if err != nil {
panic(err)
}
kubeclient := client.CoreV1().Secrets("default")

// Create resource object
object := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-secret",
},
StringData: map[string]string{
"password": "1f2d1e2e67df",
"username": "admin",
},
Type: corev1.SecretType("Opaque"),
}

// Manage resource
_, err = kubeclient.Create(context.TODO(), object, metav1.CreateOptions{})
if err != nil {
panic(err)
}
fmt.Println("Secret Created successfully!")
}

```

### Generating code for custom resources

```bash
$ docker run --rm -i ghcr.io/prasadg193/kyaml2go:latest create --cr --scheme "k8s.io/sample-controller/pkg/generated/clientset/versioned/scheme" --apis "k8s.io/sample-controller/pkg/apis/samplecontroller" --client "k8s.io/sample-controller/pkg/generated/clientset/versioned" < testdata/crs/foo.yaml

// Auto-generated by kyaml2go - https://github.com/PrasadG193/kyaml2go
package main

import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
samplecontrollerv1alpha1 "k8s.io/sample-controller/pkg/apis/samplecontroller/v1alpha1"
clientset "k8s.io/sample-controller/pkg/generated/clientset/versioned"
"os"
"path/filepath"
)

func main() {
// Create client
var kubeconfig string
kubeconfig, ok := os.LookupEnv("KUBECONFIG")
if !ok {
kubeconfig = filepath.Join(homedir.HomeDir(), ".kube", "config")
}

config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
panic(err)
}
client, err := clientset.NewForConfig(config)
if err != nil {
panic(err)
}
kubeclient := client.SamplecontrollerV1alpha1().Foos("default")

// Create resource object
object := &samplecontrollerv1alpha1.Foo{
TypeMeta: metav1.TypeMeta{
Kind: "Foo",
APIVersion: "samplecontroller.k8s.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "example-foo",
},
Spec: samplecontrollerv1alpha1.FooSpec{
DeploymentName: "example-foo",
Replicas: ptrint32(1),
},
}

// Manage resource
_, err = kubeclient.Create(context.TODO(), object, metav1.CreateOptions{})
if err != nil {
panic(err)
}
fmt.Println("Foo Created successfully!")
}

func ptrint32(p int32) *int32 {
return &p
}
```

## Workflow/Algorithm:
End-to-end workflow and algorithm to find imports can be found [here](https://docs.google.com/presentation/d/1_Es0d-QAkMMVdh8NiFCUMbKDQO76np-gCdTWlXLxAIY)

## Contributing

We love your input! We want to make contributing to this project as easy and transparent as possible, whether it's:
- Reporting a bug
- Discussing the current state of the code
- Submitting a fix
- Proposing new features

## Credits
The Go Gopher is originally by [Renee French](http://reneefrench.blogspot.com/)

This artwork is borrowed from an awesome artwork collection by [Egon Elbre](https://github.com/egonelbre/gophers)