How k8sgpt-ai/k8sgpt works
Project Type and Purpose
• This is a Go-based CLI and server application called k8sgpt for scanning Kubernetes clusters, diagnosing issues, and generating human-readable recommendations using AI backends.
• It also includes a Kubernetes operator/monitoring mode (via Helm chart under charts/k8sgpt), and supports both local (CLI) and gRPC ("serve") modes.
File Structure Analysis
• cmd/: Cobra-based CLI commands (analyze, auth, cache, filters, serve, etc.)
• pkg/server/: gRPC/HTTP server implementation for MCP mode (client handlers, config, query, mcp).
• pkg/analyzer/ and pkg/analysis/: Core analyzer engine that introspects k8s resources (pods, deployments, CRDs) and orchestrates analysis.
• pkg/ai/: Adapter/factory for multiple AI backends (OpenAI, Azure, Cohere, Bedrock, custom REST, local models).
• pkg/cache/: Caching layer with local file, S3, Azure Blob, GCS, Interplex implementations.
• chart/: Helm chart for deploying the operator/service inside a Kubernetes cluster.
• container/: Dockerfile and Grafana dashboard for in-cluster monitoring.
Key Architectural Patterns
• Modular plugin architecture: Each analyzer implements a common interface and can be toggled on/off or extended via custom analyzers (Protobuf schema).
• Factory pattern for AI backends: pkg/ai/factory.go builds provider instances.
• CLI ↔ Server duality: The CLI (main.go + cmd/*) can call analyzers directly or start a gRPC server (serve command) for external clients (e.g., Claude Desktop).
• Dependency inversion: High-level analysis logic depends on abstractions (interfaces for AI, cache, integrations), not concrete implementations.
Main Components & Interactions
A. User Interface (CLI / MCP gRPC Server)
• CLI invokes commands under cmd/; the “serve” command spins up the gRPC/HTTP server in pkg/server.
B. Core Engine
• Analyzer Manager (pkg/analysis + pkg/analyzer)
• Discovers enabled analyzers
• Fetches k8s objects via pkg/kubernetes (client-go wrapper)
• Runs each analyzer, collects intermediate findings
• Cache Layer (pkg/cache)
• Checks/updates cached analysis results
• Writes to local file or remote store (AWS S3, Azure Blob, GCS)
C. External Systems
• Kubernetes API Server
• LLM AI Backends (OpenAI, Azure, Cohere, Amazon Bedrock, local models, etc.)
• Remote Cache Services (S3, Azure Blob, GCS)
• Integrations (Prometheus, Keda, Kyverno, EKS, etc.)
D. Output & UI
• CLI prints plain text or JSON
• Grafana Dashboard (container/dashboards) for operator mode
• ServiceMonitor (charts/templates) for Prometheus scraping