How does Flux detect configuration drift in Kubernetes clusters, and what remediation strategies are available for production environments?
Quick Answer
Flux detects drift by periodically comparing the desired state from Git sources against the live state in the cluster. When differences are found, Flux can automatically remediate by reapplying the desired configuration, with options to force overwrite manual changes, prune orphaned resources, or alert operators before taking action.
Detailed Answer
Configuration drift occurs when the live state of resources in a Kubernetes cluster diverges from the desired state declared in Git. This can happen through manual kubectl edits, other controllers modifying resources, admission webhooks mutating objects, or even operator reconciliation loops that adjust fields Flux also manages. Flux addresses drift through a continuous reconciliation model where controllers periodically fetch the desired state from sources, render manifests, compare them against live resources using server-side apply semantics, and reapply any differences. Understanding the nuances of this process is essential for operating Flux in production.
The kustomize-controller is the primary drift detection mechanism for non-Helm workloads. At each reconciliation interval defined by spec.interval, it fetches the latest artifact from the source, runs kustomize build to produce the desired manifests, and applies them to the cluster using server-side apply. Server-side apply tracks field ownership, meaning Flux owns the fields it manages while other controllers can own different fields on the same resource. This prevents conflicts where Flux would overwrite fields managed by HPA for replica counts or by cert-manager for TLS certificates. The spec.force field can override this behavior, causing Flux to take ownership of all fields regardless of current ownership, which is useful for enforcing strict GitOps compliance but dangerous if other controllers legitimately manage certain fields.
For HelmRelease resources, the helm-controller provides drift detection through the spec.driftDetection field introduced in Flux v2. This feature performs a three-way diff between the last applied Helm values, the live state, and the desired state. When drift is detected, it can be configured to log the differences, emit events, or automatically correct the drift by reapplying the Helm release. The mode can be set to enabled for full detection or warn for observation-only mode. This is particularly powerful because Helm releases often contain dozens of resources, and manually tracking drift across all of them would be impractical.
Pruning is a critical aspect of drift remediation that handles the inverse problem: resources that exist in the cluster but have been removed from Git. When spec.prune is set to true on a Kustomization, Flux will delete resources that were previously applied but are no longer present in the desired state. Flux tracks which resources it manages using labels, specifically the kustomize.toolkit.fluxcd.io/name and kustomize.toolkit.fluxcd.io/namespace labels. Resources without these labels are never pruned, providing a safety net against accidentally deleting resources that Flux did not create. However, pruning can still be dangerous if a Git repository restructuring accidentally removes manifests, which is why many teams use spec.prune: false in production and rely on alerts to notify them of orphaned resources.
Production drift management requires a layered strategy combining automated remediation with observability and safeguards. Teams should configure Flux notification-controller to send alerts to Slack or PagerDuty whenever drift is detected and corrected, giving operators visibility into unauthorized changes. The spec.retryInterval field controls how quickly Flux retries after a failed reconciliation, preventing thundering herds when cluster issues cause widespread failures. Organizations with compliance requirements often implement a two-tier approach: automated drift correction for infrastructure components like RBAC and NetworkPolicies where unauthorized changes are security risks, and alert-only drift detection for application workloads where developers might legitimately need to make temporary manual adjustments during incident response. Audit logging of all drift events provides the compliance trail needed for SOC 2 and similar frameworks.