How containerd/cgroups works
Main Components
• cgroup2 Package – Provides the API for cgroups v2 (unified), with similar operations as cgroup1 (creation, loading, deletion, process management, etc.) but tailored to the unified hierarchy and systemd integration.
• Command-line Tool (cgctl) – Located under the “cmd/cgctl” directory. This is a CLI front-end that enables users to interact with the cgroups functionality. It leverages the underlying libraries (cgroup1 or cgroup2) based on the runtime mode.
• Testing and Utility Components – Represented by the various “*_test.go” files and the “utils.go” scripts. Though not major architectural blocks, these demonstrate the modularity and testability of the design.
• CI/CD and Build Configurations – While not part of runtime architecture, the “.github” workflows (ci.yml and dependabot.yml) and files like Makefile indicate DevOps practices that ensure code quality.
Out the Relationships and Interactions
• That external consumers (other containerd components or end-user commands using cgctl) interface with the CLI tool or call into the library’s public APIs.
• The CLI tool (cgctl) should be shown as a consumer that delegates work to either the cgroup1 or cgroup2 components based on the detected runtime environment.
• Both cgroup packages abstract operations against the underlying Linux kernel interfaces (e.g., interacting with filesystem paths like /sys/fs/cgroup) and adhere to the OCI runtime-spec.
• That there is an exchange of resource configuration structures (for example, using LinuxResources from the OCI specification) when creating or updating cgroups.
Architectural Patterns and Design Principles
• Separation of Concerns – Note how the library splits functionality between two main packages (cgroup1 and cgroup2) based on cgroups version.
• Abstraction and Extensibility – The use of interfaces in Go (implied by the design) to allow easy addition of new cgroup types or resource operations.
• Command Pattern – The command-line tool (cgctl) acts as an orchestrator, parsing commands and triggering the appropriate operations.
• Testability – The inclusion of extensive tests (with separate files for various operations) underscores the modular design.
Relevant Technologies and Libraries
• Go Language – The entire project is written in Go.
• OCI Runtime-Spec – As a guide for resource definitions and overall structure.
• Systemd Integration – Particularly in the cgroup2 package for operations that involve systemd slices and unified modes.
• CI/CD Infrastructure – GitHub Actions for automated testing and dependency management.
• Protobuf – The “metrics.proto” file in both cgroup1 and cgroup2 stats directories suggests that some metrics may be serialized/deserialized using protobuf.