Workflow: Git Is the Only Way In
How Flux Works
Two kinds of objects do all the work:
- GitRepository - where to read from.
flux-systempoints at your fork, branchmain, and fetches it every minute. - Kustomization - what to apply. Each one takes one folder of that repository, builds it with Kustomize, and applies it to the cluster on its own interval (the backend in
developevery 5 minutes). Every object it creates gets the labelkustomize.toolkit.fluxcd.io/name=<kustomization>, so you can always find the owner.
Two things share the name Kustomization, and the chapter uses both:
- a Flux
Kustomizationis an object in the cluster (kubectl -n flux-system get kustomizations). It says which folder to apply, from which source, how often, and whether to prune.backend-productionsays: apply./flux/apps/backend/productionevery 10 minutes. - a
kustomization.yamlis a file in that folder, read by the Kustomize tool. It says how to assemble the manifests: which files to include (resources), which shared folder to build on (../base), and what to change for this environment (patches). The productionkustomization.yamltakes the backend from../baseunchanged - the256Mimemory limit, the line from the incident, is in the base.
So the Flux object points at a folder; the file in that folder builds what gets applied. A shared base is why one line there can reach every environment.
GitRepository and the root Kustomization
Show gotk-sync.yaml
---
apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
name: flux-system
namespace: flux-system
spec:
interval: 1m0s
ref:
branch: main
url: https://github.com/safeops-course/sre.git
---
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: flux-system
namespace: flux-system
spec:
interval: 10m0s
retryInterval: 2m0s
path: ./flux/bootstrap/flux-system
prune: true
sourceRef:
kind: GitRepository
name: flux-system
Each time a Kustomization reconciles, three things happen:
- Apply - every object in the folder is created or updated to match Git.
- Undo drift - a field changed by hand is set back to the value in Git. Changes made with
kubectlare removed even when they only add something, such as an annotation. - Prune - with
prune: true, an object that Flux created earlier and that is no longer in Git is deleted. A single object can opt out with the annotationkustomize.toolkit.fluxcd.io/prune: Disabled: the environment namespaces carry it, so removing one from Git never deletes the Namespace - and with it everything inside. The objects inside are still pruned by their own Kustomizations when they are removed from Git - except the three CloudNativePGClusterobjects, which carry it too: a database that leaves Git stays, with its data. Deleting one on purpose is a separate step after its manifest is gone from Git - while it is still there, Flux would recreate it, empty.
Deleting an object by hand does not delete it for long: the next reconcile creates it again. Removing it from Git does.
Steps 2 and 3 are the reason to run Flux, not a side effect of it. Undoing drift means the cluster cannot slowly turn into something nobody reviewed - a forgotten debug setting, a script’s edit, an attacker’s change are all set back. Re-creating what was deleted means a mistaken kubectl delete heals within minutes. Prune means that what you remove in Git is really gone from the cluster, instead of lingering forgotten - except an object that opts out, such as the environment namespaces and the databases. The price is the one the incident paid for: a change that is not in Git does not last.
Guardrails That Stop It
Git is the only way in (team). A change that is not in Git is gone at the next reconcile - for every object an active Kustomization manages, Flux restores what Git declares. (Single objects can opt out with Flux annotations; on this platform only the namespaces and the CloudNativePG Cluster objects do, and only from prune.) What gets into Git is decided by review and CI (Chapter 05), not by Flux. During an incident the fastest lasting fix is a one-line pull request. A hand change is at best a bridge of a few minutes, and only if you open the pull request at the same time.
Suspend is an off-switch, and it looks like Ready (team, cluster). flux suspend stops a Kustomization from applying anything - hand changes stay, and new commits are ignored. Its READY column keeps the last result, so it looks healthy. Rules:
- suspend only with a record: who, why, and when it will be resumed;
- every baseline check reads the
SUSPENDEDcolumn, not onlyREADY; - resume as soon as the fix is in Git.
Preview what a merge will do (workstation, review). flux diff kustomization builds a folder from your working copy and compares it with the cluster. Run it from the root of the repository: --path is relative to where you are. It is the preview for GitOps, like terraform plan in Chapter 02 - with one difference: a saved Terraform plan is exactly what gets applied, while flux diff is only a look. What Flux applies after the merge is whatever is in Git then, so run it again if main moved.
flux --context kind-sre-control-plane diff kustomization network-policies-develop -n flux-system \
--path ./flux/infrastructure/network-policies/develop
- no output and exit code
0- nothing would change; - exit code
1- merging this would change the cluster. Each►line names one object to review -created,drifted(changed) ordeleted; adeletedis a prune; - an error and an exit code above
1- the diff itself failed (a wrong Kustomization name or--path), so it tells you nothing about the change. A script that treats any non-zero code as “changes found” misreads this one.
Folders share a base. A line removed from flux/infrastructure/network-policies/base deletes the policy in develop, staging and production - run the diff for each environment the base feeds.
Roll back with Git (team). Going back is git revert of the commit that caused it; Flux applies the revert like any other change. Chapter 10 practises it on a real promotion.
The One Place Where Flux Decrypts
Most Kustomizations apply plain manifests. One is different: secrets-local has a decryption field, so Flux decrypts SOPS-encrypted files with the key Chapter 00 put into the cluster as sops-age. Its folder, flux/secrets/local/, is empty for now - Chapter 04 puts your first encrypted secret there.
secrets-local Kustomization
Show secrets-local.yaml
---
# The learner's own SOPS-encrypted secrets (secrets chapter). Starts empty; the
# sops-age key is generated by infra/terraform/kind_cluster on first apply and
# scripts/sops-setup.sh registers its public half in .sops.yaml.
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: secrets-local
namespace: flux-system
spec:
interval: 10m0s
retryInterval: 2m0s
sourceRef:
kind: GitRepository
name: flux-system
path: ./flux/secrets/local
prune: true
wait: false
decryption:
provider: sops
secretRef:
name: sops-age
dependsOn:
- name: infrastructure
Safe Workflow (Step-by-Step)
Every command names its cluster with --context (Chapter 01); on your lab cluster that is kind-sre-control-plane.
- Find the owner.
kubectl --context kind-sre-control-plane get <object> -o jsonpath='{.metadata.labels.kustomize\.toolkit\.fluxcd\.io/name}'names the Kustomization; itsspec.pathnames the folder, and thekustomization.yamlthere names the file that sets the value. - Change the folder in Git, never the object.
- Preview.
flux --context kind-sre-control-plane diff kustomization <name> --path <folder>for every environment the change reaches. Read everydeleted; an exit code above1means the preview failed, not that it is clean. - Merge through a pull request, then watch it arrive:
flux --context kind-sre-control-plane reconcile kustomization <name> --with-source. - Prove it.
flux --context kind-sre-control-plane get kustomizations -A- every rowREADY True,SUSPENDED False, on the latest revision - and then the application’s own signals: the delivery arrived, but only the application says it works (Chapter 01, rule IV).
Instructions for Your AI Agent
The rules this chapter adds to the list from Chapters 01 and 02. Most of them protect the loop that keeps the cluster equal to Git - from the shortcut the incident took.
- A fix to an object Flux manages goes into Git, through a pull request - never
kubectl edit,patchorapplyon that object. Enforced by: on Hetzner, the OIDC roles are read-only instagingandproduction; influx-systemtoo, except that admins may changespec.suspendand request a reconcile - nothing else - and no OIDC user may create, change or delete any other Flux object, in any namespace (oidc-flux-objects-from-git). Indevelopand on kind a hand change is allowed, and Flux sets it back at the next reconcile - that undoes it, it does not stop it: instruction only. - Never
flux suspendon your own. If a suspend looks necessary, stop and report: who would suspend what, why, and when it is resumed. Enforced by: on Hetzner only thesafeops-course:adminsgroup may suspend, resume or reconcile; an agent signed in as an admin can, so for it this is an instruction. On kind: instruction only. - Never report the cluster as healthy from
READYalone - read theSUSPENDEDcolumn too. Enforced by:make smoke-testfails on a suspended Kustomization. - Before proposing a change to a shared
base, runflux diff kustomizationfor every environment it feeds and report everydeleted. An exit code above1means the preview failed, not that nothing changes. Enforced by: instruction only. The Flux Diff CI job builds and validates the manifests; it does not compare them with a cluster. - Never change how Flux manages an object to get around it. None of these switches Flux off - each does something else:
- removing its labels only hides the owner: the object is still in Git, and the next reconcile sets the labels back;
prune: falsekeeps apply and drift correction running, but objects removed from Git keep running in the cluster;- changing
spec.pathor the source changes what Flux applies - another folder or another repository. Enforced by: on Hetzner the admission policyoidc-flux-operator-fieldslets admins change onlyspec.suspendand the annotations that request a reconcile, and GitRepositories are read-only. In Git: the review of the pull request. On kind: instruction only.
The full list, chapter by chapter, is docs/agent-rules.md in your fork of the platform repository.
This builds on: Infrastructure as Code (Chapter 02) - Terraform built the cluster and installed Flux; from here on, Git drives everything else.
This enables: Secrets Management (Chapter 04) - secrets travel through the same Git path, encrypted, and secrets-local decrypts them.