GitOps makes Git the source of truth for what runs in the cluster: an agent syncs the desired state, so the cluster matches the repo. This skill covers the practice: repo layout, tool setup, promotion flows, and the security of the pipeline. It is engineering with security built in, since every change is reviewed and auditable.
Quick Workflow
- Structure the repo: one folder per environment, shared bases, and clear ownership.
- Choose the sync tool and install it with least privilege.
- Define the promotion flow: how a change moves dev to staging to production.
- Handle secrets: never in Git; use sealed secrets or external secret operators.
- Enable drift detection and alerting: manual cluster edits must surface.
- Require reviews on the Git side: the PR is the change control.
- Test the sync in a non production cluster first.
- Document rollback: revert the commit, the agent does the rest.
1. Repo structure and sync
# Layout: environments as folders, apps as subfolders
mkdir -p /tmp/gitops/{base,overlays/{dev,staging,prod}}/webapp
cat > /tmp/gitops/overlays/prod/kustomization.yaml << 'YAML'
resources:
- ../../base/webapp
images:
- name: webapp
newTag: "1.4.2" # promotion = bump the tag in a PR
YAML
# Argo CD app pointing at the environment folder (least privilege
# service account, sync only its namespace)
# argocd app create webapp-prod \
# --repo https://git.example/gitops.git \
# --path overlays/prod --dest-server https://kubernetes.default.svc \
# --dest-namespace prod --sync-policy automated
echo "check: sync status, drift alerts, and who can approve prod PRs"
2. Secrets and drift
# Secrets via external operator (example: External Secrets)
# The Git repo holds an ExternalSecret manifest; the operator fetches
# the value from the vault at sync time. No secret bytes in Git, ever.
grep -rn "password\|token" /tmp/gitops/ --include="*.yaml" | head -5
# Any hit is a finding; move it to the secret operator
# Drift detection: the agent reports OutOfSync when someone edits the
# cluster by hand. Alert on it; manual edits bypass review.
# argocd app get webapp-prod -o json | jq .status.sync.status
echo "policy: drift auto heals or pages, never silently persists"
Edge cases
- Emergency fix needed faster than the pipeline. Allow a documented break glass with automatic revert to Git state after.
- Multiple clusters and regions. One repo with per cluster overlays, or one repo per cluster; pick and stay consistent.
- The sync tool itself needs admin. Scope its service account to the namespaces it manages, not cluster admin.
Escalation
- Sync stuck or failing: check the agent logs and the repo permissions before touching the cluster.
- Secret sprawl already in Git: rotate every exposed secret, purge history per policy, then move to the operator.
Detection and defender view
GitOps is defensive by design: every change is a reviewed commit, and drift detection catches manual tampering. Misuse looks like direct cluster edits bypassing Git, or secrets committed to the repo. Monitor the sync status, alert on drift, and audit who can merge to the production path. The security win is auditability: the cluster's history is the Git log.
Key references
- Argo CD and Flux documentation.
- Related skills: git-workflows, devsecops-pipeline.
