A Guide to Sample-Controller written in Go K8s

Rajiv Sharma
2 min readJul 31, 2024

--

Automate Rolling Restart of deployment on making changes in configmaps.

If you’ve ever needed to ensure that your Kubernetes deployment pods are automatically restarted whenever a configuration change is made, you’re in the right place. Today, we’re diving into the sample-controller, a Kubernetes controller designed to handle rolling restarts of deployment pods triggered by changes in a ConfigMap.

What is Sample-Controller?

The sample-controller is a Kubernetes controller that performs rolling restarts on deployment pods when there are updates to a ConfigMap. Inspired by the k8spatterns expose-controller, this implementation focuses on reacting to changes in the ConfigMap’s data.

Note: This controller is set to work with the default namespace and expects a ConfigMap named webapp-config and a deployment named webapp.

Getting Started

Prerequisites

Before you can run the sample-controller, ensure you have the following tools installed:

  • Go version 1.22.0 or later
  • Docker version 17.03 or later
  • kubectl version 1.11.3 or later
  • Access to a Kubernetes cluster version 1.11.3 or later

Running Locally

To get started, follow these steps to run the sample-controller on your local machine using Minikube:

  1. Start Minikube
minikube start

2. Deploy the Sample Application

Deploy a sample application to test the controller. Apply the configuration using:

kubectl apply -f ./config/samples/webapp.yaml

3. Get the Endpoint for the Web Application

Retrieve the URL to access the web application:

minikube service list

| NAMESPACE| NAME | TARGETPORT | URL |
| default | webapp| http/8080| http://192.168.49.2:31803 |

4. Verify the Web Application

Use curl to see the sample data:

curl http://192.168.49.2:31803

Expected output: sample data for configmap!

5. Run the Controller

Launch the sample-controller locally:

make run

You should see log output similar to this:

/home/devops/projects/git/sample-controller/bin/controller-gen rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
/home/devops/projects/git/sample-controller/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
go fmt ./...
internal/controller/configmap_controller.go
go vet ./...
go run ./cmd/main.go
2024-07-30T17:18:24+05:30 INFO setup starting manager
2024-07-30T17:18:24+05:30 INFO starting server {"name": "health probe", "addr": "[::]:8081"}
2024-07-30T17:18:24+05:30 INFO Starting EventSource {"controller": "configmap", "controllerGroup": "", "controllerKind": "ConfigMap", "source": "kind source: *v1.ConfigMap"}
2024-07-30T17:18:24+05:30 INFO Starting Controller {"controller": "configmap", "controllerGroup": "", "controllerKind": "ConfigMap"}
2024-07-30T17:18:24+05:30 INFO Starting workers {"controller": "configmap", "controllerGroup": "", "controllerKind": "ConfigMap", "worker count": 1}

6. Update the ConfigMap

Modify the ConfigMap to test the rolling restart:

kubectl edit cm webapp-config

After saving the changes, you’ll see logs indicating the update:

2024-07-30T17:48:18+05:30 INFO ConfigMap Updated: webapp-config
2024-07-30T17:48:18+05:30 INFO ConfigMap Data: map[message:working as expected!]
2024-07-30T17:48:18+05:30 INFO Triggered rolling restart for Deployment: webapp

7. Verify the Rolling Restart

Check that the pods have restarted:

kubectl get pods -n default

Finally, use curl to confirm that your changes have been applied:

--

--

No responses yet