Go client code generator from Kubernetes resource specs yaml
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
docker run --rm -i ghcr.io/prasadg193/kyaml2go:latest create < testdata/secrets.yaml
$ git clone https://github.com/PrasadG193/kyaml2go.git
$ make
kyaml2go is available at https://kyaml2go.prasadg.dev
Alternatively, you can also use CLI or docker image to generate code
$ 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
$ 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!")
}
$ 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
}
End-to-end workflow and algorithm to find imports can be found here
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
The Go Gopher is originally by Renee French
This artwork is borrowed from an awesome artwork collection by Egon Elbre