# longhorn/longhorn-manager architecture

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

- Interactive diagram: https://gitdiagram.com/longhorn/longhorn-manager
- Repository: https://github.com/longhorn/longhorn-manager

## Overview

##### Main Components

a. Core Components:
- Longhorn Manager (Control Plane)
- Instance Manager
- CSI Plugin
- UI Component
- Webhook Server
- Recovery Backend
- Metrics Collector

b. External Dependencies:
- Kubernetes API Server
- Storage Backend (iSCSI/NFS)
- Host System Components

##### Component Relationships

- Control flow between Longhorn Manager and Kubernetes API
- Data flow between storage components
- Communication between controllers and their managed resources
- Integration points with CSI infrastructure

##### Detailed Component Breakdown

a. Controller Layer:
- Volume Controller
- Node Controller
- Engine Controller
- Replica Controller
- Backup Controller
- System Backup Controller

b. API Layer:
- REST API endpoints
- WebSocket Controller
- Admission Webhooks
- Conversion Webhooks

c. Storage Layer:
- Engine Management
- Replica Management
- Backup Management
- Volume Management

##### Component Relationships to Highlight

**Longhorn Manager**
- Control Plane
- Controller Manager
- API Server
- Webhook Server
- Data Plane
- Instance Manager
- Engine Components
- Replica Components
- Monitoring
- Metrics Collector
- Recovery Backend

##### Implementation Details

- CSI driver integration
- Volume lifecycle management
- Backup and restore flows
- High availability features
- Monitoring and metrics collection

##### Specific Elements

- The manager's role in orchestrating storage operations
- Integration with Kubernetes storage primitives
- Backup and disaster recovery paths
- Monitoring and metrics collection flows

**┌─ Application Layer ─┐**
- UI / API / Webhook │
- Control Layer ────┤
- Controllers       │
- Storage Layer ───┤
- Engines/Replicas  │
- Infrastructure ──┘

Remember to:
- Use consistent styling
- Brief descriptions for complex interactions
- Both control and data paths
- Scalability points

This system design should provide a clear understanding of how Longhorn Manager orchestrates distributed storage in a Kubernetes environment while maintaining high availability and durability of the data.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Kubernetes Layer"
        K8S[Kubernetes API Server]:::k8s
        CSI[CSI Plugin]:::core
        click CSI "https://github.com/longhorn/longhorn-manager/tree/master/csi/"
    end

    subgraph "Control Plane"
        LHM[Longhorn Manager]:::core
        click LHM "https://github.com/longhorn/longhorn-manager/tree/master/manager/"
        WH[Webhook Server]:::core
        click WH "https://github.com/longhorn/longhorn-manager/tree/master/webhook/"
        API[API Server]:::api
        click API "https://github.com/longhorn/longhorn-manager/tree/master/api/"
        
        subgraph "Controllers"
            VC[Volume Controller]:::controller
            click VC "https://github.com/longhorn/longhorn-manager/blob/master/controller/volume_controller.go"
            NC[Node Controller]:::controller
            click NC "https://github.com/longhorn/longhorn-manager/blob/master/controller/node_controller.go"
            EC[Engine Controller]:::controller
            click EC "https://github.com/longhorn/longhorn-manager/blob/master/controller/engine_controller.go"
            RC[Replica Controller]:::controller
            click RC "https://github.com/longhorn/longhorn-manager/blob/master/controller/replica_controller.go"
            BC[Backup Controller]:::controller
            click BC "https://github.com/longhorn/longhorn-manager/blob/master/controller/backup_controller.go"
            SBC[System Backup Controller]:::controller
            click SBC "https://github.com/longhorn/longhorn-manager/blob/master/controller/system_backup_controller.go"
        end
    end

    subgraph "Data Plane"
        IM[Instance Manager]:::core
        click IM "https://github.com/longhorn/longhorn-manager/blob/master/controller/instance_manager_controller.go"
        MC[Metrics Collector]:::core
        click MC "https://github.com/longhorn/longhorn-manager/tree/master/metrics_collector/"
        
        subgraph "Storage Operations"
            ENG[Engine]:::storage
            click ENG "https://github.com/longhorn/longhorn-manager/blob/master/engineapi/engine.go"
            REP[Replica]:::storage
            click REP "https://github.com/longhorn/longhorn-manager/blob/master/engineapi/proxy_replica.go"
            BAK[Backup]:::storage
            click BAK "https://github.com/longhorn/longhorn-manager/blob/master/engineapi/backups.go"
            RB[Recovery Backend]:::storage
            click RB "https://github.com/longhorn/longhorn-manager/tree/master/recovery_backend/"
        end
    end

    %% Relationships
    K8S <--> LHM
    K8S <--> CSI
    LHM --> WH
    LHM --> API
    
    %% Controller relationships
    LHM --> VC & NC & EC & RC & BC & SBC
    
    %% Data plane relationships
    VC --> IM
    EC --> IM
    RC --> IM
    IM --> ENG
    IM --> REP
    BC --> BAK
    SBC --> RB
    
    %% Metrics flow
    MC --> ENG
    MC --> REP
    MC --> IM

    %% Styles
    classDef core fill:#2374ab,stroke:#000,color:#fff
    classDef controller fill:#457b9d,stroke:#000,color:#fff
    classDef storage fill:#ffd166,stroke:#000
    classDef k8s fill:#2a9d8f,stroke:#000,color:#fff
    classDef api fill:#e63946,stroke:#000,color:#fff

    %% Legend
    subgraph Legend
        L1[Core Components]:::core
        L2[Controllers]:::controller
        L3[Storage]:::storage
        L4[Kubernetes]:::k8s
        L5[API]:::api
    end
```
