xcoulon / podset-operator

An example of Kubernetes Operator using the Operator SDK
Apache License 2.0
46 stars 30 forks source link

= PodSet Operator

An example of Kubernetes Operator built with the https://github.com/operator-framework/operator-sdk[Operator SDK].

This PodSet operator takes care of scaling up and down pods that run a sleep 3600 command in a busybox image. Nothing really fancy, but the point is to:

== Requirements

== Building and deploying

Before deploying the operator, we need to create the link:./deploy/crds[Custom Resource Definitions], which (as their name suggests) define the custom resources we will manage with this operator, and we also need to configure the link:./deploy/service_account.yaml[Service Account] along with its link:./deploy/role.yaml[role] and link:./deploy/role_binding.yaml[role binding].

The commands below use the oc CLI which is the equivalent of kubectl for OpenShift.

# create a project/namespace for the Service Account and the operator controller
$ oc new-project podset-operator-test
Now using project "podset-operator-test" on server ...

# Create the CRDs
$ oc create -f deploy/crds/app.example.com_podsets_crd.yaml
customresourcedefinition.apiextensions.k8s.io/podsets.app.example.com created

# create the Service Account
$ oc create -f deploy/service_account.yaml
serviceaccount/podset-operator created

# apply the role and role binding to the service account
$ oc create -f deploy/role.yaml
role.rbac.authorization.k8s.io/podset-operator created

$ oc create -f deploy/role_binding.yaml
rolebinding.rbac.authorization.k8s.io/podset-operator created

=== Testing the operator

The Operator SDK provides a command to run the operator controller locally (ie, on the developer's machine). In this mode, the operator controller will use the cluster's external API to create, list, update, delete and watch the resources in the namespace.

$ operator-sdk up local --namespace podset-operator-test
INFO[0000] Running the operator locally.                
INFO[0000] Using namespace podset-operator-test.        
{"level":"info","ts":1573804940.828196,"logger":"cmd","caller":"manager/main.go:26","msg":"Go Version: go1.13.4"}
{"level":"info","ts":1573804940.828258,"logger":"cmd","caller":"manager/main.go:27","msg":"Go OS/Arch: darwin/amd64"}
...

in a separate terminal session, we can use the link:./deploy/crds/app_v1alpha1_podset_cr.yaml[custom resource example] to see how the operator controller reacts:

# create a custom PodSet resource to obtain 3 pods
$ oc create -f deploy/crds/app.example.com_podsets_cr.yaml
podset.app.example.com/example-podset created

# after checking the operator controller logs...
$ oc get pods
NAME                      READY   STATUS    RESTARTS   AGE
example-podset-pod46ttq   1/1     Running   0          2m19s
example-podset-pod885mx   1/1     Running   0          2m20s
example-podset-podtqdzs   1/1     Running   0          2m19s

== License

This project is available under the link:./LICENSE[Apache License, Version 2.0].