Gitops w/ FluxCD: Add Notifications for Slack.
What is a Notification Controller?
Its true purpose is evident right from its name. It sends notifications to our commonly used communication channels like MS Teams, Discord and Slack.
![](https://hestia.ghost.io/content/images/2024/02/image-478.png)
Slack Notification
![](https://hestia.ghost.io/content/images/2024/02/image-479.png)
A Notification requires that an Alert (made using Alert CRDs) is sent to a Provider through a Provider CRD. An incoming webhook from Slack into the cluster is also required and is authenticated using a K8s Secret.
Demo: Configure FluxCD to Notify a Slack Channel.
Of course, the first thing one should have ready (or access to) is a Slack channel to send notifications.
Step 1: A Slack channel (gitops-channel) was created.
![](https://hestia.ghost.io/content/images/2024/02/image-480.png)
Step 2: Add the Incoming webhooks plugin for Slack
![](https://hestia.ghost.io/content/images/2024/02/image-481.png)
Search for the Incoming webhook Webhook using the search bar.
![](https://hestia.ghost.io/content/images/2024/02/image-483.png)
Configure the plugin to send messages to the gitops-channel (or any channel of choice).
![](https://hestia.ghost.io/content/images/2024/02/image-484.png)
Copy the Webhook URL. This URL will be saved in our cluster and the Notification Controller we end up creating will send alerts and messages to this URL's endpoint (which is ... and no prizes for guessing right ... gitops-channel).
Step 3: Add the Webhook URL as a Kubernetes Secret.
kubectl create secret generic slack-url \
--from-literal=address=<URL> \
-n flux-system
With the Secret created, we will move on to creating an Alert-Provider.
![](https://hestia.ghost.io/content/images/2024/02/image-500.png)
Step 4: Add a Provider that connects FluxCD to Slack.
The Provider CRD is bootstrapped (read this article for details) and we can confirm its presence using kubectl. The Provider CRD is used for generating the Provider object.
What does a Provider Object look like?
![](https://hestia.ghost.io/content/images/2024/02/image-490.png)
We can check the Provider CRD was bootstrapped.
kubectl get crds -n flux-system
![](https://hestia.ghost.io/content/images/2024/02/image-486.png)
Create the Provider as shown below.
flux create alert-provider slack \
--type=slack \
--channel= gitops-channel \
--secret-ref=slack-url
![](https://hestia.ghost.io/content/images/2024/02/image-487.png)
![](https://hestia.ghost.io/content/images/2024/02/image-496.png)
Step 5: Configure an Alert to send notifications to Slack
We are now in the final stages of our setup.
Recapping our journey so far:
- We have a Slack channel ready
- We have an Alert-Provider object, configured for providing alerts to Slack
The last piece of this puzzle is an Alert object. An Alert object encapsulates the rules under which the Alert-Provider will send a message to Slack.
What does an Alert look like?
![](https://hestia.ghost.io/content/images/2024/02/image-493.png)
flux create alert slack-notif \
--provider-ref=slack \
--event-source=GitRepository/* \
--event-source=Kustomization/* \
--event-source=HelmRelease/* \
--event-severity=info
The rules under which Alert-Provider/Slack will send any message to #gitops-channel are provided as event-sources above.
The rules in slack-notif are:
If there are any events like reconciliations, health checks passing and/or failures in ANY GitRepository, Kustomization or HelmRelease, extract just the informational logs for these events and pass them onto the Alert-Provider/Slack.
Switch to Slack to see the messages flowing into the targetted channel.
![](https://hestia.ghost.io/content/images/2024/02/image-492.png)
All parts of the notification pipeline are done and messages are flowing to Slack.
![](https://hestia.ghost.io/content/images/2024/02/image-495.png)
Step 6: Behave like a responsible team member and check in YAML for Alert and Alert-Provider
# Assumption: You are at the root of flux-infra/clusters/staging
flux export alert slack-notif > slack-notif-alert.yaml
flux export alert-provider slack > slack-alert-provider-staging.yaml
git add *
git commit -m "some comment"
git push origin main
Architecturally Speaking
![](https://hestia.ghost.io/content/images/2024/02/image-499.png)
Summary: What and Why?
![](https://hestia.ghost.io/content/images/2024/02/image-497.png)
GitHub Folder Structure So Far
![](https://hestia.ghost.io/content/images/2024/03/image-7.png)
I write to remember, and if, in the process, I can help someone learn about Containers, Orchestration (Docker Compose, Kubernetes), GitOps, DevSecOps, VR/AR, Architecture, and Data Management, that is just icing on the cake.