|
| 1 | +## DontPanic kubernetes controller |
| 2 | + |
| 3 | +This is an example of a k8s controller that demonstrates its availability irrespective of an runtime error or due to misconfigurations. In other words, controller should not crash. |
| 4 | + |
| 5 | +- This is a k8s controller that imports metac as library |
| 6 | + - This enables use of inline hooks instead of webhooks |
| 7 | +- Controller's kubernetes resources are configured in a config file |
| 8 | + - Refer: config/config.yaml |
| 9 | +- Controller business logic is implemented in Go |
| 10 | + - Refer: cmd/main.go |
| 11 | + - Kubernetes client libraries are completely abstracted from this logic |
| 12 | + - Logic is implemented in respective reconcile functions |
| 13 | + - A CR `IAmError` is sent as the response i.e. desired state |
| 14 | + - NOTE: `IAmError`'s definition i.e. _(CRD)_ is not set in k8s cluster |
| 15 | + - Hence this reconciliation should **error out** |
| 16 | +- Expectations: |
| 17 | + - Binary should **not panic** inspite of above error |
| 18 | + - Log should provide the root cause of error |
| 19 | +- Docker image includes the binary as well as its config file |
| 20 | + - Refer: Dockerfile |
| 21 | +- Controller is deployed as a single StatefulSet |
| 22 | + - No need of separate metac binary since metac is imported as a library |
| 23 | + - Refer: dontpanic-operator.yaml |
| 24 | + |
| 25 | +### Steps |
| 26 | + |
| 27 | +```sh |
| 28 | +# workstation needs to have Docker |
| 29 | +# use kind to create a k8s cluster |
| 30 | +# |
| 31 | +# Refer: https://kind.sigs.k8s.io/docs/user/local-registry/ |
| 32 | +sudo ./kind-with-registry.sh |
| 33 | + |
| 34 | +# cat $HOME/.kube/config |
| 35 | +# connect to kind cluster |
| 36 | +sudo kubectl cluster-info --context kind-kind |
| 37 | + |
| 38 | +# debugging info if required |
| 39 | +# |
| 40 | +# Kubernetes master is running at https://127.0.0.1:32774 |
| 41 | +# |
| 42 | +# KubeDNS is running at: |
| 43 | +# https://127.0.0.1:32774/api/v1/namespaces/# kube-system/services/kube-dns:dns/proxy |
| 44 | +# |
| 45 | +# To further debug and diagnose cluster problems, use |
| 46 | +#'kubectl cluster-info dump'. |
| 47 | +``` |
| 48 | + |
| 49 | +```sh |
| 50 | +# NOTE: |
| 51 | +# - Docker daemon always runs as a root user |
| 52 | +# - sudo may not be required depending on individual confgurations |
| 53 | +# - sudo is needed if docker group is not configured |
| 54 | +# - KIND runs entirely as containers |
| 55 | +# - Hence, all kubectl commands might need to used with sudo |
| 56 | +``` |
| 57 | + |
| 58 | +```sh |
| 59 | +# workstation needs to have Docker |
| 60 | +make image |
| 61 | + |
| 62 | +# tag the image to use the local registry |
| 63 | +sudo docker tag dontpanic:latest localhost:5000/dontpanic:latest |
| 64 | + |
| 65 | +# push to local registry configured to be used by kind |
| 66 | +sudo docker push localhost:5000/dontpanic:latest |
| 67 | +``` |
| 68 | + |
| 69 | +```sh |
| 70 | +# install namespace, rbac, crds & operator |
| 71 | +sudo kubectl apply -f dontpanic-ns.yaml |
| 72 | +sudo kubectl apply -f dontpanic-rbac-crd.yaml |
| 73 | +sudo kubectl apply -f dontpanic-operator.yaml |
| 74 | + |
| 75 | +# verify if above were installed properly |
| 76 | +sudo kubectl get ns |
| 77 | +sudo kubectl get crd |
| 78 | +sudo kubectl get sts -n dontpanic |
| 79 | +sudo kubectl describe po -n dontpanic |
| 80 | +sudo kubectl get po -n dontpanic |
| 81 | +sudo kubectl logs -n dontpanic dontpanic-0 |
| 82 | +``` |
| 83 | + |
| 84 | +### Test |
| 85 | + |
| 86 | +```sh |
| 87 | +# check operator pod |
| 88 | +sudo kubectl get pods -n dontpanic |
| 89 | + |
| 90 | +# check operator pod logs |
| 91 | +sudo kubectl logs -n dontpanic dontpanic-0 |
| 92 | + |
| 93 | +# create the dontpanic custom resource |
| 94 | +sudo kubectl apply -f dontpanic.yaml |
| 95 | + |
| 96 | +# check operator pod logs |
| 97 | +sudo kubectl logs -n dontpanic dontpanic-0 |
| 98 | +``` |
| 99 | + |
| 100 | +### Observations |
| 101 | +- Binary did not panic |
| 102 | +- Following were the logs that points the root cause |
| 103 | + |
| 104 | +```bash |
| 105 | +I0402 15:33:10.482481 1 discovery.go:174] API resources discovery completed |
| 106 | +I0402 15:33:10.651607 1 metacontroller.go:270] Condition failed: Will retry after 1s: Local GenericController: Failed to init dontpanic-controller: Local GenericController: Selector init failed: Can't find "iamerrors": Version "notsure.com/v1" |
| 107 | +``` |
| 108 | +
|
| 109 | +### Cleanup |
| 110 | +
|
| 111 | +```sh |
| 112 | +sudo kubectl delete -f dontpanic.yaml |
| 113 | +sudo kubectl delete -f dontpanic-operator.yaml |
| 114 | +sudo kubectl delete -f dontpanic-rbac-crd.yaml |
| 115 | +sudo kubectl delete -f dontpanic-ns.yaml |
| 116 | +
|
| 117 | +sudo kind delete cluster |
| 118 | +``` |
0 commit comments