Our deploys to Kubernetes run on a self hosted runner, a long lived virtual machine inside our network with an IAM role that can reach both of our EKS clusters, staging and production. The production job has always named its cluster explicitly. The staging job never did. It ran kubectl apply -k overlays/staging and relied on the current context in the runner's kubeconfig, which had pointed at staging since the day the machine was built.

In September a colleague added a nightly drift report to the same runner. It ran aws eks update-kubeconfig --name prod-eu, then kubectl diff against the production manifests, and posted the result in our channel. It was a read only job and it was reviewed as one.

At twelve minutes past nine the next morning a developer merged a change to the label service and the staging deploy ran. Twenty six minutes later our Wakefield depot rang to say every label coming off the printers was a carrier test label that their scanners refused.

update-kubeconfig does two things. It writes the cluster into ~/.kube/config, and it makes that cluster the current context. The file lives in the home directory of the runner's user, so it is not cleaned up between jobs the way the workspace is. The drift report had left the runner pointed at production, and the staging deploy applied the staging overlay there. Both clusters use the same namespace names, so nothing failed. Production's label service restarted with staging's configuration, carrier sandbox endpoints included, and printed sandbox labels until we redeployed the production overlay.

Nothing in the staging job was wrong on the day it was written. It depended on a piece of state outside the job, and another job was free to change it.

Every job now sets KUBECONFIG to a file in RUNNER_TEMP, which the runner empties at the start and end of each job, and writes its own cluster into it. Every kubectl call passes --context. Before applying anything, the deploy step compares the cluster ARN in its context with the one the environment expects and stops if they differ. Production deploys moved to a separate runner whose role cannot see staging, and staging's role cannot see production. The runners are now ephemeral: each one takes a single job and is replaced.

A job should carry everything it relies on. Anything it finds lying around was left there by somebody else's job.

– Sergey Shinder