Deploy Window Operator

reading settings
font:
size:
spacing:

A Kubernetes operator that only lets selected Deployments roll out during allowed time windows, and pauses them the rest of the time.

new ReplicaSets when an image changes outside the window
0
test coverage of the schedule logic
93%
controller coverage, tested against a real API server
77%

The problem

Many teams have a rule like "no production deploys on Friday evening or at the weekend". Usually it lives in a wiki page, and nothing stops a late kubectl, Argo CD sync or CI job from breaking it.

What changed

  • Enforced deploy windows in the cluster, whatever tool changes the Deployment. In the demo, changing the image while the window was closed created no new ReplicaSet, and the rollout ran once the window was suspended.
  • Resumed only what the operator itself paused. A Deployment someone paused by hand has no annotation, so the operator leaves it alone.
  • Avoided polling. The controller works out when the window next opens or closes and requeues for exactly that moment.

What I worked on

  • I built the operator in Go with Kubebuilder and controller-runtime, around a DeploymentWindow custom resource with a label selector, a time zone and a list of windows.
  • I kept the schedule logic as a pure function with no Kubernetes code, covered by table tests for time zones, back-to-back windows, the weekend wait and invalid input.
  • I tested the controller with envtest against a real API server and a fake clock, covering pausing, resuming, suspend, a Deployment leaving the selector, deletion and invalid specs.

A DeploymentWindow names the Deployments it covers and the hours they may roll out. Outside those hours the operator sets spec.paused: true on each one, so a new image or config change waits until the window opens.

apiVersion: policy.deploywindow.dev/v1alpha1
kind: DeploymentWindow
metadata:
  name: business-hours
spec:
  selector:
    matchLabels:
      tier: prod
  timeZone: Africa/Lagos
  windows:
    - days: [Mon, Tue, Wed, Thu, Fri]
      start: "09:00"
      end: "17:00"
  suspend: false # true releases everything, for an emergency fix

The reconcile loop

Each pass works out whether the window is open now and when that next changes. If it's closed, the operator pauses every matching Deployment and marks it with a deploywindow.dev/paused-by annotation. If it's open or suspended, it resumes only the Deployments carrying that annotation. Then it writes the status and requeues for the next boundary.

The controller also watches Deployments, so a new or relabelled one is handled at once. A finalizer makes sure that deleting a window resumes everything it paused before the object goes away. An invalid spec, such as an unknown time zone, sets Ready=False and leaves nothing paused.

The demo

A script runs the whole story on a kind cluster: a closed window pauses a tier=prod Deployment, an image change creates no new ReplicaSet, suspend: true lets the rollout finish, suspend: false pauses it again, and deleting the window resumes it.

Limits

It covers Deployments only, because StatefulSets and DaemonSets have no paused field. A window applies to one namespace and can't cross midnight, so 22:00–02:00 needs two windows. A rollout already in progress when the window closes stops where it is and finishes when the window opens.