How googlecloudplatform/kubectl-ai works
Purpose and Entry Points
kubectl-ai is a Go CLI/kubectl plugin that turns natural-language requests into Kubernetes operations through an LLM agent. Primary startup paths are the normal CLI in cmd/main.go and MCP-server mode in cmd/mcp.go. It accepts interactive terminal input, piped/one-shot prompts, or an HTML UI.
Application Composition
The agent is assembled around conversation state, an LLM client, tools, UI, and optional persistence. pkg/agent/manager.go coordinates request execution; pkg/agent/conversation.go maintains multi-turn context; pkg/api/models.go supplies shared API models. The agent iterates up to configured limits, letting model tool calls drive execution and returning streamed/final output to the UI.
LLM Provider Boundary
The nested gollm Go module abstracts model-specific APIs behind common interfaces and a provider factory: gollm/interfaces.go, gollm/factory.go, gollm/schema.go. Implementations support Gemini, OpenAI-compatible/Azure OpenAI, Anthropic, Grok, Bedrock, Ollama, and llama.cpp, separating provider authentication/protocols from agent orchestration. Journal/persistence facilities support recording and replayable LLM HTTP/context data in gollm/http_journal.go and gollm/persist.go.
Tool Execution and Kubernetes Safety Boundary
Tools exposed to the model are defined and assembled in pkg/tools/interfaces.go, pkg/tools/tools.go, and pkg/tools/kubectl_tool.go. Built-ins include kubectl and bash; YAML-defined custom tools and MCP-backed tools extend the same tool surface. Kubernetes commands can be filtered and routed through permission/sandbox controls, with execution backends in pkg/sandbox/executor.go, pkg/sandbox/kubernetes.go, and pkg/sandbox/local.go. This is the critical boundary between generated model instructions and cluster/local side effects.
MCP Integration Hub
In client mode, configured external MCP servers are discovered and translated into callable tools via pkg/mcp/manager.go, pkg/mcp/stdio_client.go, and pkg/mcp/http_client.go; the agent consumes them through pkg/agent/mcp_client.go and pkg/tools/mcp_tool.go. In server mode, cmd/mcp.go exposes native tools, optionally aggregates those external tools, and supports stdio or streamable HTTP transport. This makes the binary both an MCP tool consumer and provider.
State, Interaction, and Observability
Session storage is abstracted by pkg/sessions/store.go and managed through pkg/sessions/manager.go, with memory and filesystem implementations in pkg/sessions/memory.go and pkg/sessions/filesystem.go. UI adapters in pkg/ui/terminal.go, pkg/ui/tui.go, and pkg/ui/html/htmlui.go present the same agent flow through terminal, TUI, or web runtime. Execution/context logging is handled by pkg/journal/recorder.go and pkg/journal/loader.go.
Deployment and External Dependencies
The runtime needs a configured kubeconfig/kubectl and credentials for the selected LLM provider. Container packaging is images/kubectl-ai/Dockerfile; Kubernetes deployment manifests are k8s/kubectl-ai.yaml and k8s/kubectl-ai-gke.yaml. Local or cluster-hosted llama.cpp serving is supplied separately under modelserving/k8s/llm-server.yaml and modelserving/images/llamacpp-server/Dockerfile. Releases are built through GoReleaser (.goreleaser.yaml) and distributed as a standalone binary/Krew plugin.
Main entry points: agent/manager.go Agent manager · mcp/manager.go MCP client manager · tools.go Tool registry · factory.go LLM factory & interfaces
Components 19
Entry & Interaction
- CLI entrypoint · Normal kubectl plugin and CLI startup.
cmd/main.go - MCP server mode · Exposes native and aggregated tools over MCP.
cmd/mcp.go - Terminal, TUI & HTML UI · Presents interactive, piped, and web agent flows.
pkg/ui/terminal.go
Agent Core
- Agent manager · Runs bounded LLM/tool iterations and streams results.
pkg/agent/manager.go - Conversation state · Maintains multi-turn request context.
pkg/agent/conversation.go - Shared API models
pkg/api/models.go
LLM Boundary
- LLM factory & interfaces · Selects a common client for configured providers.
gollm/factory.go - LLM providers · Gemini, OpenAI-compatible, Anthropic, Grok, Bedrock, Ollama, or llama.cpp.
gollm/interfaces.go - LLM HTTP persistence · Records and replays LLM HTTP and context data.
gollm/http_journal.go
Tools & Safety
- Tool registry · Assembles built-in, custom YAML, and MCP tools.
pkg/tools/tools.go - kubectl & bash tools · Turns model tool calls into Kubernetes or local commands.
pkg/tools/kubectl_tool.go - Permissioned execution · Filters and routes side-effecting commands to execution backends.
pkg/sandbox/executor.go - Kubernetes cluster · Reached through kubeconfig and kubectl.
pkg/sandbox/kubernetes.go - Local execution
pkg/sandbox/local.go
MCP Hub
- MCP client manager · Discovers external MCP tools over stdio or HTTP.
pkg/mcp/manager.go - External MCP servers
pkg/mcp/stdio_client.go
State & Observability
- Session manager & store · Persists sessions in memory or filesystem storage.
pkg/sessions/manager.go - Execution journal · Records and loads execution and context logs.
pkg/journal/recorder.go
Other components
- Container & Kubernetes packaging · Docker image and Kubernetes deployment manifests.
images/kubectl-ai/Dockerfile
Connections 21
- CLI entrypoint → Terminal, TUI & HTML UI: selects interaction
- Terminal, TUI & HTML UI → Agent manager: submits prompts
- CLI entrypoint → Agent manager: one-shot or piped prompt
- Agent manager → Conversation state: reads and updates
- Agent manager → Shared API models: uses
- Agent manager → LLM factory & interfaces: invokes model
- LLM factory & interfaces → LLM providers: provider protocol
- LLM factory & interfaces → LLM HTTP persistence: records/replays
- Agent manager → Tool registry: executes tool calls
- Tool registry → kubectl & bash tools: native tools
- kubectl & bash tools → Permissioned execution: command request
- Permissioned execution → Kubernetes cluster: approved cluster commands
- Permissioned execution → Local execution: approved local commands
- MCP client manager → External MCP servers: stdio or HTTP
- MCP client manager → Tool registry: registers discovered tools
- Agent manager → MCP client manager: MCP tool client
- MCP server mode → Tool registry: exposes native tools
- MCP server mode → MCP client manager: optionally aggregates
- Agent manager → Session manager & store: loads and saves session
- Agent manager → Execution journal: records execution
- Container & Kubernetes packaging → CLI entrypoint: packages runtime