# k8sgpt-ai/k8sgpt architecture

> Architecture diagram of the GitHub repository k8sgpt-ai/k8sgpt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/k8sgpt-ai/k8sgpt
- Repository: https://github.com/k8sgpt-ai/k8sgpt

## Overview

##### 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

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Layer 1: User Interface
    User["User"]:::external
    CLI["CLI (cmd/)"]:::external
    ServeCmd["serve Command (cmd/serve/)"]:::external
    gRPCSrv["gRPC Server (pkg/server/)"]:::core

    User -->|"invokes"| CLI
    User -->|"starts MCP mode"| ServeCmd
    ServeCmd -->|"launches"| gRPCSrv

    %% Layer 2: Core Engine
    subgraph "Core Engine"
        direction TB
        AM["Analyzer Manager"]:::core
        AO["Analysis Orchestrator"]:::core
        AP["Analyzers (pkg/analyzer)"]:::core
    end

    CLI -->|"direct analysis"| AM
    gRPCSrv -->|"gRPC request"| AM
    AM -->|"orchestrates"| AO
    AM -->|"runs"| AP

    %% Layer 3: Support Services
    K8sAPI["Kubernetes API Server"]:::external
    Cache["Cache Layer (pkg/cache)"]:::core
    Integrations["Integration Plugins"]:::optional

    AM -->|"Fetch Objects"| K8sAPI
    AM <-->|"Cache Read/Write"| Cache
    AM -.->|"optional"| Integrations

    %% Layer 4: External Backends
    subgraph "AI Backends & Remote Stores"
        direction TB
        AI_Factory["AI Backend Factory"]:::ai
        AI_Providers["Providers (pkg/ai/)"]:::ai
        RemoteCache["Remote Cache Stores"]:::external
    end

    AO -->|"Explain Request"| AI_Factory
    AI_Factory -->|"instantiates"| AI_Providers
    Cache -->|"store results"| RemoteCache

    %% Click Events
    click CLI "https://github.com/k8sgpt-ai/k8sgpt/tree/main/cmd/"
    click ServeCmd "https://github.com/k8sgpt-ai/k8sgpt/blob/main/cmd/serve/serve.go"
    click gRPCSrv "https://github.com/k8sgpt-ai/k8sgpt/blob/main/pkg/server/server.go"
    click AM "https://github.com/k8sgpt-ai/k8sgpt/blob/main/pkg/analysis/analysis.go"
    click AO "https://github.com/k8sgpt-ai/k8sgpt/blob/main/pkg/analysis/analysis.go"
    click AP "https://github.com/k8sgpt-ai/k8sgpt/tree/main/pkg/analyzer/"
    click Cache "https://github.com/k8sgpt-ai/k8sgpt/blob/main/pkg/cache/cache.go"
    click Integrations "https://github.com/k8sgpt-ai/k8sgpt/tree/main/pkg/integration/"
    click AI_Factory "https://github.com/k8sgpt-ai/k8sgpt/blob/main/pkg/ai/factory.go"

    %% Styles
    classDef core fill:#cce5ff,stroke:#004085,color:#004085
    classDef external fill:#d4edda,stroke:#155724,color:#155724
    classDef ai fill:#ffe5b4,stroke:#cc8400,color:#cc8400
    classDef optional fill:#f8f9fa,stroke:#6c757d,color:#6c757d
```
