# jonasprogrammer/docker-machine-driver-hetzner architecture

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

- Interactive diagram: https://gitdiagram.com/jonasprogrammer/docker-machine-driver-hetzner
- Repository: https://github.com/jonasprogrammer/docker-machine-driver-hetzner

## Overview

##### Project type and purpose
- This is a Go‑based CLI plugin (a Docker Machine “driver”) that enables users to create and manage Docker hosts on the Hetzner Cloud.
- It integrates into the Docker Machine ecosystem by implementing the driver interface and calls out to the Hetzner Cloud API to provisioning servers, networks, volumes, SSH keys, etc.

##### File structure
- Top-level:
- main.go – the CLI entrypoint that registers the driver with Docker Machine
- go.mod/go.sum – Go module configuration
- .github/, .gitignore, LICENSE, README.md, goreleaser.yml – repo, build and CI config
- driver/ directory: contains the core driver implementation, subdivided by concern:
- flag_processing.go, flag_failure.go – parse CLI flags via mcnflags and translate into driver options
- driver.go, setup.go, cleanup.go – implement the Create, Remove, GetState, etc. operations of the driver interface
- networking.go, volumes.go, placement_groups.go, ssh_keys.go, instrumentation_impl.go – domain‑specific modules that each wrap corresponding Hetzner Cloud API calls
- instrumentation_stub.go – stubbed implementation for testing
- hetzner_query.go – centralizes API‑client calls (via hcloud-go)

##### Extract architecture hints from the README
- The driver accepts CLI flags or environment variables (e.g. HETZNER_API_TOKEN)
- Uses the hcloud-go v2 client to call Hetzner Cloud REST endpoints
- Follows Docker Machine’s driver plugin interface – communicates with Docker Machine via RPC/CLI
- Provisions servers, networks, keys, volumes, placement groups in Hetzner Cloud, then configures the host via SSH/cloud‑init

##### Components
a. Frontend / CLI Layer
- User’s shell
- docker‑machine core (external)
- docker‑machine-driver-hetzner binary
b. Driver internals
- Flag Processor (mcnflags)
- Driver Controller (implements Create(), Remove(), ... )
- Domain Services:
- Server Provisioner (setup.go, cleanup.go)
- Networking Manager (networking.go)
- Volume Attacher (volumes.go)
- Placement Group Manager (placement_groups.go)
- SSH Key Manager (ssh_keys.go)
- Instrumentation & Logging (instrumentation_impl.go)
- hcloud-go API Client
c. External Dependencies
- Hetzner Cloud API (REST endpoints: Servers, Images, SSH Keys, Networks, Volumes, Placement Groups)
- Target VM via SSH / cloud‑init (for provisioning Docker Engine)
- Local File System (for storing machine‑state JSON, SSH keys)

##### Relationships & Data Flows
- User types “docker-machine create --driver hetzner …” → docker‑machine core invokes the driver binary
- CLI Layer → Flag Processor → Driver Controller
- Driver Controller coordinates Domain Services, each of which calls out over HTTPS to Hetzner Cloud API via the hcloud-go client
- Hetzner Cloud returns JSON responses → Domain Services interpret results → Driver Controller waits for server to reach “running” state
- Driver Controller uses SSH Key Manager + Local File System → writes private key and machine state files
- Driver Controller (or cloud-init) SSHs into the new server to install Docker Engine, then returns control back to docker‑machine core
- docker‑machine core persists machine metadata and reports success

##### Architectural patterns & design principles
- Separation of Concerns: each cloud‑resource type has its own module
- Plug‑in based architecture: implements Docker Machine’s driver interface
- Dependency Injection / Stubbing: instrumentation_stub.go allows testing without real API calls
- Use of a third‑party client library (hcloud-go) to encapsulate REST calls

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend / CLI Layer
    subgraph "CLI Layer"
        direction TB
        User["User Shell"]:::user
        DMCore["docker-machine core"]:::external
        DMHetzner["docker-machine-driver-hetzner"]:::frontend
    end

    %% Driver Internals
    subgraph "Driver Internals"
        direction TB
        FP["Flag Processor"]:::process
        Controller["Driver Controller"]:::process
        subgraph "Domain Services"
            direction TB
            ServerProv["Server Provisioner"]:::process
            ServerClean["Server Cleanup"]:::process
            NetMgr["Networking Manager"]:::process
            VolMgr["Volume Attacher"]:::process
            PGMgr["Placement Group Manager"]:::process
            SSHMgr["SSH Key Manager"]:::process
            InstrReal["Instrumentation & Logging"]:::process
            InstrStub["Instrumentation Stub"]:::process
        end
        HcloudClient["Hetzner Cloud API Client"]:::service
        Options["Driver Options & Labels"]:::process
    end

    %% External Dependencies
    subgraph "External Dependencies"
        direction TB
        HetznerAPI["Hetzner Cloud API"]:::cloud
        TargetVM["Target VM (SSH/cloud-init)"]:::cloud
        LocalFS["Local File System"]:::storage
    end

    %% Flows
    User -->|"docker-machine create --driver hetzner"| DMCore
    DMCore -->|invokes| DMHetzner
    DMHetzner -->|parses flags| FP
    FP -->|"builds options"| Options
    Options --> Controller
    Controller -->|"provision"| ServerProv
    Controller -->|cleanup| ServerClean
    Controller -->|configure network| NetMgr
    Controller -->|attach volumes| VolMgr
    Controller -->|manage placements| PGMgr
    Controller -->|manage SSH keys| SSHMgr
    Controller -->|log & metrics| InstrReal
    Controller -->|stub metrics| InstrStub
    ServerProv -->|calls| HcloudClient
    ServerClean -->|calls| HcloudClient
    NetMgr -->|calls| HcloudClient
    VolMgr -->|calls| HcloudClient
    PGMgr -->|calls| HcloudClient
    SSHMgr -->|calls| HcloudClient
    HcloudClient -->|HTTPS JSON| HetznerAPI
    HetznerAPI -->|JSON response| HcloudClient
    Controller -->|writes state & keys| LocalFS
    Controller -->|SSH to provision| TargetVM
    TargetVM -->|cloud-init| TargetVM
    TargetVM -->|Docker Engine installed| Controller
    Controller -->|reports success| DMCore

    %% Click Events
    click DMHetzner "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/main.go"
    click FP "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/driver/flag_processing.go"
    click Controller "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/driver/driver.go"
    click ServerProv "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/driver/setup.go"
    click ServerClean "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/driver/cleanup.go"
    click NetMgr "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/driver/networking.go"
    click PGMgr "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/driver/placement_groups.go"
    click SSHMgr "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/driver/ssh_keys.go"
    click InstrReal "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/driver/instrumentation_impl.go"
    click InstrStub "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/driver/instrumentation_stub.go"
    click HcloudClient "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/driver/hetzner_query.go"
    click Options "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/driver/labels.go"
    click LocalFS "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/go.mod"
    click LocalFS "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/go.sum"
    click DMCore "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/.github/workflows/go.yml"
    click DMCore "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/.goreleaser.yml"
    click DMCore "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/blob/master/README.md"
    click DMCore "https://github.com/jonasprogrammer/docker-machine-driver-hetzner/tree/master/LICENSE"

    %% Styles
    classDef user fill:#a4f9a4,stroke:#333,stroke-width:1px;
    classDef frontend fill:#a3d5ff,stroke:#333,stroke-width:1px;
    classDef process fill:#d0e6a5,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5;
    classDef service fill:#ffe699,stroke:#333,stroke-width:1px;
    classDef external fill:#f2f2f2,stroke:#333,stroke-width:1px;
    classDef cloud fill:#cde6f7,stroke:#333,stroke-width:1px;
    classDef storage fill:#f7d6e0,stroke:#333,stroke-width:1px;
```
