Tag 10: Image Automation II – Flux committet
Ziel: Flux die gewählte Version selbst ins Fleet-Repo schreiben lassen, erst direkt auf
main, dann als Branch für einen Pull Request, und die Stolperfallen dabei kennen. · Dauer: ca. 60 min · Voraussetzung: Tag 9
Briefing
- Die ImageUpdateAutomation klont das Fleet-Repo, sucht in einem Ordner nach Setter-Markierungen, ersetzt die Werte dahinter durch das Ergebnis einer ImagePolicy, committet und pusht.
- Eine Setter-Markierung ist ein YAML-Kommentar am Ende einer Zeile:
NOTES_VERSION: 0.7.0 # {"$imagepolicy": "flux-system:notes-api:tag"}
└ Namespace ─┘ └ Policy ┘ └ Teil ┘
- Der letzte Teil sagt, was eingesetzt wird:
tag(nur0.7.1),name(nur das Image ohne Tag) oder nichts (das ganze Image mit Tag). - Die Markierung funktioniert in jeder YAML-Datei: in Deployments, in
HelmReleases unter
valuesund, wie hier, in der ConfigMap mit den Variablen aus Tag 4.
ImagePolicy notes-api: 0.7.1
│
ImageUpdateAutomation ── klont ── flux-fleet (main)
│ ersetzt 0.7.0 → 0.7.1 in clusters/lab/cluster-vars.yaml
└── commit + push ──► main oder flux/notes-updates (Pull Request)
│
GitRepository flux-system ─► Kustomization notes ─► 0.7.1
Drill 1: Die Markierung
Damit es gleich etwas zu tun gibt, stellst du prod zurück auf 0.7.0 und setzt die Markierung dahinter. Die einfachen Anführungszeichen um den sed-Ausdruck schützen das Dollarzeichen:
cd ~/flux-lab/fleet
sed -i 's/ NOTES_VERSION: 0.7.1/ NOTES_VERSION: 0.7.0 # {"$imagepolicy": "flux-system:notes-api:tag"}/' \
clusters/lab/cluster-vars.yaml
cat clusters/lab/cluster-vars.yamlErwartet:
apiVersion: v1
kind: ConfigMap
metadata:
name: cluster-vars
namespace: flux-system
data:
CLUSTER_NAME: lab
NOTES_VERSION: 0.7.0 # {"$imagepolicy": "flux-system:notes-api:tag"}
git commit -am "notes: 0.7.0 mit Setter-Markierung"
git push
flux reconcile kustomization flux-system --with-source
flux reconcile kustomization notes
curl -s localhost:30080/version; echoErwartet (gekürzt):
...
0.7.0
Fortschritt speichern? Kostenlos registrieren
Drill 2: Die Automation
flux create image update notes --git-repo-ref=flux-system \
--git-repo-path=./clusters/lab --checkout-branch=main --push-branch=main \
--author-name=fluxcdbot --author-email=[email protected] \
--commit-template='{{range .Changed.Changes}}{{print .OldValue}} -> {{println .NewValue}}{{end}}' \
--interval=5m --export > clusters/lab/notes-automation.yaml
cat clusters/lab/notes-automation.yamlErwartet:
---
apiVersion: image.toolkit.fluxcd.io/v1
kind: ImageUpdateAutomation
metadata:
name: notes
namespace: flux-system
spec:
git:
checkout:
ref:
branch: main
commit:
author:
email: [email protected]
name: fluxcdbot
messageTemplate: '{{range .Changed.Changes}}{{print .OldValue}} -> {{println
.NewValue}}{{end}}'
push:
branch: main
interval: 5m0s
sourceRef:
kind: GitRepository
name: flux-system
update:
path: ./clusters/lab
strategy: Setters
update.pathbegrenzt die Suche nach Markierungen aufclusters/lab.- Das
messageTemplateist eine Go-Vorlage..Changed.Changesenthält jede einzelne Ersetzung mit altem und neuem Wert. - Die Automation pusht über das Secret der GitRepository
flux-system, also über den Deploy-Key, der seit Tag 9 schreiben darf.
git add -A
git commit -m "notes: Image-Updates automatisch"
git push
flux reconcile kustomization flux-system --with-sourceEin paar Sekunden später:
flux events --for ImageUpdateAutomation/notesErwartet (gekürzt):
Normal Succeeded ImageUpdateAutomation/notes pushed commit 'a414d2c' to branch 'main'
0.7.0 -> 0.7.1
git pull
git log --oneline -3
git log -1 --format='%an <%ae>%n%B'
grep NOTES_VERSION clusters/lab/cluster-vars.yamlErwartet (die Hashes weichen ab):
a414d2c 0.7.0 -> 0.7.1
8a38360 notes: Image-Updates automatisch
bcd5771 notes: 0.7.0 mit Setter-Markierung
fluxcdbot <[email protected]>
0.7.0 -> 0.7.1
NOTES_VERSION: 0.7.1 # {"$imagepolicy": "flux-system:notes-api:tag"}
Der Commit von fluxcdbot läuft den normalen Weg:
GitRepository holt ihn, die Kustomization wendet ihn an.
flux reconcile kustomization flux-system --with-source
flux reconcile kustomization notes
curl -s localhost:30080/version; echoErwartet (gekürzt):
✔ applied revision main@sha1:a414d2ce60a83b10468091e592790c9d12723359
0.7.1
Ab jetzt gilt: Erscheint notes-api 0.7.2 in ghcr, findet
die ImageRepository den Tag beim nächsten Scan, die Policy wählt ihn,
die Automation committet, und prod rollt aus. Niemand muss etwas
tun.
Fortschritt speichern? Kostenlos registrieren
Drill 3: Lieber per Pull Request
Direkt auf main ist bequem, aber für prod mutig. Die
Alternative: Die Automation pusht auf einen eigenen Branch, und ein
Mensch merged.
Die Automation bekommt einen anderen Push-Branch.
checkout bleibt main:
sed -i '/ push:/{n;s#branch: main#branch: flux/notes-updates#}' \
clusters/lab/notes-automation.yaml
grep -n branch clusters/lab/notes-automation.yamlErwartet:
11: branch: main
19: branch: flux/notes-updates
Wieder zurück auf 0.7.0, damit es etwas zu tun gibt:
sed -i 's/NOTES_VERSION: 0.7.1/NOTES_VERSION: 0.7.0/' clusters/lab/cluster-vars.yaml
git commit -am "notes: Updates per Pull Request"
git push
flux reconcile kustomization flux-system --with-source
flux reconcile kustomization notes
flux reconcile image update notes
git fetch
git branch -rErwartet (gekürzt):
...
origin/HEAD -> origin/main
origin/flux/notes-updates
origin/main
git diff main origin/flux/notes-updatesErwartet (gekürzt):
- NOTES_VERSION: 0.7.0 # {"$imagepolicy": "flux-system:notes-api:tag"}
+ NOTES_VERSION: 0.7.1 # {"$imagepolicy": "flux-system:notes-api:tag"}
prod läuft bis zum Merge auf 0.7.0. Auf GitHub zeigt dir das Repo jetzt einen Hinweis „flux/notes-updates had recent pushes“ mit dem Knopf Compare & pull request. Das ist der Weg im Team: Pull Request öffnen, prüfen, mergen. Im Lab mergst du direkt:
git merge --no-edit origin/flux/notes-updates
git push
git push origin --delete flux/notes-updates
flux reconcile kustomization flux-system --with-source
flux reconcile kustomization notes
curl -s localhost:30080/version; echoErwartet (gekürzt):
Updating 005b0f8..3f4f7a7
Fast-forward
clusters/lab/cluster-vars.yaml | 2 +-
...
0.7.1
Das Löschen des Branches ist kein Aufräumen aus Ordnungsliebe. Die Challenge zeigt, warum.
Fortschritt speichern? Kostenlos registrieren
Kontrollpunkt
flux get image updateErwartet: notes, READY True,
repository up-to-date.
git log --format='%an' | sort | uniq -cErwartet (die Zahlen weichen ab): Commits von Flux
(Bootstrap), Flux Lab (du) und fluxcdbot
(Automation).
diff ~/flux-lab/fleet/clusters/lab/notes-automation.yaml \
~/flux-lab/code/loesung/tag10/clusters/lab/notes-automation.yamlErwartet: keine Ausgabe.
Challenge
dev soll ab jetzt immer den neuesten Release-Kandidaten fahren, über
die Policy notes-api-rc aus Tag 9. Setz in
clusters/lab/notes-dev.yaml eine Markierung und bring den
Pull Request der Automation nach main.
Hinweis: In dev gilt substitute vor
substituteFrom (Tag 5). Fang mit 0.6.0-rc.1
an, damit die Automation etwas zu tun hat.
Lösung
sed -i 's/^ substituteFrom:/ substitute:\n NOTES_VERSION: 0.6.0-rc.1 # {"$imagepolicy": "flux-system:notes-api-rc:tag"}\n substituteFrom:/' \
clusters/lab/notes-dev.yaml
tail -7 clusters/lab/notes-dev.yamlErwartet:
wait: true
postBuild:
substitute:
NOTES_VERSION: 0.6.0-rc.1 # {"$imagepolicy": "flux-system:notes-api-rc:tag"}
substituteFrom:
- kind: ConfigMap
name: cluster-vars
git commit -am "notes-dev: Release-Kandidaten"
git push
flux reconcile kustomization flux-system --with-source
flux reconcile image update notes
git fetch -p
git log --oneline -1 origin/flux/notes-updatesErwartet (gekürzt):
2dedf40 0.6.0-rc.1 -> 0.6.0-rc.4
Hättest du den Branch nach dem Merge in Drill 3 stehen lassen, käme hier nichts:
Ausprobiert: Gibt es den Push-Branch schon, baut die Automation auf ihm auf und nicht auf
main. Der alte, längst gemergte Branch kennt die neue Markierung innotes-dev.yamlnicht, also meldet die Automationrepository up-to-date. Erst nachgit push origin --delete flux/notes-updateserscheint der neue Commit. Auf GitHub schaltest du dafür Settings → General → Automatically delete head branches ein.
Mergen:
git diff main origin/flux/notes-updatesErwartet (gekürzt):
- NOTES_VERSION: 0.6.0-rc.1 # {"$imagepolicy": "flux-system:notes-api-rc:tag"}
+ NOTES_VERSION: 0.6.0-rc.4 # {"$imagepolicy": "flux-system:notes-api-rc:tag"}
- - kind: ConfigMap
- name: cluster-vars
+ - kind: ConfigMap
+ name: cluster-vars
Die Automation schreibt die ganze Datei neu und rückt die Liste dabei anders ein. Für YAML ist das gleichwertig, im Diff stört es. Wer Ruhe will, formatiert Listen gleich so, wie die Automation sie schreibt.
git merge --no-edit origin/flux/notes-updates
git push
git push origin --delete flux/notes-updates
flux reconcile kustomization flux-system --with-source
flux reconcile kustomization notes-dev
curl -s localhost:30081/version; echoErwartet (gekürzt):
...
0.6.0-rc.4
Fortschritt speichern? Kostenlos registrieren
Enterprise-Notiz
- Pull Requests automatisch öffnen: Flux pusht nur
den Branch. Den Pull Request öffnet in vielen Teams eine kleine GitHub
Action, die auf Pushes nach
flux/**reagiert. Alternativ übernimmt Renovate die Image-Updates ganz, dann bleibt Flux beim Ausrollen (siehe „Renovate zum Anfassen“). - Viele Updates auf einmal: Erscheinen mehrere Images
kurz hintereinander, wartet die Automation mit
spec.policyChangeDelay(neu in Flux 2.10) eine Weile und bündelt sie in einen Commit. - Signierte Commits:
spec.git.commit.signingKeysigniert die Commits des Bots mit GPG. Zusammen mit Branch-Schutz („nur signierte Commits“) weiß jeder, dass ein Commit wirklich von Flux stammt.