Watch-Tower is a Kubernetes controller written in Go that:
- Monitors an AutomationController Custom Resource (CRD).
- Checks the PostgreSQL database role (Primary/Standby).
- Automatically scales the AutomationController based on:
- If the DB is Primary, it sets
spec.replicas = watch-tower/replicasannotation. - If the DB is Standby, it sets
spec.replicas = 0.
- If the DB is Primary, it sets
✅ Continuously monitors AutomationController CRD
✅ Detects PostgreSQL role (Primary/Standby)
✅ Reads watch-tower/replicas annotation for scaling
✅ Uses Kubernetes API to patch spec.replicas
✅ Runs in an infinite loop for real-time updates
Ensure you have the following installed:
- Go 1.20+ (
go version) - Kubernetes CLI (kubectl) (
kubectl version --client) - OpenShift CLI (oc) (if using OpenShift) (
oc version) - PostgreSQL running in Kubernetes
git clone https://github.com/your-org/watch-tower.git
cd watch-towergo mod tidyexport DB_CREDENTIAL_PATH="/srv/db_credential"
export AAP_NAMESPACE="aap"
export KUBECONFIG=~/.kube/configgo run main.go-
Loops every 30 seconds to check:
- PostgreSQL role (
PrimaryorStandby). - AutomationController resources in the namespace.
- PostgreSQL role (
-
Reads the
watch-tower/replicasannotation:- If missing, it skips the resource.
- If invalid, it logs a warning.
-
Decides the correct replica count:
- If Primary, it uses the annotation value.
- If Standby, it scales down to
0.
-
Checks the current
spec.replicas:- ✅ If already correct, it skips patching.
- 🔄 If different, it patches the CRD.
Here’s an example AutomationController CRD with the required annotation:
apiVersion: automationcontroller.ansible.com/v1beta1
kind: AutomationController
metadata:
name: example
namespace: aap
annotations:
watch-tower/replicas: "3"
spec:
replicas: 3- The annotation
watch-tower/replicas: "3"tells Watch-Tower how many replicas to use if the DB is Primary.
✅ Using local Kubeconfig
🚀 Starting Watch Loop for AutomationController...
🔄 Checking AutomationController and Database Role...
✅ Successfully connected to PostgreSQL!
🔍 Database Role: Primary
✅ No update needed for example (replicas already set to 3)
✅ Using local Kubeconfig
🚀 Starting Watch Loop for AutomationController...
🔄 Checking AutomationController and Database Role...
✅ Successfully connected to PostgreSQL!
🔍 Database Role: Standby
✅ Successfully patched example: spec.replicas = 0