A demo K8s controller that listens for the custom resources based on the following CRD. Based on https://itnext.io/how-to-generate-client-codes-for-kubernetes-custom-resource-definitions-crd-b4b9907769ba and https://github.com/kubernetes/sample-controller
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: hellotypes.foo.com
spec:
group: foo.com
scope: Namespaced
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
message:
type: string
status:
type: object
properties:
availableReplicas:
type: integer
subresources:
status: {}
additionalPrinterColumns:
- name: Replicas
type: string
jsonPath: .status.availableReplicas
names:
kind: HelloType
shortNames:
- ht
plural: hellotypes
singular: hellotype
- Install the following tools to your
GOBIN
directory.$ go install k8s.io/code-generator/cmd/client-gen@latest $ go install k8s.io/code-generator/cmd/applyconfiguration-gen@latest $ go install k8s.io/code-generator/cmd/conversion-gen@latest $ go install k8s.io/code-generator/cmd/deepcopy-gen@latest $ go install k8s.io/code-generator/cmd/defaulter-gen@latest $ go install k8s.io/code-generator/cmd/go-to-protobuf@latest $ go install k8s.io/code-generator/cmd/informer-gen@latest $ go install k8s.io/code-generator/cmd/prerelease-lifecycle-gen@latest $ go install k8s.io/code-generator/cmd/register-gen@latest
- Clone the kubernetes code-generator repository
cd ~
mkdir -p go/src/k8s.io
cd go/src/k8s.io
git clone https://github.com/kubernetes/code-generator.git
cd code-generator
git checkout v0.29.2
- Follow this guide to create the superman/demo repo with the golang code in the appropriate files:
pkg/apis/foo/v1/doc.go
pkg/apis/foo/v1/register.go
pkg/apis/foo/v1/types.go
- In your demo repo
go mod init
git init
git add .
git commit -m "check in all demo files"
- Run the following to generate the k8s required code:
/go/src/k8s.io/code-generator/generate-groups.sh all "github.com/superman/demo/pkg/client" "github.com/superman/demo/pkg/apis" foo:v1 --go-header-file ~/go/src/k8s.io/code-generator/examples/hack/boilerplate.go.txt
- Copy the generated code that was generated to github.com/ inside of your repo to the top level demo folder
cp -R github.com/jacob-delgado/demo/pkg .
rm -rf github.com
go mod tidy
git add .
git commit -m "check in generated files"