GithubHelp home page GithubHelp logo

hadaf-cloud / kyaml2go Goto Github PK

View Code? Open in Web Editor NEW

This project forked from prasadg193/kyaml2go

0.0 0.0 0.0 406 KB

K8s Go client code generator from Kubernetes resource yamls

Home Page: https://kyaml2go.prasadg.dev

License: Apache License 2.0

Shell 8.88% JavaScript 19.05% Go 47.34% CSS 12.19% Makefile 1.45% HTML 10.64% Dockerfile 0.46%

kyaml2go's Introduction

kyaml2go (Pronounced as camel2go ๐Ÿซ)

Build Status

https://kyaml2go.prasadg.dev

Go client code generator from Kubernetes resource specs yaml

https://github.com/PrasadG193/kyaml2go

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

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

Step 2: Build binary using make

$ make

Usage

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

Examples

Generating code for native resources

$ 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

$ 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

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

This artwork is borrowed from an awesome artwork collection by Egon Elbre

kyaml2go's People

Contributors

prasadg193 avatar viveksinghggits avatar bajran avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.