# rancher/rke2 architecture

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

- Interactive diagram: https://gitdiagram.com/rancher/rke2
- Repository: https://github.com/rancher/rke2

## Overview

##### Main Components
- RKE2 Core Engine – Represent the heart of the project that includes the components in pkg/rke2 (logic for the Kubernetes distribution, handling of cluster “agent” vs “server” modes, service accounts, etc.).
- CLI and Configuration Management – Modules in pkg/cli (commands for starting the agent, server, certificate management, defaults, and configuration mapping from YAML/CLI). Also include configuration files (/etc/rancher/rke2/config.yaml) as an input.
- Supporting Libraries and Modules – Other subsystems like pkg/auth for authentication, pkg/staticpod for pod generation, pkg/logging for consistent logging, pkg/images for managing images, and modules in pkg/windows (support for Windows variants) as well.
- Deployment & Service Wrappers – The bundle directory contains scripts and systemd unit files for both rke2-agent and rke2-server (for Linux environments) and the corresponding Windows PowerShell scripts. Include these to show how the project integrates with system init systems.
- Build and Deployment Infrastructure – Show the build pipelines (GitHub workflows, Dockerfiles, Vagrantfiles) and packaging scripts (in the scripts directory) orchestrating builds, tests, and image uploads.
- External Components – Mark external integrations for Kubernetes (the kubeconfig, kubectl integration), CIS benchmarks, FIPS compliance support, CVE scanning via Trivy, and the Kubernetes API server interactions once deployed. Also note that helm charts (in the charts directory) support cluster installation and upgrades.

##### Relationships and Interactions
- Configuration Flow: Illustrate how configuration flows from the YAML file and CLI parameters into the startup routines handled by pkg/bootstrap and pkg/cli, eventually reaching core functions in pkg/rke2.
- Service Initialization: The main.go ties it all together, calling the necessary libraries to initialize either an agent or server process. The systemd/unit scripts (bundled in bundle/lib/systemd) then orchestrate service management.
- Modular Communication: Show interaction between the CLI commands and the underlying engine (e.g., commands in pkg/cli invoking methods in pkg/rke2, authentication routines from pkg/auth, and logging from pkg/logging).
- Build & Operational Pipelines: Represent how the GitHub workflows (from .github/workflows) and supporting scripts in “scripts” and “updatecli” trigger builds, tests, and deployment actions. This outlines the CI/CD and image-building aspects.

##### Architectural Patterns and Design Principles
- Modular Design: The repository is split into clearly defined Go packages each responsible for a set of functionalities (e.g., authentication, logging, CLI commands).
- Separation of Concerns: There is a distinct separation between configuration management, runtime logic, and deployment scripts.
- Integration with System Services: The project fully integrates with system-level service managers (systemd on Linux, Windows services) indicating a design that enables consistent management across different operating systems.

##### Relevant Technologies, Frameworks, and Tools
- Go as the primary language with its module system (see go.mod and go.sum).
- Systemd for Linux service orchestration and Windows PowerShell scripts for Windows.
- Docker and containerization (as shown by Dockerfile variants and container test resources).
- CI/CD using GitHub Actions (review .github/workflows and associated GitHub actions).
- Security and compliance tools – Trivy for vulnerability scanning and direct references to CIS benchmarks and FIPS compliance.

## Diagram (Mermaid)

```mermaid
graph TD
    %% User/Operator Layer
    subgraph "User/Operator Layer"
        Config["Config YAML (/etc/rancher/rke2/config.yaml)"]:::operator
        CLI["RKE2 CLI & Configuration Management"]:::internal
    end

    %% Core Engine Layer
    subgraph "Core Engine Layer"
        Core["RKE2 Core Engine"]:::internal
        Auth["Authentication Module"]:::internal
        Static["Static Pod Generator"]:::internal
        Logging["Logging Module"]:::internal
        Images["Images Management Module"]:::internal
        Windows["Windows Support Modules"]:::internal
    end

    %% Infrastructure & Deployment Layer
    subgraph "Infrastructure & Deployment Layer"
        subgraph "Deployment & Service Wrappers"
            Systemd["Systemd Service Unit (Linux)"]:::deployment
            Scripts["Uninstall & Launch Scripts"]:::deployment
        end
        subgraph "Build & Deployment Infrastructure"
            CICD["CI/CD Pipelines"]:::ci
            BuildScripts["Packaging & Build Scripts"]:::ci
        end
        Helm["Helm Charts for Installation"]:::deployment
    end

    %% External Components
    External["External Integrations (Kubernetes API, CIS, FIPS, Trivy)"]:::external

    %% Relationships
    Config -->|"provides"| CLI
    CLI -->|"initiates"| Core
    Core -->|"calls"| Auth
    Core -->|"triggers"| Static
    Core -->|"logs"| Logging
    Core -->|"handles"| Images
    Core -->|"supports"| Windows
    BuildScripts -->|"feeds"| CICD
    CICD -->|"executes"| BuildScripts
    BuildScripts -->|"deploys via"| Systemd
    BuildScripts -->|"deploys via"| Scripts
    Systemd -->|"orchestrates"| Core
    Scripts -->|"orchestrates"| Core
    Helm -->|"installs/upgrades"| Core
    Core -->|"integrates_with"| External

    %% Additional internal relation for CLI defaults
    CLI -->|"uses_defaults"| CLI_Default["CLI Defaults"]:::internal

    %% Click Events
    click Core "https://github.com/rancher/rke2/tree/master/pkg/rke2"
    click CLI "https://github.com/rancher/rke2/tree/master/pkg/cli"
    click CLI_Default "https://github.com/rancher/rke2/tree/master/pkg/cli/defaults"
    click Auth "https://github.com/rancher/rke2/tree/master/pkg/auth"
    click Static "https://github.com/rancher/rke2/tree/master/pkg/staticpod"
    click Logging "https://github.com/rancher/rke2/tree/master/pkg/logging"
    click Images "https://github.com/rancher/rke2/tree/master/pkg/images"
    click Windows "https://github.com/rancher/rke2/tree/master/pkg/windows"
    click Systemd "https://github.com/rancher/rke2/tree/master/bundle/lib/systemd/system"
    click Scripts "https://github.com/rancher/rke2/tree/master/bundle/bin"
    click CICD "https://github.com/rancher/rke2/tree/master/.github/workflows"
    click BuildScripts "https://github.com/rancher/rke2/tree/master/scripts"
    click Helm "https://github.com/rancher/rke2/tree/master/charts"

    %% Styles
    classDef internal fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef operator fill:#D5DBDB,stroke:#5D6D7E,stroke-width:2px;
    classDef deployment fill:#F9E79F,stroke:#7D6608,stroke-width:2px;
    classDef ci fill:#F5CBA7,stroke:#A04000,stroke-width:2px;
    classDef external fill:#ABEBC6,stroke:#1E8449,stroke-width:2px;
```
