Giter Club home page Giter Club logo

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

Installation

Install From Source

Step 1: Clone the repo

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

Step 2: Build binary using make

$ make

Step 3: Convert K8s specs into Go client code

$ cat testdata/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
  labels:
    app: mysql
spec:
  ports:
    - name: server
      port: 8080
      targetPort: 5000
      protocol: TCP
  type: ClusterIP
  selector:
    app: mysql
$ kyaml2go create -f testdata/service.yaml
// Auto-generated by kyaml2go - https://github.com/PrasadG193/kyaml2go
package main

import (
	"fmt"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/util/intstr"
	"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)
	}
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		panic(err)
	}
	kubeclient := clientset.CoreV1().Services("default")

	// Create resource object
	object := &corev1.Service{
		TypeMeta: metav1.TypeMeta{
			Kind:       "Service",
			APIVersion: "v1",
		},
		ObjectMeta: metav1.ObjectMeta{
			Name: "mysql-service",
			Labels: map[string]string{
				"app": "mysql",
			},
		},
		Spec: corev1.ServiceSpec{
			Ports: []corev1.ServicePort{
				corev1.ServicePort{
					Name:     "server",
					Protocol: corev1.Protocol("TCP"),
					Port:     8080,
					TargetPort: intstr.IntOrString{
						Type:   intstr.Type(0),
						IntVal: 5000,
					},
					NodePort: 0,
				},
			},
			Selector: map[string]string{
				"app": "mysql",
			},
			Type:                corev1.ServiceType("ClusterIP"),
			HealthCheckNodePort: 0,
		},
	}

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

Usage

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

Alternatively, you can also use CLI to generate code

CLI

$ 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/get/update/delete -f /path/to/resource_specs.yaml

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

Watchers

James Cloos 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.