How k3d-io/k3d works
1. What this repository is
- k3d is a Go-based CLI for creating and managing k3s clusters inside Docker containers.
- The architectural boundary is clear: the user talks to the CLI, and the CLI talks to the local Docker daemon to create/update/remove cluster components.
2. Primary entry points
- main.go and cmd/root.go are the top-level entry into the application.
- The cmd/ tree is organized by user-facing subcommands: cluster, node, registry, image, config, kubeconfig, debug, and version.
- Each subcommand layer is thin and delegates most real work to packages under pkg/.
3. Command layer as orchestration shell
- The cmd/* packages are the presentation layer for CLI arguments, flags, and output formatting.
- They translate user intent such as “create cluster”, “import image”, or “merge kubeconfig” into structured actions and calls into the client/domain packages.
- cmd/util/ contains shared CLI helpers for output, filtering, completion, ports, runtime labels, and config handling.
4. Core domain and intent model
- pkg/types/ defines the project’s core data structures: clusters, nodes, registries, load balancers, images, K3s-related paths/env, and “fixes” assets.
- This layer represents the desired topology and runtime metadata the CLI needs to materialize.
- The pkg/actions/ package contains higher-level behavior hooks, such as node lifecycle hooks.
5. Configuration pipeline
- pkg/config/ is a substantial subsystem for reading, merging, validating, transforming, and migrating configuration files across versions.
- Versioned schemas and types live under pkg/config/v1alpha2 through v1alpha5, showing that config compatibility is a first-class concern.
- The flow is: user config file → merge/transform/migrate → validate → normalized internal config used by cluster operations.
6. Client layer as the main coordination boundary
- pkg/client/ is the central orchestration layer that bridges CLI commands and runtime implementation.
- It owns cluster, node, registry, kubeconfig, host, IPAM, load balancer, environment, and port-related coordination.
- This is where high-level requested state becomes concrete runtime operations, making it the most important “control plane” inside the repo.
7. Runtime abstraction and Docker backend
- pkg/runtimes/ defines the runtime interface and errors/types shared across implementations.
- pkg/runtimes/docker/ is the only visible concrete backend, responsible for talking to Docker to create networks, containers, volumes, images, and inspect host/runtime state.
- The architecture therefore depends on Docker as the external execution substrate; k3d is not managing Kubernetes directly, but K3s containers running in Docker.
8. Cluster topology and data flow
- A cluster creation request typically flows from CLI → client → Docker runtime.
- The runtime creates the k3s server/agent containers, networking, volumes, and any supporting components.
- The cluster can be multi-node, and the codebase includes explicit start/stop/delete/edit/list operations for both clusters and nodes, indicating lifecycle management is a central workflow.
9. Load balancer and networking
- k3d includes an internal load balancer model in pkg/types/loadbalancer.go and runtime support in pkg/client/loadbalancer.go plus Docker network helpers.
- The proxy/ directory contains an nginx-based proxy image/config/templates, which is used as the cluster’s traffic entry point.
- This means service exposure is mediated through an internal proxy container rather than direct host binding alone.
10. Registries and image handling
- The repo has dedicated support for local registries via cmd/registry/, pkg/client/registry.go, and pkg/types/registry.go.
- cmd/image/ and pkg/client/tools.go support importing images into the Docker/k3d environment.
- This subsystem matters because local dev workflows often depend on preloading images and wiring registries into the cluster.
11. Kubeconfig integration
- cmd/kubeconfig/ and pkg/client/kubeconfig.go manage retrieving and merging kubeconfig data after cluster creation.
- This is the main integration boundary with external Kubernetes tooling such as kubectl.
- The user workflow is designed so a newly created cluster can be immediately made current by updating the local kubeconfig context.
12. Environment and compatibility helpers
- pkg/types/fixes/ contains shell assets used to patch runtime behavior inside containers, such as DNS, cgroupv2, and mount-related fixes.
- pkg/util/ and pkg/runtimes/util/ hold reusable helpers for ports, YAML, labels, random strings, registry logic, and volume handling.
- These are supporting layers that keep the runtime orchestration consistent across Docker/container edge cases.
13. Validation, testing, and compatibility
- The repository has extensive tests under pkg/config, pkg/client, pkg/runtimes/docker, cmd/util, and end-to-end shell tests under tests/.
- Versioned config schemas, migration tests, and matrix test scripts indicate backward compatibility and lifecycle reliability are major architectural concerns.
- The tools/ and docgen/ directories support release/documentation generation rather than product runtime behavior.
14. How to draw the architecture
- Model the system as: User → CLI commands → client orchestration → runtime abstraction → Docker daemon → containers/network/volumes/proxy → k3s cluster.
- Put config migration/validation as a preprocessing path feeding the client layer.
- Put kubeconfig merge/get as an output path back to the user’s local Kubernetes tooling.
Main entry points: root.go Root cmd · docker.go Docker backend · config.go Config core · types.go Intent model
Components 32
CLI Layer
User-facing commands, argument parsing, and output helpers.
- main.go · Program startup entry point.
main.go - Root cmd · Top-level CLI wiring and subcommand registration.
cmd/root.go - Cluster cmds · Create, delete, edit, list, start, and stop clusters.
cmd/cluster/cluster.go - Node cmds · Create, delete, edit, list, start, and stop nodes.
cmd/node/node.go - Registry cmds · Create, delete, list, start, and stop registries.
cmd/registry/registry.go - Config cmds · Init, migrate, and view configuration files.
cmd/config/config.go - Kubeconfig cmds · Get and merge kubeconfig output for clusters.
cmd/kubeconfig/kubeconfig.go - Image cmds · Import images into the local k3d environment.
cmd/image/image.go - CLI utils · Shared helpers for output, filtering, completions, ports, and config handling.
cmd/util/util.go
Core Domain
Shared types and behavior models used across the app.
- Intent model · Core cluster, node, registry, load balancer, image, and K3s metadata types.
pkg/types/types.go - Node hooks · Hook points for node lifecycle behavior.
pkg/actions/nodehooks.go
Config Pipeline
Versioned config ingestion, normalization, and validation.
- Config core · Orchestrates config processing from raw input to normalized state.
pkg/config/config.go - Versioned schemas · Versioned config types and migration schemas across releases.
pkg/config/v1alpha5/types.go - Migrate/merge · Merges, transforms, and migrates config between versions.
pkg/config/migrate.go - Validate · Validates processed config before cluster operations.
pkg/config/validate.go
Client Orchestration
High-level coordination between commands and runtime operations.
- Client core · Main coordination boundary between CLI intent and runtime actions.
pkg/client/cluster.go - Cluster flow · Cluster lifecycle orchestration.
pkg/client/cluster.go - Node flow · Node lifecycle orchestration.
pkg/client/node.go - Registry flow · Registry lifecycle orchestration.
pkg/client/registry.go - Kubeconfig flow · Retrieves and merges kubeconfig data.
pkg/client/kubeconfig.go - LB flow · Coordinates load balancer and exposure setup.
pkg/client/loadbalancer.go - IPAM/ports · Manages address allocation and port coordination.
pkg/client/ipam.go
Runtime Backend
Runtime abstraction and Docker implementation.
- Runtime API · Shared runtime contract and error types.
pkg/runtimes/runtime.go - Docker backend · Concrete runtime implementation over Docker.
pkg/runtimes/docker/docker.go - Network ops · Creates and manages Docker networks.
pkg/runtimes/docker/network.go - Container ops · Creates, starts, stops, and inspects containers.
pkg/runtimes/docker/container.go - Volume ops · Manages Docker volumes used by clusters and nodes.
pkg/runtimes/docker/volume.go - Image ops · Handles image import and image inspection in Docker.
pkg/runtimes/docker/image.go - Host info · Reads host and Docker environment details.
pkg/runtimes/docker/host.go
External Integrations
Proxying, kubeconfig output, and local registry/image flows.
- Nginx proxy · Traffic entry proxy for exposed cluster services.
proxy/nginx-proxy - Local kubeconfig · Merged kubeconfig written back for kubectl use.
pkg/client/kubeconfig.go - Local registry · Registry support for local dev workflows and image pulls.
pkg/types/registry.go
Connections 32
- main.go → Root cmd: starts · Program entry initializes the CLI command tree.
- Root cmd → Cluster cmds: registers · Root command exposes cluster lifecycle subcommands.
- Root cmd → Node cmds: registers · Root command exposes node lifecycle subcommands.
- Root cmd → Registry cmds: registers · Root command exposes registry subcommands.
- Root cmd → Config cmds: registers · Root command exposes config subcommands.
- Root cmd → Kubeconfig cmds: registers · Root command exposes kubeconfig subcommands.
- Root cmd → Image cmds: registers · Root command exposes image import subcommands.
- Cluster cmds → Cluster flow: delegates · Cluster commands translate user intent into client workflows.
- Node cmds → Node flow: delegates · Node commands delegate lifecycle operations.
- Registry cmds → Registry flow: delegates · Registry commands delegate registry lifecycle operations.
- Kubeconfig cmds → Kubeconfig flow: delegates · Kubeconfig commands request get/merge behavior from the client.
- Image cmds → Client core: delegates · Image import uses the client coordination boundary.
- Config cmds → Config core: uses · Config commands invoke parsing, migration, and validation.
- Config core → Migrate/merge: includes · Processing includes version migration and merging.
- Config core → Validate: includes · Processing validates normalized config.
- Migrate/merge → Versioned schemas: transitions · Migration moves config across versioned schemas.
- Config core → Intent model: normalizes to · Processed config becomes internal intent and topology types.
- Cluster flow → Intent model: reads intent · Cluster orchestration is driven by the domain intent model.
- Node flow → Intent model: reads intent · Node orchestration is driven by the domain intent model.
- Registry flow → Intent model: reads intent · Registry orchestration is driven by the domain intent model.
- Client core → Runtime API: depends on · Client coordinates work through the runtime contract.
- Runtime API → Docker backend: implemented by · Docker is the concrete runtime backend.
- Docker backend → Container ops: manages · Runtime provisions and controls containers.
- Docker backend → Network ops: manages · Runtime creates and wires Docker networks.
- Docker backend → Volume ops: manages · Runtime provisions persistent volumes.
- Docker backend → Image ops: manages · Runtime imports and inspects images.
- Docker backend → Host info: inspects · Runtime discovers host and daemon capabilities.
- LB flow → Nginx proxy: orchestrates · Client sets up the proxy entrypoint for service exposure.
- Kubeconfig flow → Local kubeconfig: writes · Kubeconfig operations return local client config.
- Registry flow → Local registry: coordinates · Registry workflow integrates local registry support.
- IPAM/ports → Network ops: allocates around · IP and port logic is tied to Docker networking.
- Node hooks → Node flow: extends · Lifecycle hooks augment node orchestration.