Notify kubernetes events with botkube
Tags: kubernetes, day2, webhook
Published: 2022-04-12 00:43:22

At Cisco we use Webex as our IM collaboration engine. It is same as Teams/Slack with communication capabilities. One can build a bot for interaction and use APIs to communicate with webex teams.
As a platform team we want to notify interested parties when events happen like pod creation, deployment rollout, pod errors ...
When exploring around I came across [botkube](https://www.botkube.io/) which can help solve our problem to a major extent

##### Architecture
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lx6fu3iieqxb7myihh0g.png)
Source: [https://www.botkube.io/architecture/](https://www.botkube.io/architecture/)

###### Modes of usage
- Full fledged chatbot - integrations to Slack, Mattermost and MS Teams
- Sink - to push events, used for datastores like Elasticsearch and webhooks for use-cases
- Custom filters - for validations and recommendations

---

##### Install botkube
1. Prepare a webhook listener (`<WEBHOOK_URL>`)
2. Decide on a name for your cluster (any string: `<CLUSTER_NAME>`)
3. Get and update configuration values
```bash
$ wget https://raw.githubusercontent.com/infracloudio/botkube/v0.12.4/helm/botkube/sample-res-config.yaml
$ mv sample-res-config.yaml /path/to/config.yaml
$ vi /path/to/config.yaml
```
4. Install
```bash
$ helm repo add infracloudio https://infracloudio.github.io/charts
$ helm repo update
$ helm install --version v0.12.4 botkube --namespace botkube \
--set communications.webhook.enabled=true \
--set communications.webhook.url=<WEBHOOK_URL> \
--set config.settings.clustername=<CLUSTER_NAME> \
--set image.repository=infracloudio/botkube \
--set image.tag=v0.12.4 \
-f /path/to/config.yaml
infracloudio/botkube
```

5. Check the installation:
```
$ kubectl get-all -n botkube
NAME                                  NAMESPACE  AGE
configmap/botkube-configmap           botkube    6d22h
configmap/kube-root-ca.crt            botkube    6d22h
pod/botkube-785d6c889-srbzg           botkube    6d22h
secret/botkube-communication-secret   botkube    6d22h
secret/botkube-sa-token-btjvj         botkube    6d22h
secret/default-token-d5s9n            botkube    6d22h
secret/sh.helm.release.v1.botkube.v1  botkube    6d22h
serviceaccount/botkube-sa             botkube    6d22h
serviceaccount/default                botkube    6d22h
deployment.apps/botkube               botkube    6d22h
replicaset.apps/botkube-785d6c889     botkube    6d22h
```
[get-all](https://github.com/corneliusweig/ketall) is a krew plugin

---
##### Usage
Now when identified events are triggered, webhook url is `POST`ed with an appropriate json payload
```bash
$ kubectl logs -n botkube pod/botkube-785d6c889-srbzg
...
INFO[2022-04-09T13:27:26Z] Registering resource lifecycle informer
INFO[2022-04-09T13:27:26Z] Adding informer for resource:apps/v1/deployments
INFO[2022-04-09T13:27:26Z] Adding informer for resource:apps/v1/statefulsets
INFO[2022-04-09T13:27:26Z] Adding informer for resource:apps/v1/daemonsets
INFO[2022-04-09T13:27:26Z] Adding informer for resource:v1/nodes
INFO[2022-04-09T13:27:26Z] Registering kubernetes events informer for types: warning
INFO[2022-04-09T13:27:26Z] Registering kubernetes events informer for types: normal
INFO[2022-04-09T13:27:26Z] Registering watcher on configfile /config/resource_config.yaml
```
Payloads will be like:
```json
{
    "meta": {
        "cluster": "macbook-kind",
        "kind": "Deployment",
        "name": "test-dep4",
        "namespace": "default"
    },
    "status": {
        "level": "info",
        "type": "create"
    },
    "summary": "Deployment *default/test-dep4* has been created in *macbook-kind* cluster\n",
    "timestamp": "2022-04-12T00:38:15Z"
}
```
---

##### Conclusion
`botkube` is capable of a lot more including building a full-fledged chatbot for managing kubernetes cluster and provides extension points as well. Do check-out the [documentation](https://www.botkube.io/)

There is another similar project called [kubewatch](https://github.com/vmware-archive/kubewatch), but not active anymore