# kusionstack/karpor architecture

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

- Interactive diagram: https://gitdiagram.com/kusionstack/karpor
- Repository: https://github.com/kusionstack/karpor

## Overview

##### Core Components
- Frontend UI: Located in the “ui” directory, it is built using React/TypeScript. It handles user interactions, displays dashboards (search, insights, topology maps, AI diagnostics) and communicates with the backend via REST/HTTP.
- API Server / Backend: The “cmd/karpor” folder and many packages inside “pkg” (especially pkg/core, pkg/server, pkg/syncer) form the backend. This part handles business logic, processes API requests, deals with authentication, and returns data to the frontend.
- API Specification: The “api” folder (with openapi spec files) documents the REST endpoints used by the API server. Include this as a “Contract/API Spec” component.
- Syncer and Data Aggregation: Under “pkg/syncer” you find modules for resource caching and synchronization. These components are responsible for collecting data from multiple Kubernetes clusters and keeping that in sync.
- Kubernetes Integration: The “pkg/kubernetes” directory contains generated clientsets, Kubernetes APIs, and registration logic. This enables the system to interact with one or many Kubernetes clusters.
- Infrastructure Integrations: The “pkg/infra” folder contains integrations with external services such as:
- AI Providers (see pkg/infra/ai – with clients for OpenAI, Azure OpenAI, HuggingFace, Deepseek).
- Persistence and Search: Modules for Elasticsearch clients are in pkg/infra/persistence/elasticsearch and pkg/infra/search/storage.
- Topology & Resource Relationship Management: Modules under pkg/infra/topology handle the resource mapping.
- Additional Services and Tools: There is a “cmd/cert-generator” (for certificate generation) and configuration files in “config” (which help define RBAC policies and sync strategies).

##### Depicting Relationships and Data Flows
- Within the “Backend” area, divide components into:
– API Layer (using contracts from the “api” folder).
– Business Logic and Core Module (pkg/core, pkg/server) that includes route handling, search & insight logic.
– Syncer/Cache Module (pkg/syncer), illustrating continuous communication with Kubernetes clusters and pulling real-time data.
- Backend to multiple external systems:
– Persistence/Search Engine: Add “Elasticsearch” (from pkg/infra/persistence and pkg/infra/search) and show how data from syncers or search handlers is pushed to/read from the search storage.

##### Architectural Patterns and Design Principles
- The code organization suggests use of separation of concerns (with clear divisions for API, business logic, external integration, and UI).
- Use of code generation (“hack” and parts of “pkg/kubernetes/generated”) that integrates with the Kubernetes API conventions.
- The project uses a modular design so that each integration (search, AI, topology) is encapsulated in its own package.

##### Putting It Together
- UI (React application).
- API/Backend (Golang-based service, including syncer, business logic, and Kubernetes integrations).
- External Systems (Kubernetes clusters, AI providers, Elasticsearch).
- Configuration/Deployment (Helm Charts, Docker, CI/CD as indicated by configuration and workflows).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        FE["Frontend UI"]:::frontend
    end

    %% API/Backend Layer
    subgraph "API / Backend"
        APIS["API Server"]:::backend
        BL["Business Logic/Core"]:::backend
        APISpec["API Specification (Contract)"]:::backend
        SYNC["Syncer/Cache Module"]:::backend
        KUBE_INT["Kubernetes Integration"]:::backend
    end

    %% External Systems
    subgraph "External Systems"
        KUBE["Kubernetes Clusters"]:::integration
        AI["AI Providers"]:::integration
        ES["Persistence/Search (Elasticsearch)"]:::integration
        TOPO["Topology Management"]:::integration
    end

    %% Auxiliary Services
    subgraph "Auxiliary Services"
        CERT["Certificate Generator"]:::tool
        CONFIG["Configuration/Policy"]:::config
    end

    %% Connections between layers
    FE -->|"REST_API"| APIS
    APIS -->|"Uses"| APISpec
    APIS -->|"Delegates"| BL
    APIS -->|"Triggers"| SYNC
    APIS -->|"Manages"| KUBE_INT
    SYNC -->|"Sync_Data"| KUBE
    KUBE_INT -->|"Connects"| KUBE
    BL -->|"Queries"| AI
    BL -->|"Search_Query"| ES
    BL -->|"Resource_Map"| TOPO
    CONFIG -->|"Feeds_Config"| APIS
    CONFIG -->|"Feeds_Config"| SYNC
    CERT -.->|"Secures"| APIS

    %% Click Events
    click FE "https://github.com/kusionstack/karpor/tree/main/ui/"
    click APIS "https://github.com/kusionstack/karpor/tree/main/cmd/karpor/"
    click APISpec "https://github.com/kusionstack/karpor/tree/main/api/openapispec/"
    click BL "https://github.com/kusionstack/karpor/tree/main/pkg/core/"
    click SYNC "https://github.com/kusionstack/karpor/tree/main/pkg/syncer/"
    click KUBE_INT "https://github.com/kusionstack/karpor/tree/main/pkg/kubernetes/"
    click AI "https://github.com/kusionstack/karpor/tree/main/pkg/infra/ai/"
    click ES "https://github.com/kusionstack/karpor/tree/main/pkg/infra/persistence/elasticsearch/"
    click TOPO "https://github.com/kusionstack/karpor/tree/main/pkg/infra/topology/"
    click CERT "https://github.com/kusionstack/karpor/tree/main/cmd/cert-generator/"
    click CONFIG "https://github.com/kusionstack/karpor/tree/main/config/"

    %% Styles for color coding
    classDef frontend fill:#f4cccc,stroke:#cc0000,stroke-width:2px;
    classDef backend fill:#c9daf8,stroke:#1155cc,stroke-width:2px;
    classDef integration fill:#d9ead3,stroke:#38761d,stroke-width:2px;
    classDef tool fill:#fff2cc,stroke:#e69138,stroke-width:2px;
    classDef config fill:#ead1dc,stroke:#b64a8f,stroke-width:2px;
```
