tadata
Back to home

GitOps Patterns: Git as the Single Source of Truth

#gitops#devops#kubernetes#ci-cd#infrastructure

GitOps is an operational framework that applies DevOps best practices for infrastructure automation -- using Git as the single source of truth for declarative infrastructure and applications.

Core Principles

  • Declarative: the entire system is described declaratively (YAML, HCL, JSON)
  • Versioned and immutable: Git provides the audit trail, rollback capability, and collaboration model
  • Pulled automatically: software agents pull the desired state from Git and apply it
  • Continuously reconciled: agents detect drift and correct it automatically

Push vs Pull Model

AspectPush Model (traditional CI/CD)Pull Model (GitOps)
Who deploysCI pipeline pushes to clusterAgent inside cluster pulls from Git
CredentialsCI needs cluster accessOnly agent needs cluster access
Drift detectionNone -- state can diverge silentlyContinuous reconciliation
SecurityBroader attack surfaceReduced -- no external cluster access needed
ToolsJenkins, GitHub Actions, GitLab CIArgoCD, Flux

The pull model is the GitOps standard because it reduces credential exposure and guarantees the cluster matches the Git state.

ArgoCD vs Flux

FeatureArgoCDFlux
UIRich web UI with visualizationCLI-first, optional Weave GitOps UI
ArchitectureCentralized, multi-cluster from one instanceDecentralized, agent per cluster
Multi-tenancyProjects and RBAC built-inKubernetes-native RBAC
Helm supportRenders Helm charts, stores manifestsNative Helm controller
KustomizeBuilt-in supportBuilt-in support
NotificationsBuilt-in notification engineNotification controller
CommunityCNCF graduated, larger communityCNCF graduated, strong ecosystem
Best forTeams wanting visibility and a UITeams preferring a lightweight, composable approach

Decision: ArgoCD for teams that value a visual dashboard and centralized management. Flux for teams that prefer Kubernetes-native primitives and decentralized operation.

Multi-Environment Promotion

A typical GitOps promotion flow:

dev/ --> staging/ --> production/
  |         |            |
  Git commit flow: PR-based promotion

Strategies:

  • Directory-based: separate directories per environment (environments/dev/, environments/prod/)
  • Branch-based: environment branches (main = prod, staging, dev) -- less recommended due to merge complexity
  • Overlay-based: Kustomize overlays with a shared base and environment-specific patches

Promotion mechanisms:

  • PR from dev config to staging config
  • Automated promotion after successful smoke tests
  • Image tag updates via image automation controllers (Flux) or image updater (ArgoCD)

Secret Management in GitOps

Secrets in Git repositories require special handling:

ApproachHow it worksTrade-offs
Sealed SecretsEncrypt secrets with cluster public key, only cluster can decryptSimple, but cluster-specific
SOPS (Mozilla)Encrypt with AWS KMS, GCP KMS, or PGP keysMulti-cloud, git-diff friendly
External Secrets OperatorSync secrets from Vault, AWS SM, etc. into K8sSecrets never in Git, but adds dependency
Vault Agent InjectorInject secrets at pod runtimeNo K8s secrets at all, but more complex

Recommendation: External Secrets Operator for most teams -- secrets stay in a dedicated vault, never in Git, and sync automatically.

Drift Detection and Reconciliation

GitOps agents continuously compare desired state (Git) with actual state (cluster):

  • Auto-sync: automatically apply changes when drift is detected
  • Manual sync: require human approval before applying (safer for production)
  • Sync windows: only allow syncs during maintenance windows
  • Diff visualization: ArgoCD shows exactly what will change before sync
  • Health checks: verify application health after sync, auto-rollback on failure

Resources

:::