From 3c801b7dc7109b9b9cca2bcb900193716c55a213 Mon Sep 17 00:00:00 2001 From: nixpig <143995476+nixpig@users.noreply.github.com> Date: Sun, 15 Dec 2024 06:31:03 +0000 Subject: [PATCH] refactor: move cgroupsv1 out into package --- cgroups/cgroupsv1.go | 34 ++++++++++++++++++++++++++++++++++ container/container_init.go | 32 ++++++++++++++------------------ 2 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 cgroups/cgroupsv1.go diff --git a/cgroups/cgroupsv1.go b/cgroups/cgroupsv1.go new file mode 100644 index 0000000..18df0f9 --- /dev/null +++ b/cgroups/cgroupsv1.go @@ -0,0 +1,34 @@ +package cgroups + +import ( + "fmt" + + "github.com/containerd/cgroups/v3/cgroup1" + "github.com/opencontainers/runtime-spec/specs-go" +) + +func AddV1( + path string, + devices []specs.LinuxDeviceCgroup, + pid int, +) error { + + staticPath := cgroup1.StaticPath(path) + + cg, err := cgroup1.New( + staticPath, + &specs.LinuxResources{ + Devices: devices, + }, + ) + if err != nil { + return fmt.Errorf("create cgroups (path: %s): %w", path, err) + } + defer cg.Delete() + + if err := cg.Add(cgroup1.Process{Pid: pid}); err != nil { + return fmt.Errorf("add cgroups (path: %s, pid: %d): %w", path, pid, err) + } + + return nil +} diff --git a/container/container_init.go b/container/container_init.go index 768a18c..37286f3 100644 --- a/container/container_init.go +++ b/container/container_init.go @@ -10,7 +10,7 @@ import ( "strings" "syscall" - "github.com/containerd/cgroups/v3/cgroup1" + "github.com/nixpig/brownie/cgroups" "github.com/nixpig/brownie/namespace" "github.com/nixpig/brownie/terminal" "github.com/opencontainers/runtime-spec/specs-go" @@ -18,39 +18,35 @@ import ( func (c *Container) Init(reexec string, arg string) error { if err := c.ExecHooks("createRuntime"); err != nil { - return fmt.Errorf("execute createruntime hooks: %w", err) + return fmt.Errorf("execute createRuntime hooks: %w", err) } if err := c.ExecHooks("createContainer"); err != nil { - return fmt.Errorf("execute createcontainer hooks: %w", err) + return fmt.Errorf("execute createContainer hooks: %w", err) } useTerminal := c.Spec.Process != nil && c.Spec.Process.Terminal && c.Opts.ConsoleSocket != "" - var err error if useTerminal { - if c.State.ConsoleSocket, err = terminal.Setup(c.Rootfs(), c.Opts.ConsoleSocket); err != nil { + var err error + if c.State.ConsoleSocket, err = terminal.Setup( + c.Rootfs(), + c.Opts.ConsoleSocket, + ); err != nil { return err } } if c.Spec.Linux.CgroupsPath != "" && c.Spec.Linux.Resources != nil { - staticPath := cgroup1.StaticPath(c.Spec.Linux.CgroupsPath) - - cg, err := cgroup1.New( - staticPath, - &specs.LinuxResources{ - Devices: c.Spec.Linux.Resources.Devices, - }, - ) - if err != nil { - return fmt.Errorf("apply cgroups (path: %s): %w", c.Spec.Linux.CgroupsPath, err) + if err := cgroups.AddV1( + c.Spec.Linux.CgroupsPath, + c.Spec.Linux.Resources.Devices, + c.PID(), + ); err != nil { + return err } - defer cg.Delete() - - cg.Add(cgroup1.Process{Pid: c.PID()}) } // ---------------------------