# canonical/microk8s architecture

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

- Interactive diagram: https://gitdiagram.com/canonical/microk8s
- Repository: https://github.com/canonical/microk8s

## Overview

##### Project type and purpose
- This is an open-source, single-node Kubernetes distribution (MicroK8s) packaged as a Snap and as a standalone installer (Linux and Windows).
- It bundles upstream K8s binaries (etcd, kube-apiserver, kube-scheduler, kube-controller-manager, kubelet, kube-proxy), container runtime (containerd, runc), CNI plugins (flannel, Calico, etc.), a lightweight SQL datastore (k8s-dqlite), a cluster agent, and a pluggable add-on framework.

##### Main architectural layers and components
A. CI/Build Pipeline
- .github/actions, .github/workflows, build-scripts/
- Builds each component (containerd, CNI, Kubernetes, cluster-agent, etc.) from source, applies patches, produces binaries and a final Snap.

B. Packaging Layer
- snap/snapcraft.yaml, snap/hooks/
- Uses Snapcraft to assemble binaries, resources, hooks and wrapper scripts into the final snap.
- Lifecycle hooks (install, configure, start, stop, remove, post-refresh) drive cluster install and management.

C. Installer CLI Layer
- installer/ (Python): microk8s CLI for Linux and Windows.
- installer/cli/ commands (echo, microk8s), installer/common utilities, vm_providers for VM-based installs.

D. Command Wrappers & Orchestration
- microk8s-resources/wrappers/*.wrapper (shell stubs) and scripts/wrappers/*.py
- These implement microk8s commands (enable, disable, start, stop, status, join, leave, addons, version).
- They invoke underlying orchestration logic in installer/common or scripts/wrappers/common, and apply or rollback configuration via hooks.

E. Configuration & Template Store
- microk8s-resources/
– default-args/ YAML and templates for each Kubernetes component.
– default-hooks/ scripts for pre- and post- operations (enable/disable, reconcile).
– certs/ templates for TLS certificates (etcd, apiserver, dqlite).
– actions/ Kubernetes manifests for add-ons (DNS, dashboard, Istio, Linkerd, Prometheus, etc.).

F. Runtime K8s Components
- Container runtime: containerd, runc (with patches).
- Networking: CNI plugin binaries and config (flannel, Calico, custom).
- Datastore: etcd (if HA) or k8s-dqlite for single-node HA.
- Kubernetes binaries: kube-apiserver, kube-controller-manager, kube-scheduler, kubelet, kube-proxy, kubelite.
- Cluster agent: monitors cluster state and applies hooks.

G. Add-on & Upgrade Engine
- microk8s enable/disable frameworks in actions and wrappers.
- upgrade-scripts/ handle in-place migrations (e.g. switch to Calico, dqlite, flannel-etcd).

H. Test Suite
- tests/unit, tests/integration, tests/lxc, smoke tests.
- Validates CLI, wrappers, cluster behavior across distros.

##### Relationships and data flow
- The CI/build pipeline produces a Snap package containing all binaries, templates, hooks and command wrappers.
- snapd invokes lifecycle hooks on install/refresh/remove; hooks call wrapper scripts which use templates from microk8s-resources to generate configs, provision certs and launch each K8s component as a confined process.
- The microk8s CLI (shell or Python) issues commands via wrappers; wrappers call into orchestration libraries (installer/common or scripts/wrappers/common) to perform operations (enable DNS, join cluster, view status).
- Orchestration logic interacts with the Kubernetes API (via kubectl or direct REST) and the container runtime (containerd gRPC socket) to manage containers and pods.
- -ons are deployed by applying Kubernetes manifests (actions/) via the API server.
- Upgrade-scripts are invoked by the CLI or hooks to perform in-place component swaps.
- Tests exercise the CLI and verify that a conformant cluster forms on various distros under LXD.

##### Architectural patterns and design principles
- Layered architecture: build → package → CLI/orchestration → runtime.
- Plugin pattern for add-ons: placing new YAMLs in microk8s-resources/actions automatically surfaces in enable/disable.
- Convention-over-configuration: directory-based mapping of commands, hooks, default args.
- Snap confinement: all binaries and configs self-contained in the snap’s $SNAP directory.
- Extensibility: upgrade-scripts and vm_providers factories allow adding new upgrade paths and VM backends.

##### Essential elements
- Components: Build Pipelines, Snapcraft, Hooks, CLI/Wrappers, Orchestration Libs, Config Templates, K8s Binaries, Container Runtime, Add-on Manifests, Upgrade Engine, Tests

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% CI/Build Pipeline
    subgraph "CI/Build Pipeline"
        github_actions[".github/actions/"]:::build
        workflows[".github/workflows/"]:::build
        build_scripts["build-scripts/"]:::build
    end

    %% Packaging Layer
    subgraph "Packaging Layer"
        snapcraft["snap/snapcraft.yaml"]:::packaging
        snap_hooks["snap/hooks/"]:::packaging
        snap_package["Snap Package"]:::packaging
    end

    %% Installer CLI Layer
    subgraph "Installer CLI Layer"
        cli_dir["installer/cli/"]:::cli
        installer_microk8s["installer/microk8s.py"]:::cli
        installer_common["installer/common/"]:::cli
        vm_providers["installer/vm_providers/"]:::cli
    end

    %% Command Wrappers & Orchestration
    subgraph "Command Wrappers & Orchestration"
        wrappers_resources["microk8s-resources/wrappers/"]:::cli
        wrappers_scripts["scripts/wrappers/"]:::cli
    end

    %% Orchestration & Config Store
    subgraph "Orchestration & Config Store"
        orchestration_libs["Orchestration Libraries"]:::orchestration
        default_args["microk8s-resources/default-args/"]:::orchestration
        default_hooks["microk8s-resources/default-hooks/"]:::orchestration
        certs["microk8s-resources/certs/"]:::orchestration
    end

    %% Runtime Kubernetes Components
    subgraph "Runtime Kubernetes Components"
        contd["build-scripts/components/containerd/"]:::runtime
        runcc["build-scripts/components/runc/"]:::runtime
        cni["build-scripts/components/cni/"]:::runtime
        etcd["build-scripts/components/etcd/"]:::runtime
        dqlite["build-scripts/components/k8s-dqlite/"]:::runtime
        kubernetes["build-scripts/components/kubernetes/"]:::runtime
        profile["microk8s-resources/containerd-profile/"]:::runtime
    end

    %% Add-on & Upgrade Engine
    subgraph "Add-on & Upgrade Engine"
        addon_manifests["microk8s-resources/actions/"]:::addons
        upgrade_scripts["upgrade-scripts/"]:::addons
    end

    %% Test Suite
    subgraph "Test Suite"
        tests_unit["tests/unit/"]:::tests
        tests_integration["tests/integration/"]:::tests
        tests_lxc["tests/lxc/"]:::tests
        tests_smoke["tests/smoke-test.sh"]:::tests
        installer_tests["installer/tests/"]:::tests
    end

    %% Connections
    github_actions --> build_scripts
    workflows --> build_scripts
    build_scripts --> snap_package
    snap_package --> snapcraft
    snap_package --> snap_hooks

    cli_dir --> wrappers_resources
    cli_dir --> wrappers_scripts
    installer_microk8s --> wrappers_resources
    installer_common --> wrappers_resources
    vm_providers --> wrappers_resources

    snap_hooks --> wrappers_scripts
    wrappers_resources --> orchestration_libs
    wrappers_scripts --> orchestration_libs

    orchestration_libs --> default_args
    orchestration_libs --> default_hooks
    orchestration_libs --> certs

    orchestration_libs --> contd
    orchestration_libs --> runcc
    orchestration_libs --> cni
    orchestration_libs --> etcd
    orchestration_libs --> dqlite
    orchestration_libs --> kubernetes
    orchestration_libs --> profile

    orchestration_libs --> addon_manifests
    orchestration_libs --> upgrade_scripts

    tests_unit --> cli_dir
    tests_integration --> snap_hooks
    tests_lxc --> snap_hooks
    tests_smoke --> snap_hooks
    installer_tests --> cli_dir

    %% Click Events
    click github_actions "https://github.com/canonical/microk8s/tree/master/.github/actions/"
    click workflows "https://github.com/canonical/microk8s/tree/master/.github/workflows/"
    click build_scripts "https://github.com/canonical/microk8s/tree/master/build-scripts/"
    click snapcraft "https://github.com/canonical/microk8s/blob/master/snap/snapcraft.yaml"
    click snap_hooks "https://github.com/canonical/microk8s/tree/master/snap/hooks/"
    click cli_dir "https://github.com/canonical/microk8s/tree/master/installer/cli/"
    click installer_microk8s "https://github.com/canonical/microk8s/blob/master/installer/microk8s.py"
    click installer_common "https://github.com/canonical/microk8s/tree/master/installer/common/"
    click vm_providers "https://github.com/canonical/microk8s/tree/master/installer/vm_providers/"
    click wrappers_resources "https://github.com/canonical/microk8s/tree/master/microk8s-resources/wrappers/"
    click wrappers_scripts "https://github.com/canonical/microk8s/tree/master/scripts/wrappers/"
    click default_args "https://github.com/canonical/microk8s/tree/master/microk8s-resources/default-args/"
    click default_hooks "https://github.com/canonical/microk8s/tree/master/microk8s-resources/default-hooks/"
    click certs "https://github.com/canonical/microk8s/tree/master/microk8s-resources/certs/"
    click addon_manifests "https://github.com/canonical/microk8s/tree/master/microk8s-resources/actions/"
    click contd "https://github.com/canonical/microk8s/tree/master/build-scripts/components/containerd/"
    click runcc "https://github.com/canonical/microk8s/tree/master/build-scripts/components/runc/"
    click cni "https://github.com/canonical/microk8s/tree/master/build-scripts/components/cni/"
    click etcd "https://github.com/canonical/microk8s/tree/master/build-scripts/components/etcd/"
    click dqlite "https://github.com/canonical/microk8s/tree/master/build-scripts/components/k8s-dqlite/"
    click kubernetes "https://github.com/canonical/microk8s/tree/master/build-scripts/components/kubernetes/"
    click profile "https://github.com/canonical/microk8s/tree/master/microk8s-resources/containerd-profile/"
    click upgrade_scripts "https://github.com/canonical/microk8s/tree/master/upgrade-scripts/"
    click tests_unit "https://github.com/canonical/microk8s/tree/master/tests/unit/"
    click tests_integration "https://github.com/canonical/microk8s/tree/master/tests/integration/"
    click tests_lxc "https://github.com/canonical/microk8s/tree/master/tests/lxc/"
    click tests_smoke "https://github.com/canonical/microk8s/blob/master/tests/smoke-test.sh"
    click installer_tests "https://github.com/canonical/microk8s/tree/master/installer/tests/"

    %% Styles
    classDef build fill:#ADD8E6,stroke:#333
    classDef packaging fill:#00008B,stroke:#fff,color:#fff
    classDef cli fill:#98FB98,stroke:#333
    classDef orchestration fill:#FFA500,stroke:#333
    classDef runtime fill:#D3D3D3,stroke:#333
    classDef addons fill:#FFFF00,stroke:#333
    classDef tests fill:#DDA0DD,stroke:#333
```
