This project show cases how to implement a golang operator. It uses operator-sdk to initialize and build the project. The operator logic is based on a use case of application redeploying automatically when the configmap, which is mounted on the application, is updated. This is a common scenario that many applications encounter whereby a change in configmap does not automatically start an application and some manual intervention is required. This operator automate this common chore.
In order to run deploy and test this operator locally, the following tools are requried:
-
openshift local installation
-
Operator-sdk for golang
-
Podman is installed and the classpath is configured for it:
You need a local OCP cluster in order to deploy and run the operator and test it with an application. These intructions assume that there is an OCP cluster running locally.
Note: Your controller will automatically use the current context in your kubeconfig file (i.e. whatever cluster oc cluster-info
shows).
- Install the CRD (custom resource definition) From the root of the project directory:
make install
- Build and push your image to the location specified by
IMG
. In order to push to the registry, you need to be logged on the registry with podman
make docker-build docker-push IMG=<some-registry>/configwatcher-go-operator:tag
-
In order to make sure that the operator is deployed to a specific namespace, and that namespace has access to the registry in step 2) above, the following changes need to be made:
-
Create a project in the OCP
oc new-project <project-name>
-
Change the config/default/kustomization.yaml file to edit the project name to match the name of your project
# Adds namespace to all resources. namespace: <project-name>
-
Create a secret in the OCP project to access the registry:
oc create secret docker-registry my-secret --docker-server=quay.io --docker-username=<u-name> --docker-password=<password> --docker-email=<email>
-
Change the rbac/service_account.yaml file with the following:
-
apiVersion: v1
imagePullSecrets:
- name: my-secret
kind: ServiceAccount
metadata:
labels:
app.kubernetes.io/name: serviceaccount
app.kubernetes.io/instance: controller-manager
app.kubernetes.io/component: rbac
app.kubernetes.io/created-by: memcached-operator
app.kubernetes.io/part-of: memcached-operator
app.kubernetes.io/managed-by: kustomize
name: controller-manager
namespace: system
secrets:
- name: my-secret
- Deploy the controller to the cluster with the image specified by
IMG
:
make deploy IMG=<some-registry>/configwatcher-go-operator:tag
note: ake sure you are on the targeted OCP project created in the previous step
- Deploy the application on the OCP. This is the sample application having a configmap mounted that would be updated in the later step to test the workings of the operator:
oc apply -f extra/web-app.yaml
- Deploy the CR (custom resource) on the cluster:
apply -f config/samples/tutorials_v1_configwatcher.yaml
- Create a route for the application
oc expose svc webapp
- Access the application using the route
curl http://$(oc get route -o jsonpath='{.items[0].spec.host}')
- Update the configmap using this command:
oc patch configmap webapp-config -p '{"data":{"message":"Greets from your smooth operator!"}}'
- Verify that your application message is updated by running the step 8)
To delete the CRDs from the cluster:
make uninstall
UnDeploy the controller to the cluster:
make undeploy IMG=<the image in the registry>
This project aims to follow the Kubernetes Operator pattern
It uses Controllers which provides a reconcile function responsible for synchronizing resources untile the desired state is reached on the cluster