-
Git
-
Go version 1.12.x
-
operator-sdk version v0.8.x
-
docker version 17.03+ or buildah version 1.8.2+
-
oc
-
Access to an OpenShift cluster
-
Quay account
Note: You can chose between using dep or modules. Modules are used by default. In next versions it will also be added support by default for external vendoring (i.e. --vendor=false
).
cd projects
operator-sdk new hellogo-operator --repo=github.com/radudd/hellogo-operator
cd hellogo-operator
After that review the generated code and resources. Pay special attention to the following:
-
deploy
: folder containing yaml files to easily run your operator on kubernetes -
cmd/manager
: The manager register the scheme for all custom resources underpkg/apis
and run all controlers underpkg/controller
-
pkg/controller
: The controllers implementation -
pkg/apis
: The custom types definitions
Check the [project scaffolding layout](https://github.com/operator-framework/operator-sdk/blob/master/doc/project_layout.md) for more details
operator-sdk add api --api-version=hellogo.workshop.redhat.com/v1alpha1 --kind=HelloGo
After that we can see the following changed:
-
deploy/crds
is created with a basic CRD (including OpenApiV3Schema validation) and a sample CR -
pkg/apis/hellogo/v1alpha1
contains a filehellogo_types.go
with the CRD structs
operator-sdk add controller --api-version=hellogo.workshop.redhat.com/v1alpha1 --kind=HelloGo
Let’s review:
-
pkg/controller/hellogo/hellogo_controller.go
has a default implementation that creates abusybox
pod
To update the spec of your CRD, go to the pkg/apis/hellogo/v1alpha1/hellogo_types.go
file and add the necessary attributes - in this example we add a size
attribute
type HelloGoSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
// Add custom validation using kubebuilder tags: https://book-v1.book.kubebuilder.io/beyond_basics/generating_crd.html
Size int32 `json:"size"`
}
When finished, run the following to update the generated code:
operator-sdk generate k8s
Note
|
Run this command each time you change the types |
operator-sdk generate openapi
Note
|
Run this command only when you want to replace the CRD with the new generated values. This will overwrite any customization |
-
Watch for changes in owned objects: just add here the ones you feel necessary.
-
Implement the reconcile loop: You will receive notification changes for all the watched objects (CustomResources and OwnedObjects). Create the owned objects that are missing or replace them if the expected objects don’t match with the existing ones. Let’s replace the
busybox
image with thehelloworld
one.
// newPodForCR returns a busybox pod with the same name/namespace as the cr func newPodForCR(cr *hellogov1alpha1.HelloGo) *corev1.Pod { labels := map[string]string{ "app": cr.Name, } return &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: cr.Name + "-pod", Namespace: cr.Namespace, Labels: labels, }, Spec: corev1.PodSpec{ Containers: []corev1.Container{ { Name: "hellogo", // changed here Image: "openshift/hello-openshift", // changed here Command: []string{"sleep", "3600"}, // remove this line }, }, }, } }
-
Update the
role.yaml
in case different permissions are required (e.g. createingress
orroute
)
Note
|
Replace <myuser-namespace> with your namespace |
oc project <myuser-namespace>
oc create -f deploy/crds/hellogo.workshop.redhat.com_hellogos_crd.yaml
go mod vendor
operator-sdk up local --namespace=<myuser-namespace>
Now you can create an example CR. Notice that the generated CR is not valid.
Note
|
Replace <user> with your own username |
operator-sdk build --image-builder docker {{IMAGE_REGISTRY}}/<user/>go-operator:latest
... INFO[0008] Operator build complete.
Push the image to your quay.io repository (make sure the repository is public for this workshop)
docker push {{IMAGE_REGISTRY}}/<user/>go-operator:latest
Before deploying the Operator make sure you replaced all the placeholders:
sed -i 's|REPLACE_IMAGE|{{IMAGE_REGISTRY}}/<user/>go-operator:latest|g' deploy/operator.yaml
Create all the necessary resources:
oc create -f deploy
deployment.apps/hellogo-operator created role.rbac.authorization.k8s.io/hellogo-operator created rolebinding.rbac.authorization.k8s.io/hellogo-operator created serviceaccount/hellogo-operator created
Deploy the CR and check if the hello pod is created by the operator
oc create -f deploy/crds/hellogo.workshop.redhat.com_v1alpha1_hellogo_cr.yaml
oc get pods