{"id":21349016,"url":"https://github.com/vietanhduong/xcontroller","last_synced_at":"2025-03-16T04:23:18.946Z","repository":{"id":166714614,"uuid":"573354564","full_name":"vietanhduong/xcontroller","owner":"vietanhduong","description":"A sample Kubernetes Controller using protobuf to define CRD model","archived":false,"fork":false,"pushed_at":"2022-12-02T15:25:04.000Z","size":64,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-01-22T16:49:52.228Z","etag":null,"topics":["k8s-controller","kubernetes","proto-crd","protobuf3"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vietanhduong.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-12-02T09:10:16.000Z","updated_at":"2024-09-26T23:38:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"c1f53aa0-7abf-4eac-828d-aec2c5562bc1","html_url":"https://github.com/vietanhduong/xcontroller","commit_stats":null,"previous_names":["vietanhduong/xcontroller"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vietanhduong%2Fxcontroller","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vietanhduong%2Fxcontroller/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vietanhduong%2Fxcontroller/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vietanhduong%2Fxcontroller/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vietanhduong","download_url":"https://codeload.github.com/vietanhduong/xcontroller/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243823982,"owners_count":20353771,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["k8s-controller","kubernetes","proto-crd","protobuf3"],"created_at":"2024-11-22T02:41:03.432Z","updated_at":"2025-03-16T04:23:18.926Z","avatar_url":"https://github.com/vietanhduong.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# xcontroller\nA sample Kubernetes Controller using protobuf to define CRD. \n\nThis repository is base on the concept of [kubernetes/sample-controller](https://github.com/kubernetes/sample-controller) but\nmore details. Additionally, this repository also use protobuf to define CRD model.\n\nBasically, `go-client` using `json` to marshal and unmarshal CRD after retrieve them from API, \nand you are unable to unmarshal a proto message by `encoding/json`. To do that, you need a marshal/unmarshal redirect \nto `jsonpb`.\n\n```go\npackage v1alpha1\n\nimport (\n    bytes \"bytes\"\n    jsonpb \"github.com/golang/protobuf/jsonpb\"\n)\n\ntype Bar struct { } // Your CRD\n\nfunc (this *Bar) MarshalJSON() ([]byte, error) {\n\tstr, err := BarMarshaler.MarshalToString(this)\n\treturn []byte(str), err\n}\n\nfunc (this *Bar) UnmarshalJSON(b []byte) error {\n\treturn BarUnmarshaler.Unmarshal(bytes.NewReader(b), this)\n}\n\nvar (\n\tBarMarshaler   = \u0026jsonpb.Marshaler{}\n\tBarUnmarshaler = \u0026jsonpb.Unmarshaler{AllowUnknownFields: true}\n)\n```\n\nThe second thing you need is the deepcopy.\n\n```go\npackage v1alpha1\n\nimport (\n\tproto \"google.golang.org/protobuf/proto\"\n)\n\nfunc (in *Bar) DeepCopyInto(out *Bar) {\n\tp := proto.Clone(in).(*Bar)\n\t*out = *p\n}\n\nfunc (in *Bar) DeepCopy() *Bar {\n\tif in == nil {\n\t\treturn nil\n\t}\n\tout := new(Bar)\n\tin.DeepCopyInto(out)\n\treturn out\n}\n\nfunc (in *Bar) DeepCopyInterface() interface{} {\n\treturn in.DeepCopy()\n}\n```\n\nFortunately, `istio/tools` support us several tools to automatically generate these codes, this will save a lot of times\nfor us when we're working with Kubernetes CRDs. Big thanks for `istio` contributors.\n\n## Usage\n\n### To generate code from protobuf\nMake sure that you already install `buf`.\n\n```console\n# install istio tools\n$ go install istio.io/tools/cmd/protoc-gen-golang-deepcopy\n\n$ go install istio.io/tools/cmd/protoc-gen-golang-jsonshim\n\n# lint check before generate code\n$ buf lint --path api\n\n$ buf generate --path api\n```\n\n### To update codegen\n```\n$ export CODEGEN_PKG=~/go/pkg/mod/k8s.io/code-generator@v0.21.0\n$ ./hack/update-codegen.sh \n```\n\n### To start `xcontroller`\n```console\n$ xcontroller --help\nUsage:\n  xcontroller [flags]\n\nFlags:\n  -h, --help                help for xcontroller\n      --kubeconfig string   Full path to kubernetes client configuration, i.e. ~/.kube/config\n      --log-level string    Log level (default \"info\")\n      --workers int         Number of workers (default 10)\n```\n\n## References\n* https://github.com/kubernetes/sample-controller\n* https://github.com/istio/tools\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvietanhduong%2Fxcontroller","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvietanhduong%2Fxcontroller","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvietanhduong%2Fxcontroller/lists"}