# maksim-paskal/hcloud-k8s-ctl architecture

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

- Interactive diagram: https://gitdiagram.com/maksim-paskal/hcloud-k8s-ctl
- Repository: https://github.com/maksim-paskal/hcloud-k8s-ctl

## Overview

##### Main components
- **User CLI (hcloud‑k8s‑ctl)**
- Entry point: `cmd/main.go`
- Parses `config.yaml`, command‑line flags, environment
- **Configuration Module** (`pkg/config`)
- Loads and validates user settings (cluster size, versions, autoscaling groups)
- **Hetzner Cloud API Client** (`pkg/api`)
- Wraps Hetzner Cloud REST calls (create server, network, load balancer, server types)
- **Provisioning Engine** (core logic in `internal/internal.go` + `pkg/drain`)
- Orchestrates resource creation, SSH execution, cluster bootstrap
- **SSH/Script Executor** (`scripts/*.sh`)
- `common-install.sh`, `init-master.sh`, `post-install.sh`, etc.
- Installs OS packages, Docker/containerd, kubeadm, configures master & worker
- **Kubernetes Bootstrap**
- `kubeadm init` on masters, `kubeadm join` on workers
- Flannel CNI, CSI driver, cloud‑controller‑manager, metrics server, autoscaler chart
- **Autoscaling Controller**
- Deploys Kubernetes Cluster‑Autoscaler configured with Hetzner autoscaling groups
- **Examples & E2E Tests**
- Helm chart in `examples/charts`
- End‑to‑end harness in `e2e/` invoking CLI and validating cluster

##### Interactions
- **User** → runs `hcloud-k8s-ctl` CLI →
a. **Configuration Module** reads `config.yaml` →
b. **Hetzner API Client** provisions:
- VPC / Internal network
- Load balancer (for control plane)
- Master servers (odd count)
- (Later) Worker nodes via autoscaler groups
c. **Provisioning Engine** obtains server IPs →
- Uses SSH to run `common-install.sh` on all servers
- Runs `init-master.sh` on masters (with load balancer)
- Once control plane ready, runs `post-install.sh` to deploy add‑ons
d. **Kubernetes cluster** comes online with:
- Kubernetes control plane behind LB
- Flannel CNI, CSI, cloud‑controller, metrics-server, CSR approver
- Cluster‑Autoscaler tied to Hetzner autoscaling groups
- **E2E Test Harness** under `e2e/` → invokes CLI → validates node count, SSH access, API responses

##### Architectural patterns & principles
- **Layered design**:
- `cmd/` (presentation), `pkg/` (business logic), `internal/` (private core), `scripts/` (procedural provisioning)
- **Single‑binary CLI** (Go + Cobra flags + Viper config)
- **Infrastructure as code** via procedural scripts and API calls
- **Extensibility points**: `deployments:` in config to enable optional charts (e.g., NFS)
- **Testing harness** separated under `e2e/`

##### Technologies & frameworks
- Language: Go (modules, `go.mod`)
- HCloud SDK (REST client in `pkg/api`)
- SSH + Shell scripts (`scripts/`)
- Kubernetes tools: `kubeadm`, `kubectl`, Helm templates (`examples/` & `scripts/chart`)
- CICD: GitHub Actions workflows (`.github/workflows`)
- Release automation: Goreleaser (`.goreleaser.yml`)

##### Detail each component
- `scripts/common-install.sh`, `init-master.sh`, `post-install.sh` as separate steps
- Autoscaling groups feeding into the Kubernetes cluster via Cluster‑Autoscaler

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External User
    User["User"]:::external

    %% CLI Layer
    subgraph "Presentation Layer"
        CLI["hcloud-k8s-ctl CLI\n(cmd/main.go)"]:::code
        click CLI "https://github.com/maksim-paskal/hcloud-k8s-ctl/blob/main/cmd/main.go"
    end

    %% Config & Utility Layer
    subgraph "Business Logic Layer"
        Config["Configuration Module\n(pkg/config/config.go)"]:::code
        click Config "https://github.com/maksim-paskal/hcloud-k8s-ctl/blob/main/pkg/config/config.go"
        Version["Version Info\n(pkg/version/version.go)"]:::code
        click Version "https://github.com/maksim-paskal/hcloud-k8s-ctl/blob/main/pkg/version/version.go"
        Utils["Utilities\n(pkg/utils/utils.go)"]:::code
        click Utils "https://github.com/maksim-paskal/hcloud-k8s-ctl/blob/main/pkg/utils/utils.go"
    end

    %% API Client Layer
    subgraph "API Client Layer"
        APIClient["Hetzner Cloud API Client\n(pkg/api/api.go)"]:::code
        click APIClient "https://github.com/maksim-paskal/hcloud-k8s-ctl/blob/main/pkg/api/api.go"
    end

    %% Provisioning Engine
    subgraph "Core Provisioning Layer"
        Provision["Provisioning Engine\n(internal/internal.go)"]:::code
        click Provision "https://github.com/maksim-paskal/hcloud-k8s-ctl/blob/main/internal/internal.go"
        Drain["Node Drain Logic\n(pkg/drain/drain.go)"]:::code
        click Drain "https://github.com/maksim-paskal/hcloud-k8s-ctl/blob/main/pkg/drain/drain.go"
    end

    %% Scripts Layer
    subgraph "Scripts Executor"
        Common["common-install.sh"]:::script
        click Common "https://github.com/maksim-paskal/hcloud-k8s-ctl/blob/main/scripts/common-install.sh"
        InitMaster["init-master.sh"]:::script
        click InitMaster "https://github.com/maksim-paskal/hcloud-k8s-ctl/blob/main/scripts/init-master.sh"
        PostInstall["post-install.sh"]:::script
        click PostInstall "https://github.com/maksim-paskal/hcloud-k8s-ctl/blob/main/scripts/post-install.sh"
        SubScripts["Other scripts\n(scripts/)"]:::script
        click SubScripts "https://github.com/maksim-paskal/hcloud-k8s-ctl/tree/main/scripts/"
    end

    %% Charts & Examples
    subgraph "Charts & Examples"
        AutoscalerChart["Autoscaler Charts\n(scripts/chart/)"]:::script
        click AutoscalerChart "https://github.com/maksim-paskal/hcloud-k8s-ctl/tree/main/scripts/chart/"
        ExampleChart["Example Helm Chart\n(examples/charts/test/)"]:::data
        click ExampleChart "https://github.com/maksim-paskal/hcloud-k8s-ctl/blob/main/examples/charts/test/Chart.yaml"
    end

    %% Config File
    ConfigFile["Global Config File\n(config.yaml)"]:::data
    click ConfigFile "https://github.com/maksim-paskal/hcloud-k8s-ctl/blob/main/config.yaml"

    %% Kubernetes Cluster
    Kubernetes["Kubernetes Cluster\n(Control Plane & Workers)"]:::external

    %% E2E Test Harness
    subgraph "E2E Test Harness"
        E2E["E2E Tests\n(e2e/e2e_test.go)"]:::code
        click E2E "https://github.com/maksim-paskal/hcloud-k8s-ctl/blob/main/e2e/e2e_test.go"
        E2EConfigs["E2E Configs\n(e2e/configs/)"]:::data
        click E2EConfigs "https://github.com/maksim-paskal/hcloud-k8s-ctl/tree/main/e2e/configs/"
    end

    %% Relationships
    User -->|runs CLI| CLI
    CLI -->|reads config| Config
    CLI -->|uses version| Version
    CLI -->|uses utils| Utils
    CLI -->|calls REST API| APIClient
    ConfigFile ---|loaded by| Config

    APIClient -->|provisions infra| Provision
    Provision -->|uses drain logic| Drain
    Provision -->|executes over SSH| Common
    Provision -->|executes over SSH| InitMaster
    Provision -->|executes over SSH| PostInstall
    Common -->|bootstrap via kubeadm| InitMaster
    InitMaster -->|deploy add-ons| PostInstall
    PostInstall -->|deploy charts| AutoscalerChart
    PostInstall -->|deploy example chart| ExampleChart

    AutoscalerChart -->|configures| Kubernetes
    Kubernetes -->|monitored by| Drain

    %% E2E flow
    E2E -->|invokes CLI| CLI
    E2E -->|verifies cluster| Kubernetes
    E2E -->|uses configs| E2EConfigs

    %% Styles
    classDef code fill:#cce5ff,stroke:#4da3ff,color:#004085
    classDef external fill:#d4edda,stroke:#8bc34a,color:#155724
    classDef data fill:#e2e3e5,stroke:#6c757d,color:#1b1e21
    classDef script fill:#fff3cd,stroke:#ffecb5,color:#856404
```
