How srl-labs/containerlab works
Purpose and entry points
Containerlab is a Go CLI for declaratively deploying, wiring, operating, and inspecting container/VM-based network labs. main.go launches the Cobra-style command surface rooted in cmd/root.go; lifecycle commands such as deploy, destroy, apply, exec, graph, and generate are implemented under cmd/ and delegate to core/.
Topology ingestion and orchestration model
Topology definitions are represented by shared types and loaded/reconciled into a lab model before runtime changes occur. core/topology.go and core/config.go handle topology/config processing; core/topology_reconcile.go supports desired-versus-existing state reconciliation. core/clab.go is the central lab object coordinating runtime, nodes, links, state, and lifecycle options.
Deployment and lifecycle pipeline
Deployment flows from command input through core/deploy.go into staged node/link provisioning, while core/apply.go updates an existing lab toward a new desired topology. Start/stop/restart/destroy are distinct lifecycle operations in core/start.go, core/stop.go, and core/destroy.go. The dependency graph manager orders dependent components/stages and is a key concurrency/control boundary: core/dependency_manager/dependency_manager.go, core/dependency_manager/depender_node_stage.go.
Node-kind abstraction
nodes/node.go defines the common node contract; nodes/node_registry.go selects implementations by topology kind. Each kind encapsulates vendor/image-specific preparation, configuration, boot, and interface behavior, isolating the orchestration core from NOS and VM quirks. Representative implementations are nodes/srl/srl.go, nodes/sros/sros.go, and nodes/linux/linux.go; the repository includes many additional vendor and vrnetlab-oriented kinds.
Runtime boundary and infrastructure
The runtime layer abstracts container-engine operations behind runtime/runtime.go, with Docker as the principal implementation in runtime/docker/docker.go and optional Podman support in runtime/podman/podman.go; registration is assembled in runtime/all/all.go. It creates/manages containers, networks, events, authentication, and firewall rules. The host must provide Linux container/network primitives and a compatible Docker or Podman service.
Network-link realization
Logical topology links are converted into host/network-namespace connectivity by links/apply.go. Endpoint and link variants support veth, bridges, host attachments, macvlan, VXLAN, management networks, and stitched links; core definitions include links/link.go, links/endpoint.go, and links/link_vxlan_stitched.go. This is the material boundary between topology intent and Linux netlink/network namespace state.
State, artifacts, and operational outputs
Persistent lab/runtime state supports lifecycle and reconciliation through core/state.go and core/runtime_state.go. Deployment also produces user-facing artifacts such as inventory and SSH configuration via core/inventory.go and core/sshconfig.go. Graph rendering is generated from the resolved topology in core/graph.go, using packaged templates under core/graph_templates/nextui.
Supporting services and integrations
Certificate provisioning is a first-class deploy concern via core/cert.go and the local CA/storage implementation in cert/. Commands expose event streaming (core/events/stream.go) and utility integrations such as API server, terminal sharing, traffic impairment, and VXLAN tooling under cmd/tools*.go. Kubernetes kind nodes are supported as a specialized node implementation at nodes/k8s_kind/k8s_kind.go, rather than as the primary execution platform.
Main entry points: clab.go Lab coordinator · runtime.go Container runtime · root.go Lifecycle commands · deploy.go Deploy pipeline
Components 20
CLI surface
- CLI entrypoint
main.go - Lifecycle commands · Deploy, apply, destroy, exec, graph, and generate commands.
cmd/root.go
Lab orchestration
- Topology processing · Loads configuration and reconciles desired topology.
core/topology.go - Lab coordinator · Central object coordinating nodes, links, runtime, state, and lifecycle options.
core/clab.go - Deploy pipeline · Stages node and link provisioning.
core/deploy.go - Apply reconciliation · Moves an existing lab toward desired topology.
core/apply.go - Start / stop / destroy
core/destroy.go - Dependency manager · Orders dependent components and deployment stages.
core/dependency_manager/dependency_manager.go
Execution adapters
- Node registry · Selects node implementations from topology kind.
nodes/node_registry.go - Node-kind implementations · Vendor, Linux, VM, and kind-specific boot and interface behavior.
nodes/node.go - Runtime assembly · Registers supported container runtimes.
runtime/all/all.go - Container runtime · Abstracts containers, networks, events, auth, and firewall operations.
runtime/runtime.go - Docker runtime
runtime/docker/docker.go - Podman runtime
runtime/podman/podman.go - Link realization · Converts logical links into namespace and host connectivity.
links/apply.go - Linux host networking · Container engine plus Linux container and network primitives.
State and outputs
- Lab runtime state · Supports lifecycle operations and reconciliation.
core/state.go - Operational artifacts · Inventory, SSH configuration, and resolved-topology graph output.
core/inventory.go - Certificate provisioning · Local CA and certificate storage for lab deployment.
core/cert.go - Event streaming
core/events/stream.go
Connections 25
- CLI entrypoint → Lifecycle commands: launches
- Lifecycle commands → Topology processing: loads input
- Lifecycle commands → Deploy pipeline: deploy
- Lifecycle commands → Apply reconciliation: apply
- Lifecycle commands → Start / stop / destroy: operates
- Topology processing → Lab coordinator: builds desired lab
- Deploy pipeline → Lab coordinator: provisions
- Apply reconciliation → Lab coordinator: reconciles
- Start / stop / destroy → Lab coordinator: controls
- Lab coordinator → Dependency manager: orders stages
- Dependency manager → Node registry: provisions nodes
- Dependency manager → Link realization: provisions links
- Node registry → Node-kind implementations: resolves kind
- Node-kind implementations → Container runtime: uses
- Lab coordinator → Runtime assembly: selects runtime
- Runtime assembly → Container runtime: provides interface
- Container runtime → Docker runtime: implemented by
- Container runtime → Podman runtime: implemented by
- Container runtime → Linux host networking: manages
- Link realization → Linux host networking: creates connectivity
- Lab coordinator → Lab runtime state: persists
- Apply reconciliation → Lab runtime state: compares existing state
- Deploy pipeline → Operational artifacts: generates
- Deploy pipeline → Certificate provisioning: provisions
- Container runtime → Event streaming: streams events