Skip to content

hsaid4327/configwatcher-go-operator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Operator

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.

Pre-requisites

In order to run deploy and test this operator locally, the following tools are requried:

Testing it on local OCP or K8 cluster

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).

Running on the cluster

  1. Install the CRD (custom resource definition) From the root of the project directory:
  make install
  1. 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
  1. 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
  1. 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

  1. 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
  1. Deploy the CR (custom resource) on the cluster:
 apply -f config/samples/tutorials_v1_configwatcher.yaml
  1. Create a route for the application
 oc expose svc webapp
  1. Access the application using the route
curl http://$(oc get route -o jsonpath='{.items[0].spec.host}')
  1. Update the configmap using this command:
oc  patch configmap webapp-config -p '{"data":{"message":"Greets from your smooth operator!"}}'
  1. Verify that your application message is updated by running the step 8)

Uninstall CRDs

To delete the CRDs from the cluster:

make uninstall

Undeploy controller

UnDeploy the controller to the cluster:

make undeploy IMG=<the image in the registry>

How it works

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published