How canonical/microk8s works
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