Tutorial for building Kubernetes Custom Resources (CRD) extensions you can see the full tutorial documentation in: The New Stack
Note: CustomResourceDefinition (CRD) is the successor of the deprecated ThirdPartyResource.
this example is based on Kubernetes apiextensions-apiserver example
the example contain 3 files:
- crd - define and register our CRD class
- client - client library to create and use our CRD (CRUD)
- kube-crd - main part, demonstrate how to create, use, and watch our CRD
# assumes you have a working kubeconfig, not required if operating in-cluster
go run *.go -kubeconf=$HOME/.kube/config
kube-crd demonstrates the CRD usage, it shoes how to:
- Connect to the Kubernetes cluster
- Create the new CRD if it doesn't exist
- Create a new custom client
- Create a new Example object using the client library we created
- Create a controller that listens to events associated with new resources
The example CRD is in the following structure:
type Example struct {
meta_v1.TypeMeta `json:",inline"`
meta_v1.ObjectMeta `json:"metadata"`
Spec ExampleSpec `json:"spec"`
Status ExampleStatus `json:"status,omitempty"`
}
type ExampleSpec struct {
Foo string `json:"foo"`
Bar bool `json:"bar"`
Baz int `json:"baz,omitempty"`
}
type ExampleStatus struct {
State string `json:"state,omitempty"`
Message string `json:"message,omitempty"`
}
- The Metadata part contain standard Kubernetes properties like name, namespace, labels, and annotations
- The Spec contain the desired resource configuration
- The Status part is usually filled by the controller in response to Spec updates
Now test CodeFresh