Workflow: Git Is the Only Way In

How Flux Works

Two kinds of objects do all the work:

  • GitRepository - where to read from. flux-system points at your fork, branch main, 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 develop every 5 minutes). Every object it creates gets the label kustomize.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 Kustomization is 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-production says: apply ./flux/apps/backend/production every 10 minutes.
  • a kustomization.yaml is 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 production kustomization.yaml takes the backend from ../base unchanged - the 256Mi memory 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:

  1. Apply - every object in the folder is created or updated to match Git.
  2. Undo drift - a field changed by hand is set back to the value in Git. Changes made with kubectl are removed even when they only add something, such as an annotation.
  3. 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 annotation kustomize.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 CloudNativePG Cluster objects, 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 SUSPENDED column, not only READY;
  • 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) or deleted; a deleted is 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.

  1. Find the owner. kubectl --context kind-sre-control-plane get <object> -o jsonpath='{.metadata.labels.kustomize\.toolkit\.fluxcd\.io/name}' names the Kustomization; its spec.path names the folder, and the kustomization.yaml there names the file that sets the value.
  2. Change the folder in Git, never the object.
  3. Preview. flux --context kind-sre-control-plane diff kustomization <name> --path <folder> for every environment the change reaches. Read every deleted; an exit code above 1 means the preview failed, not that it is clean.
  4. Merge through a pull request, then watch it arrive: flux --context kind-sre-control-plane reconcile kustomization <name> --with-source.
  5. Prove it. flux --context kind-sre-control-plane get kustomizations -A - every row READY 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.

  1. A fix to an object Flux manages goes into Git, through a pull request - never kubectl edit, patch or apply on that object. Enforced by: on Hetzner, the OIDC roles are read-only in staging and production; in flux-system too, except that admins may change spec.suspend and 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). In develop and 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.
  2. Never flux suspend on 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 the safeops-course:admins group may suspend, resume or reconcile; an agent signed in as an admin can, so for it this is an instruction. On kind: instruction only.
  3. Never report the cluster as healthy from READY alone - read the SUSPENDED column too. Enforced by: make smoke-test fails on a suspended Kustomization.
  4. Before proposing a change to a shared base, run flux diff kustomization for every environment it feeds and report every deleted. An exit code above 1 means 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.
  5. 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: false keeps apply and drift correction running, but objects removed from Git keep running in the cluster;
    • changing spec.path or the source changes what Flux applies - another folder or another repository. Enforced by: on Hetzner the admission policy oidc-flux-operator-fields lets admins change only spec.suspend and 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.