Guide for autogeneratng client codes for Kubernetes Custom Resource Definitions (CRD)
-
take the configuration in directory in
pkg/apis/foo.com/
as template to define the new crd" -
Disable go modules
export GO111MODULE=off
- Download the generator repository
go get k8s.io/code-generator
- Download this repository
go get github.com/afarid/kubernetes-crd-example/pkg/apis/foo.com/v1
- Run this command inside this project directory:
cd $GOPATH/src/github.com/afarid/kubernetes-crd-example
../../../k8s.io/code-generator/generate-groups.sh all "github.com/afarid/kubernetes-crd-example/pkg/clients" "github.com/afarid/kubernetes-crd-example/pkg/apis" "foo.com:v1" -h ../../../k8s.io/code-generator/hack/boilerplate.go.txt
After running the above command, you will see a new directory pkg/clients/
.
- use generated package in your code, this is an example of how to use the generated package
package main
import (
clientset "github.com/afarid/kubernetes-crd-example/pkg/clients/clientset/versioned"
)
func main() {
_, _ = clientset.NewForConfig(nil)
}