How to Configure Pydantic AI Kubernetes Prevent Delete

Published
Length
3 min read

Luis Felipe Dias

All posts

To set up a pydantic ai kubernetes prevent delete workflow, restrict your agent role and add tool checks. You must omit delete verbs from Kubernetes RBAC rules and validate agent tool calls before they run. Many teams give AI agents broad cluster roles so they can fix broken pods fast. When an agent holds full permissions, one bad prompt can wipe out live workloads or namespaces. How a pydantic ai kubernetes prevent delete setup works A safe setup starts with Kubernetes role-based access cont

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

To set up a pydantic ai kubernetes prevent delete workflow, restrict your agent role and add tool checks. You must omit delete verbs from Kubernetes RBAC rules and validate agent tool calls before they run. Many teams give AI agents broad cluster roles so they can fix broken pods fast. When an agent holds full permissions, one bad prompt can wipe out live workloads or namespaces.

How a pydantic ai kubernetes prevent delete setup works

A safe setup starts with Kubernetes role-based access control, also called RBAC. In Kubernetes, permissions are purely additive, and there are no deny rules. A Role sets permissions inside one namespace, while a ClusterRole covers the whole cluster. A RoleBinding grants those namespace rules to a user or agent service account. A ClusterRoleBinding grants access across the entire cluster instead.

Because RBAC has no deny rules, you cannot write a rule that blocks delete alone. Instead, you must build a fresh Role from scratch for your agent. You list only safe verbs like get, list, watch, create, or patch on specific resources. If you leave the delete verb out of the Role, the API server blocks deletion requests.

Next, you lock down the tools inside your Pydantic AI agent code. Pydantic AI lets developers define strict schemas and functions that the model can call. You should split read actions and write actions into separate functions in your app. Never expose a generic function that runs raw shell commands or arbitrary API calls. Instead, build narrow functions that only fetch pod logs or scale a deployment.

Why native roles and schemas leave a runtime gap

Native RBAC and tool schemas stop basic mistakes, but they leave operational gaps at runtime. First, Kubernetes RBAC checks who makes the call, not the context of the request. Some agents need permission to remove stuck pods during an incident. Once you grant that verb in a Role, the agent can delete any pod in that namespace. RBAC cannot ask a human to approve a single pod deletion in flight.

AI Data Exfiltration Prevention + Kubernetes RBAC: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Second, subresources create hidden risks when agents debug live containers. RBAC names subresources with a slash, such as pod logs or pod exec endpoints. If an agent has permission to use the exec subresource, it can run commands inside a container. Even without a delete verb in RBAC, an exec command can still wipe files inside the app.

Third, pod logs and cluster objects often hold sensitive user data. When an agent reads logs to find a bug, it pulls raw text into the model prompt. Native Kubernetes roles do not rewrite emails, phone numbers, or card numbers in those log streams. Teams need a way to inspect every command and mask sensitive fields before data reaches the model.

Check Kubernetes API requests and responses on the wire

Kubernetes RBAC decides who may call the API server. RBAC does not check response payloads or split listing Secret names from reading Secret values when a role grants get access. With hoop.dev, you run an open-source HTTP proxy, a sidecar for your Kubernetes API. The kubectl client changes only the server address it talks to while RBAC stays in place.

On each call, hoop.dev guardrails read the method, normalized path, and headers to refuse bad deletes or full Secret reads with a 403 error. On the way back, hoop.dev data masking walks JSON responses by key and masks matched values in Secrets, ConfigMaps, and pod logs. Every request and verdict is logged without storing bearer tokens. View the project on GitHub.

Frequently asked questions

Can I write a Kubernetes RBAC deny rule to block agent deletes?

No, Kubernetes RBAC permissions are purely additive and do not support deny rules. To block deletes, you must create a Role that omits the delete verb and bind only that role.

How do I let a Pydantic AI agent read pod logs without deleting pods?

Grant the agent service account a Role that only allows get and list on pods and the pods log subresource. Then build a dedicated Pydantic AI tool that only calls the log read endpoint.

Why is the pods exec subresource risky for AI agents?

The exec subresource lets an agent run commands directly inside a running container. Even if RBAC blocks pod deletion, an agent with exec rights can delete data inside the container filesystem.

The query runs. The emails stay hidden.