xcontroller

module
v0.0.0-...-689e244 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2022 License: MIT

README

xcontroller

A sample Kubernetes Controller using protobuf to define CRD.

This repository is base on the concept of kubernetes/sample-controller but more details. Additionally, this repository also use protobuf to define CRD model.

Basically, go-client using json to marshal and unmarshal CRD after retrieve them from API, and you are unable to unmarshal a proto message by encoding/json. To do that, you need a marshal/unmarshal redirect to jsonpb.

package v1alpha1

import (
    bytes "bytes"
    jsonpb "github.com/golang/protobuf/jsonpb"
)

type Bar struct { } // Your CRD

func (this *Bar) MarshalJSON() ([]byte, error) {
	str, err := BarMarshaler.MarshalToString(this)
	return []byte(str), err
}

func (this *Bar) UnmarshalJSON(b []byte) error {
	return BarUnmarshaler.Unmarshal(bytes.NewReader(b), this)
}

var (
	BarMarshaler   = &jsonpb.Marshaler{}
	BarUnmarshaler = &jsonpb.Unmarshaler{AllowUnknownFields: true}
)

The second thing you need is the deepcopy.

package v1alpha1

import (
	proto "google.golang.org/protobuf/proto"
)

func (in *Bar) DeepCopyInto(out *Bar) {
	p := proto.Clone(in).(*Bar)
	*out = *p
}

func (in *Bar) DeepCopy() *Bar {
	if in == nil {
		return nil
	}
	out := new(Bar)
	in.DeepCopyInto(out)
	return out
}

func (in *Bar) DeepCopyInterface() interface{} {
	return in.DeepCopy()
}

Fortunately, istio/tools support us several tools to automatically generate these codes, this will save a lot of times for us when we're working with Kubernetes CRDs. Big thanks for istio contributors.

Usage

To generate code from protobuf

Make sure that you already install buf.

# install istio tools
$ go install istio.io/tools/cmd/protoc-gen-golang-deepcopy

$ go install istio.io/tools/cmd/protoc-gen-golang-jsonshim

# lint check before generate code
$ buf lint --path api

$ buf generate --path api
To update codegen
$ export CODEGEN_PKG=~/go/pkg/mod/k8s.io/code-generator@v0.21.0
$ ./hack/update-codegen.sh 
To start xcontroller
$ xcontroller --help
Usage:
  xcontroller [flags]

Flags:
  -h, --help                help for xcontroller
      --kubeconfig string   Full path to kubernetes client configuration, i.e. ~/.kube/config
      --log-level string    Log level (default "info")
      --workers int         Number of workers (default 10)

References

Directories

Path Synopsis
api
foo/v1alpha1
Code generated by protoc-gen-deepcopy.
Code generated by protoc-gen-deepcopy.
pkg
apis/foo/v1alpha1
Package v1alpha1 is the v1alpha1 version of the API.
Package v1alpha1 is the v1alpha1 version of the API.
client/clientset/versioned
This package has the automatically generated clientset.
This package has the automatically generated clientset.
client/clientset/versioned/fake
This package has the automatically generated fake clientset.
This package has the automatically generated fake clientset.
client/clientset/versioned/scheme
This package contains the scheme of the automatically generated clientset.
This package contains the scheme of the automatically generated clientset.
client/clientset/versioned/typed/foo/v1alpha1
This package has the automatically generated typed clients.
This package has the automatically generated typed clients.
client/clientset/versioned/typed/foo/v1alpha1/fake
Package fake has the automatically generated clients.
Package fake has the automatically generated clients.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL