Adopting 54 Drifted Production Kubernetes Workloads into GitOps Without Downtime
Closing the kubectl diff and Argo CD blind spot for hand-edited live fields, solving the Kubernetes $(POD_IP) Secret-expansion trap, and enforcing a trigger-only CI-to-GitOps security boundary across 54 live workloads.
Greenfield GitOps tutorials assume your cluster starts empty and Git is already the source of truth. Migrating 54 live production workloads off a managed Kubernetes PaaS into self-managed Argo CD, OpenBao, and External Secrets is the opposite problem: roughly two-thirds of live releases had drifted from their stored Helm state through months of emergency kubectl edits, manual annotations, and inline command overrides.
During the migration we documented 70 real-world failure modes and wrote deterministic verification tools so all 54 production Applications landed Synced and Healthy with zero downtime and zero silent regressions. Four failure modes stood out.
1. The kubectl diff and Argo CD blind spot for live-only fields
How the blind spot works
Before syncing a brownfield Deployment with Argo CD, the usual safety check is running kubectl diff (or checking Argo CD’s diff view). An empty diff looks safe to adopt, but it is not. Both kubectl diff and Argo CD use three-way merge semantics driven by the kubectl.kubernetes.io/last-applied-configuration annotation. When a workload was previously managed by a PaaS or edited by hand without kubectl apply:
- Fields present only on the live object (extra volumes, volumeMounts, custom annotations, sidecar flags) do not show up in the diff.
- Once you sync and record
last-applied-configuration(or enable server-side/prune semantics), Argo CD treats its own manifest as authoritative and silently deletes those live-only fields.
We caught this in staging when an apparently clean sync stripped a hand-added nginx logging ConfigMap mount from a live pod.
Bidirectional structural comparison
We built check-live-only.py, a bidirectional live-vs-Git comparator that:
- Walks both the live Kubernetes object and the rendered Git manifest to report every live-only field, ignoring only cluster-managed noise (
resourceVersion,uid,generation, controller status). - Normalizes Kubernetes resource quantities semantically (
2000M == 2G,500m == 0.5) so formatting differences do not hide real drift. - Expands
envFromSecret keys (by key name only, never printing values) to verify that no environment variable disappears when migrating from inlineenv:toExternalSecret.
Every workload followed a strict 4-step adoption protocol:
1. strict check (live vs git: must be 0 unexpected diffs)
2. record last-applied annotation (no spec mutation)
3. Argo CD sync
4. strict check again (prove live object did not lose any field)
2. The Kubernetes $(POD_IP) Secret-expansion trap and inline command secrets
$(POD_IP) breaks when moved into a Secret
Several Django workloads configured DJANGO_ALLOWED_HOSTS with $(POD_IP) so kubelet HTTP liveness/readiness probes (which connect to the pod’s IP) would pass ALLOWED_HOSTS validation.
When we migrated environment variables from literal env: entries into OpenBao and ExternalSecret (envFrom), health probes immediately failed with 400 Bad Request. Kubernetes performs dependent environment-variable expansion ($(VAR_NAME)) only on literal env[].value strings. Values sourced via envFrom or valueFrom.secretKeyRef are injected verbatim, so the container received the literal 9-character string "$(POD_IP)" instead of its IP address.
25 hidden inline exec env secrets
Inspecting containers[].env was not enough to find all secrets. Across the fleet, 25 workloads had container startup commands of the form:
exec env DB_PASSWORD=... BROKER_URL=... gunicorn ...
These overrides were invisible to env:-only exporters and would have either leaked into Git inside command:/args: or silently overridden the new OpenBao-backed secrets.
Fail-closed export tooling
We built export-app.py with fail-closed guards:
- Refuses to export any workload containing
$(...)variable references until those specific keys are explicitly pinned with--literal=NAMEso they stay in literalenv:while all other secrets move toExternalSecret. - Parses and lifts
[exec] env KEY=VAL ...prefixes out of containercommand/argsinto OpenBao. - Scans rendered ConfigMaps and Helm values for leaked credentials (such as broker URLs with embedded passwords) before writing any file to the GitOps repo.
3. Reloader and Argo CD drift loops, plus the OpenBao HA Service trap
Two infrastructure-level loops only appeared once workloads were running under live secret rotation.
First, Stakater Reloader triggers rolling restarts when a Secret changes by mutating a hash annotation on the Deployment’s pod template. Argo CD immediately sees the mutated annotation as out-of-sync drift and reverts it (or re-triggers syncs). We fixed this by configuring Reloader’s annotations strategy alongside targeted ignoreDifferences on the Reloader-owned annotation path so secret reloads do not fight Argo CD reconciliation.
Second, our SecretStore and nightly snapshot jobs initially pointed at OpenBao’s default Kubernetes Service, which load-balances across all pods, including unready and standby replicas. During pod restarts or leader elections, 50% of External Secrets logins failed. Pointing both SecretStore and backup jobs exclusively at OpenBao’s leader-only openbao-active Service over TLS (verified against our internal CA) eliminated those failures.
4. Replacing repo-wide CI push tokens with a trigger-only allowlist
The default GitOps CI pattern gives every application repository a write token to push image-tag bumps directly to the GitOps repository. Across 54 workloads, a compromised or misconfigured staging pipeline in any repo holds write access to every production manifest.
We replaced shared push tokens with a two-sided least-privilege boundary:
App Repo CI (untrusted)
│ trigger-only token (cannot push commits or read other apps)
▼
GitOps Repo Pipeline (trusted)
├─ validates (source_project, source_branch, target_namespace, target_app)
│ against a code-reviewed allowlist in Git
├─ updates only that app's image tag
└─ commits using an internal bot token scoped solely to the GitOps repo
Application pipelines can request a deployment only for the exact (namespace, app) pairs mapped to their project and branch in the reviewed allowlist. No application repo holds Git write credentials to cluster state.
Takeaways
- Do not trust a clean
kubectl diffduring brownfield GitOps adoption. Compare the full live object structurally against your rendered Git manifest beforelast-applied-configurationis written. - Kubernetes only expands
$(POD_IP)in literalenv[].valuefields, never inenvFromorsecretKeyRef. Keep downward-API-dependent variables literal and fail closed in migration tooling. - Point Vault/OpenBao
SecretStores at the leader-only-activeService in HA clusters, and keep GitOps write credentials out of application CI pipelines.
Comments