Skip to content

Commit

Permalink
refactor: move cgroupsv1 out into package
Browse files Browse the repository at this point in the history
  • Loading branch information
nixpig committed Dec 15, 2024
1 parent 72076be commit 3c801b7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
34 changes: 34 additions & 0 deletions cgroups/cgroupsv1.go
Original file line number Diff line number Diff line change
@@ -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
}
32 changes: 14 additions & 18 deletions container/container_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,43 @@ 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"
)

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()})
}

// ---------------------------
Expand Down

0 comments on commit 3c801b7

Please sign in to comment.