Kube Context Guard

Shows the cluster, context and namespace a kubectl or helm command will really hit, before it runs, so production isn't changed by accident.

StopsHelps prevent kubectl apply on the wrong cluster

Installation

pnpm dlx shadcn@latest add https://bollard-ui.vercel.app/r/kube-context-guard.json

The files land in your project. They're yours to edit.

Usage

kube-context-guard-usage.tsx
import { KubeContextGuard } from "@/components/ui/kube-context-guard"

<KubeContextGuard
  // Someone ran `kubectl config use-context prod-eu` in another terminal since the last command.
  context={CONTEXTS[1]}
  contexts={CONTEXTS}
  lastRun={{ context: "staging-eu", namespace: "payments" }}
  defaultValue="kubectl apply -f payments-api.yaml"
  manifests={MANIFEST}
  expectedNamespace="payments"
  delay={0}
  onRun={(command) => new Promise<void>((resolve) => setTimeout(() => (setRan(command), resolve()), 700))}
/>

What it catches

  • Production contexts, read word by word so pre-prod and nonprod aren't
  • Deploys into the default namespace
  • Manifest namespace that disagrees with -n (helm splits it silently)
  • Context switched since the last command
  • Fixes such as adding a server-side dry run

Props

KubeContextGuard props
PropTypeDefault
context* (required)

The current kubeconfig context.

KubeContext—
contexts

Other contexts, so a --context flag can be shown with its cluster and namespace.

KubeContext[]—
value

Controlled command.

string—
defaultValue

Initial command when uncontrolled.

string""
onValueChange

Called with the command on every change, including one-tap fixes.

(command: string) => void—
manifests

Objects being applied, or their YAML.

ManifestObject[] | string—
lastRun

Where the previous command ran, to catch a context switched in between.

{ context: string; namespace?: string }—
expectedNamespace

Namespace to suggest instead of "default".

string—
isProduction

Your own production test. Defaults to reading context and cluster names word by word.

(context: KubeContext) => boolean—
confirmProduction

Ask for the context name before a production change.

booleantrue
delay

Pause in ms before warnings refresh while typing.

number400
onRun

Called on Run. May return a Promise; shows a pending state and ignores repeat clicks.

(command: string) => void | Promise<void>—
messages

Any visible string.

Partial<KubeContextMessages>—

* required. Also accepts the native props of <div> (className, aria-*, data-*…), passed through to it.

Accessibility

  • Command and confirmation inputs have visible labels
  • Command is described by the target readback and the issues
  • Status uses role=status; warnings are text with aria-hidden icons
  • Focus returns to a nearby fix or the input after a fix

Only what the source does. Test with your own screen reader and content before shipping.

Limitations

This is a UX guard rail. Enforce on the server: Restrict production with RBAC and admission control; never rely on a client-side prompt.

  • Works from the contexts you pass; it doesn't read your kubeconfig
  • Command parsing covers common kubectl/helm forms, not every flag or plugin
  • Typing the context name is friction, not authorization

What it stops

Helps prevent kubectl apply on the wrong cluster

Also known as

kubectl · helm · kube context · namespace · eks · gke · kubernetes · prod cluster

Field type

Dependencies

Pairs well with