https://github.com/x00real/xcontroller
A sample Kubernetes Controller using protobuf to define CRD model
https://github.com/x00real/xcontroller
k8s-controller kubernetes proto-crd protobuf3
Last synced: about 1 year ago
JSON representation
A sample Kubernetes Controller using protobuf to define CRD model
- Host: GitHub
- URL: https://github.com/x00real/xcontroller
- Owner: x00real
- License: mit
- Created: 2022-12-02T09:10:16.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-12-02T15:25:04.000Z (over 3 years ago)
- Last Synced: 2025-06-04T06:34:48.474Z (about 1 year ago)
- Topics: k8s-controller, kubernetes, proto-crd, protobuf3
- Language: Go
- Homepage:
- Size: 62.5 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# xcontroller
A sample Kubernetes Controller using protobuf to define CRD.
This repository is base on the concept of [kubernetes/sample-controller](https://github.com/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`.
```go
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.
```go
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`.
```console
# 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`
```console
$ 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
* https://github.com/kubernetes/sample-controller
* https://github.com/istio/tools