Calling kubectl Safely from an Agent

kubectl is a sharp tool. The wrapper that whitelists verbs, classifies blast radius, and refuses anything outside scope. With the test suite that proves it.

Verb allowlist

The verb allowlist is the first safety primitive. Read-only verbs allow in any context; mutating verbs allow conditionally with controlled scope; dangerous verbs deny outright because they are too risky for unattended agent use.

Scope by namespace and resource

Scope narrows what each verb can touch. The wrapper restricts namespace (production requires extra approval), restricts resource type (the agent lists pods but cannot modify CRDs), restricts label selectors (anything in dev is fair game; production requires app=approved-set).

Blast-radius classification

Each operation has a blast-radius classification. get/describe is blast 0 (no state change); logs is blast 0 in normal operation but can be high on huge log lines (cost); scale 1 to 3 on a Deployment is blast contained to the deployment; rollout undo is contained but irreversible without effort.

Test suite for the wrapper

The wrapper needs a test suite that covers happy path, deny path, and edge cases. Each allowed verb on each resource type passes; each denied verb fails with a clear error; each conditional passes with the right scope and fails without; edge cases (empty namespace, malformed selectors, very long outputs) are covered explicitly.

Observability of kubectl calls

Every kubectl call from the agent is observed. Logged with agent run id, verb, resource, namespace, exit code, output size; aggregate dashboards show calls per agent role, per verb, and error rates; outlier alerts fire on a sudden spike in a normally-quiet verb because that is the kind of anomaly worth investigating.