-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Steps to generate CRD/Operator code #14
Comments
I had followed this guide. It really WORKS! Thanks! |
Close this issue after moving the steps in docs. |
Steps to create custom Kubernetes controller based on the https://github.com/kubernetes/sample-controller (July 27, 2022) Uses go modules. 1] export GO111MODULE=on
7] Create your type definitions in your-controller/pkg (See sample-controller/pkg for reference. You have to create the apis folder sub-tree manually. Create empty 'generated' sub-folder. Copy signals from sample-controller/pkg/signals). |
I can not believe it! |
Here are the steps that one needs to follow towards generating your own CRD/Operator code.
Generating Client/Listers/Informers:
For generating the client/listers/informers, etc. the first step is to create specific directory structure and define specific files in it. Here is that structure and the required files:
Following are detailed steps:
Choose a name for your CRD's group name -- say 'postgrescontroller'
Create pkg/apis/'CRD group name' directory
cd pkg/apis/'CRD group name'
Inside this directory create register.go to register the group name. When registering the name, choose some qualifier (example, in
https://github.com/cloud-ark/kubeplus/blob/master/postgres-crd-v2/pkg/apis/postgrescontroller/register.go we have used "kubeplus" as the qualifier name.).
Create v1 directory inside pkg/apis/'CRD group name'
cd pkg/apis/'CRD group name'/v1
Create doc.go. Make sure you set the "+groupName=." in line 20 of doc.go. Leave line 21 as it is. See https://github.com/cloud-ark/kubeplus/blob/master/postgres-crd-v2/pkg/apis/postgrescontroller/v1/doc.go
Copy https://github.com/cloud-ark/kubeplus/tree/master/operator manager/pkg/apis/operatorcontroller/v1/register.go into pkg/apis//v1/.
Make appropriate changes to this file.
Define your CRD spec in types.go
Nothing else needs to be created in the pkg/apis directory
Create pkg/client directory. Nothing needs to be created inside the client directory -- leave it empty.
Create pkg/signals directory and just copy all the files from https://github.com/cloud-ark/kubeplus/tree/master/postgres-crd-v2/pkg/signals into pkg/signals.
You don't need to modify anything in any of these files.
Go inside the hack/ directory.
Edit update-codegen.sh to include the path of your folder
Copy Gopkg.lock and Gopkg.toml from
https://github.com/cloud-ark/kubeplus/tree/master/operator-manager
Install Go dependencies
dep ensure (this will create the vendor folder)
mkdir vendor/k8s.io/code-generator/hack
cp hack/boilerplate.go.txt vendor/k8s.io/code-generator/hack/.
From the main directory of your CRD code execute following:
./hack/update-codegen.sh
Running the controller:
In the controller.go (and other go files), include the CRD client/listers/informers etc. with appropriate path. See https://github.com/cloud-ark/kubeplus/blob/master/postgres-crd-v2/controller.go#L47 for an example.
go run *.go -kubeconfig=$HOME/.kube/config
Reference:
kubernetes/sample-controller#13
The text was updated successfully, but these errors were encountered: