How to Configure Pydantic AI Kubernetes Read Only Access

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up pydantic ai kubernetes read only access by binding a view role to your agent identity. Then you register safe query functions inside your agent tool list. Many teams start by giving an agent a broad cluster admin key for quick testing. If that agent misreads a prompt, a broad role lets it delete pods or change live settings. Setting up pydantic ai kubernetes read only access with RBAC To lock down your agent, you start with Kubernetes Role-Based Access Control. In Kubernetes, a Rol

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.

You set up pydantic ai kubernetes read only access by binding a view role to your agent identity. Then you register safe query functions inside your agent tool list. Many teams start by giving an agent a broad cluster admin key for quick testing. If that agent misreads a prompt, a broad role lets it delete pods or change live settings.

Setting up pydantic ai kubernetes read only access with RBAC

To lock down your agent, you start with Kubernetes Role-Based Access Control. In Kubernetes, a Role or ClusterRole holds rules that represent a set of permissions. These rules are purely additive, which means there are no deny rules in the system. A Role always sets permissions within one single namespace. A ClusterRole is not namespaced and can cover cluster-scoped resources like nodes. It can also cover namespaced resources across all namespaces in your cluster.

First, create a dedicated identity for your Pydantic AI app. Next, define a Role or ClusterRole that only allows read verbs on target resources. You should leave out write verbs so the agent cannot create, update, or delete objects. Be careful with subresources like pod logs or pod exec endpoints. Kubernetes RBAC refers to resources by the exact name in the API URL. A subresource uses a slash, such as the pod log path. Make sure your role grants access to pod logs but leaves out pod exec.

Once the role exists, you attach it to your agent identity. A RoleBinding grants permissions inside one specific namespace. It can reference a Role in that same namespace or a shared ClusterRole. If your agent must check workloads across the whole cluster, use a ClusterRoleBinding instead. A ClusterRoleBinding grants access cluster-wide.

Connecting Pydantic AI tools to your cluster

Pydantic AI lets you build agents that call Python functions as tools. Each tool uses schema validation to check the inputs before the function runs. For a read-only setup, you write small functions that call the Kubernetes client library. Each function should take clear inputs like a namespace name or a pod label. The function then runs a read request against the Kubernetes API and returns the result.

Your agent uses the restricted identity you bound in the previous step. When the model decides to check pod status, it fills out the tool schema. Pydantic AI checks the data types and runs your Python function. The Kubernetes API server checks the RBAC rules for that identity before returning any data.

Auditor Read-Only Access + Kubernetes API Server Access: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

What native RBAC solves and what keeps the session active

Native Kubernetes RBAC is essential for blocking write actions at the API server level. It decides which identity can log in and which resource types that identity can read. If an agent tries to delete a deployment, the API server blocks the call right away.

Still, static RBAC rules only check who is asking and which API endpoint they hit. They do not inspect the contents of the data coming back in flight. A read-only agent reading pod logs or config maps can still pull sensitive data. For example, app logs often contain user emails, phone numbers, card numbers, or national IDs. Static roles also require you to place credentials where the agent app can read them. And native role bindings do not link individual AI tool calls to user prompts in one audit view.

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 Kubernetes RBAC add a deny rule to block one specific secret?

No, Kubernetes RBAC permissions are purely additive and have no deny rules. To block access to a resource, you must leave it out of the allowed role rules.

Should I use a RoleBinding or a ClusterRoleBinding for a Pydantic AI agent?

Use a RoleBinding if your agent only needs to read workloads in one specific namespace. Use a ClusterRoleBinding only when the agent must inspect nodes or resources across all namespaces.

Why can a read-only agent still expose sensitive data from pod logs?

RBAC checks if the identity can read the pod log subresource, not what text is inside. If an app prints emails or card numbers to logs, the read call returns them unless masking rewrites matched fields in results.

The query runs. The emails stay hidden.