{"id":21730533,"url":"https://github.com/andy2046/k8s-custom-resource-watch","last_synced_at":"2026-05-19T19:02:44.237Z","repository":{"id":57548089,"uuid":"184559161","full_name":"andy2046/k8s-custom-resource-watch","owner":"andy2046","description":"watch for custom resource","archived":false,"fork":false,"pushed_at":"2019-05-12T12:49:26.000Z","size":4986,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-20T23:27:07.976Z","etag":null,"topics":["controller","crd","custom-resource-definition","kubernetes"],"latest_commit_sha":null,"homepage":"https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/#delete-a-customresourcedefinition","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andy2046.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}},"created_at":"2019-05-02T10:07:22.000Z","updated_at":"2020-11-02T14:06:00.000Z","dependencies_parsed_at":"2022-09-26T16:31:11.273Z","dependency_job_id":null,"html_url":"https://github.com/andy2046/k8s-custom-resource-watch","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/andy2046/k8s-custom-resource-watch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andy2046%2Fk8s-custom-resource-watch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andy2046%2Fk8s-custom-resource-watch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andy2046%2Fk8s-custom-resource-watch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andy2046%2Fk8s-custom-resource-watch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andy2046","download_url":"https://codeload.github.com/andy2046/k8s-custom-resource-watch/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andy2046%2Fk8s-custom-resource-watch/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267278874,"owners_count":24063272,"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","status":"online","status_checked_at":"2025-07-26T02:00:08.937Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["controller","crd","custom-resource-definition","kubernetes"],"created_at":"2024-11-26T04:16:14.588Z","updated_at":"2026-05-19T19:02:44.144Z","avatar_url":"https://github.com/andy2046.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# k8s-custom-resource-watch\n\n\u003e a custom K8s controller example to watch for the creation, update, deletion of user defined custom resources.\n\n## Workflow\n\n### 1 Define custom resource\n\n#### 1.1 define API group name / Version / Resource name\n* The API group name `nokube.xyz`\n* The Version `v1`\n* Resource name `customresource`\n\n#### 1.2 create the directory structure\n```sh\nmkdir -p pkg/apis/customresource/v1\n```\n\n#### 1.3 add API group name const in register file\n```sh\ntouch pkg/apis/customresource/register.go\n```\n\u003e package has the same name as the custom resource\n```go\npackage customresource\n\n// GroupName for customresource\nconst GroupName = \"nokube.xyz\"\n```\n\n#### 1.4 create the resource structs\n```sh\ntouch pkg/apis/customresource/v1/types.go\n```\n\u003e `// +\u003ctag_name\u003e[=value]` are indicators for code generator\n\n\u003e `+genclient` — generate a client for the package\n\n\u003e `+genclient:noStatus` — when generating the client, there is no status stored for the package\n\n\u003e `+k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object` — generate deepcopy logic (required) implementing the runtime.Object interface (for both `CustomResource` and `CustomResourceList`)\n\n#### 1.5 create a doc source file for the package\n```sh\ntouch pkg/apis/customresource/v1/doc.go\n```\n```go\n// +k8s:deepcopy-gen=package\n// +groupName=nokube.xyz\n\npackage v1\n```\n\u003e `// +groupName=nokube.xyz` inform the generator what the API group name is\n\n\u003e `// +k8s:deepcopy-gen=package` deepcopy should be generated for all types in the package\n\n#### 1.6 add functions to handle adding types to the schemes\n```sh\ntouch pkg/apis/customresource/v1/register.go\n```\n\n### 2 Run the code generator\nrun `code-gen.sh` shell script to do all the heavy lifting via `k8s.io/code-generator` package\n\n### 3 Wire up the generated code\n\n#### 3.1 to return custom resource clientset instance to interact with `CustomResource` in `main.go`\n```go\n// retrieve the Kubernetes cluster client from outside of the cluster.\nfunc getKubeClient() (kubernetes.Interface, resourceclientset.Interface) {\n\tvar kubeConfigPath string\n\tif !inCluster {\n\t\t// resolve path to `$HOME/.kube/config`\n\t\tkubeConfigPath = path.Join(userHomeDir(), \"/.kube/config\")\n\t}\n\n\tconfig, err := clientcmd.BuildConfigFromFlags(\"\", kubeConfigPath)\n\tif err != nil {\n\t\tlogger.Fatalf(\"BuildConfigFromFlags: %v\", err)\n\t}\n\n\tclient, err := kubernetes.NewForConfig(config)\n\tif err != nil {\n\t\tlogger.Fatalf(\"client NewForConfig: %v\", err)\n\t}\n\n\tresourceClient, err := resourceclientset.NewForConfig(config)\n\tif err != nil {\n\t\tlogger.Fatalf(\"resourceClient NewForConfig: %v\", err)\n\t}\n\n\tlogger.Println(\"Successfully get k8s client\")\n\treturn client, resourceClient\n}\n```\n\n#### 3.2 to return custom resource informer instance in `controller.go`\n```go\n// New creates a Controller instance.\nfunc New(client kubernetes.Interface, resourceClient resourceclientset.Interface) *Controller {\n\tinformer := resourceinformerV1.NewCustomResourceInformer(\n\t\tresourceClient,\n\t\tnameSpace,\n\t\t0,\n\t\tcache.Indexers{},\n    )\n    \n    // ...\n}\n```\n\n### 4 Add Custom Resource Definition\n```sh\nmkdir crd\n# touch crd/customresource.yml\nkubectl apply -f crd/customresource.yml\nkubectl get customresourcedefinition\n```\n\n### 5 Run the controller\n```sh\ngo run main.go\n# touch customresource-deploy.yml\nkubectl apply -f customresource-deploy.yml\nkubectl get CustomResource\n```\n\n### 6 Clean up\n```sh\nkubectl delete CustomResource example-customresource\nkubectl delete -f crd/customresource.yml\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandy2046%2Fk8s-custom-resource-watch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandy2046%2Fk8s-custom-resource-watch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandy2046%2Fk8s-custom-resource-watch/lists"}