Skip to content

Commit

Permalink
libct/cgroups/*: switch from configs to cgroups
Browse files Browse the repository at this point in the history
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Dec 12, 2024
1 parent a050555 commit 2db7837
Show file tree
Hide file tree
Showing 52 changed files with 222 additions and 252 deletions.
4 changes: 2 additions & 2 deletions libcontainer/cgroups/devices/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
"github.com/godbus/dbus/v5"
"github.com/sirupsen/logrus"

"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/devices"
)

// systemdProperties takes the configured device rules and generates a
// corresponding set of systemd properties to configure the devices correctly.
func systemdProperties(r *configs.Resources, sdVer int) ([]systemdDbus.Property, error) {
func systemdProperties(r *cgroups.Resources, sdVer int) ([]systemdDbus.Property, error) {
if r.SkipDevices {
return nil, nil
}
Expand Down
20 changes: 10 additions & 10 deletions libcontainer/cgroups/devices/systemd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"strings"
"testing"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/devices"
)

Expand All @@ -28,11 +28,11 @@ func TestPodSkipDevicesUpdate(t *testing.T) {
}

podName := "system-runc_test_pod" + t.Name() + ".slice"
podConfig := &configs.Cgroup{
podConfig := &cgroups.Cgroup{
Systemd: true,
Parent: "system.slice",
Name: podName,
Resources: &configs.Resources{
Resources: &cgroups.Resources{
PidsLimit: 42,
Memory: 32 * 1024 * 1024,
SkipDevices: true,
Expand All @@ -47,11 +47,11 @@ func TestPodSkipDevicesUpdate(t *testing.T) {
t.Fatal(err)
}

containerConfig := &configs.Cgroup{
containerConfig := &cgroups.Cgroup{
Parent: podName,
ScopePrefix: "test",
Name: "PodSkipDevicesUpdate",
Resources: &configs.Resources{
Resources: &cgroups.Resources{
Devices: []*devices.Rule{
// Allow access to /dev/null.
{
Expand Down Expand Up @@ -124,10 +124,10 @@ func testSkipDevices(t *testing.T, skipDevices bool, expected []string) {
t.Skip("Test requires root.")
}

podConfig := &configs.Cgroup{
podConfig := &cgroups.Cgroup{
Parent: "system.slice",
Name: "system-runc_test_pods.slice",
Resources: &configs.Resources{
Resources: &cgroups.Resources{
SkipDevices: skipDevices,
},
}
Expand All @@ -140,11 +140,11 @@ func testSkipDevices(t *testing.T, skipDevices bool, expected []string) {
t.Fatal(err)
}

config := &configs.Cgroup{
config := &cgroups.Cgroup{
Parent: "system-runc_test_pods.slice",
ScopePrefix: "test",
Name: "SkipDevices",
Resources: &configs.Resources{
Resources: &cgroups.Resources{
Devices: []*devices.Rule{
// Allow access to /dev/full only.
{
Expand Down Expand Up @@ -262,7 +262,7 @@ func BenchmarkFindDeviceGroup(b *testing.B) {
}
}

func newManager(t *testing.T, config *configs.Cgroup) (m cgroups.Manager) {
func newManager(t *testing.T, config *cgroups.Cgroup) (m cgroups.Manager) {
t.Helper()
var err error

Expand Down
3 changes: 1 addition & 2 deletions libcontainer/cgroups/devices/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (

"github.com/moby/sys/userns"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/devices"
)

var testingSkipFinalCheck bool

func setV1(path string, r *configs.Resources) error {
func setV1(path string, r *cgroups.Resources) error {
if userns.RunningInUserNS() || r.SkipDevices {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/cgroups/devices/v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

"github.com/moby/sys/userns"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/devices"
)

Expand All @@ -35,7 +35,7 @@ func TestSetV1Allow(t *testing.T) {
}
}

r := &configs.Resources{
r := &cgroups.Resources{
Devices: []*devices.Rule{
{
Type: devices.CharDevice,
Expand Down
6 changes: 3 additions & 3 deletions libcontainer/cgroups/devices/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/moby/sys/userns"
"golang.org/x/sys/unix"

"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/devices"
)

Expand All @@ -27,7 +27,7 @@ func isRWM(perms devices.Permissions) bool {

// This is similar to the logic applied in crun for handling errors from bpf(2)
// <https://github.com/containers/crun/blob/0.17/src/libcrun/cgroup.c#L2438-L2470>.
func canSkipEBPFError(r *configs.Resources) bool {
func canSkipEBPFError(r *cgroups.Resources) bool {
// If we're running in a user namespace we can ignore eBPF rules because we
// usually cannot use bpf(2), as well as rootless containers usually don't
// have the necessary privileges to mknod(2) device inodes or access
Expand All @@ -51,7 +51,7 @@ func canSkipEBPFError(r *configs.Resources) bool {
return true
}

func setV2(dirPath string, r *configs.Resources) error {
func setV2(dirPath string, r *cgroups.Resources) error {
if r.SkipDevices {
return nil
}
Expand Down
5 changes: 2 additions & 3 deletions libcontainer/cgroups/fs/blkio.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/configs"
)

type BlkioGroup struct {
Expand All @@ -20,11 +19,11 @@ func (s *BlkioGroup) Name() string {
return "blkio"
}

func (s *BlkioGroup) Apply(path string, _ *configs.Resources, pid int) error {
func (s *BlkioGroup) Apply(path string, _ *cgroups.Resources, pid int) error {
return apply(path, pid)
}

func (s *BlkioGroup) Set(path string, r *configs.Resources) error {
func (s *BlkioGroup) Set(path string, r *cgroups.Resources) error {
s.detectWeightFilenames(path)
if r.BlkioWeight != 0 {
if err := cgroups.WriteFile(path, s.weightFilename, strconv.FormatUint(uint64(r.BlkioWeight), 10)); err != nil {
Expand Down
16 changes: 8 additions & 8 deletions libcontainer/cgroups/fs/blkio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"strconv"
"testing"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
)

const (
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestBlkioSetWeight(t *testing.T) {
weightFilename: strconv.Itoa(weightBefore),
})
// Apply new configuration
r := &configs.Resources{
r := &cgroups.Resources{
BlkioWeight: weightAfter,
}
blkio := &BlkioGroup{}
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestBlkioSetWeightDevice(t *testing.T) {
// Apply new configuration
wd := configs.NewWeightDevice(8, 0, 500, 0)
weightDeviceAfter := wd.WeightString()
r := &configs.Resources{
r := &cgroups.Resources{
BlkioWeightDevice: []*configs.WeightDevice{wd},
}
blkio := &BlkioGroup{}
Expand Down Expand Up @@ -272,7 +272,7 @@ func TestBlkioSetMultipleWeightDevice(t *testing.T) {
blkio.weightDeviceFilename: weightDeviceBefore,
})

r := &configs.Resources{
r := &cgroups.Resources{
BlkioWeightDevice: []*configs.WeightDevice{wd1, wd2},
}
if err := blkio.Set(path, r); err != nil {
Expand Down Expand Up @@ -752,7 +752,7 @@ func TestBlkioSetThrottleReadBpsDevice(t *testing.T) {
"blkio.throttle.read_bps_device": throttleBefore,
})

r := &configs.Resources{
r := &cgroups.Resources{
BlkioThrottleReadBpsDevice: []*configs.ThrottleDevice{td},
}
blkio := &BlkioGroup{}
Expand Down Expand Up @@ -783,7 +783,7 @@ func TestBlkioSetThrottleWriteBpsDevice(t *testing.T) {
"blkio.throttle.write_bps_device": throttleBefore,
})

r := &configs.Resources{
r := &cgroups.Resources{
BlkioThrottleWriteBpsDevice: []*configs.ThrottleDevice{td},
}
blkio := &BlkioGroup{}
Expand Down Expand Up @@ -814,7 +814,7 @@ func TestBlkioSetThrottleReadIOpsDevice(t *testing.T) {
"blkio.throttle.read_iops_device": throttleBefore,
})

r := &configs.Resources{
r := &cgroups.Resources{
BlkioThrottleReadIOPSDevice: []*configs.ThrottleDevice{td},
}
blkio := &BlkioGroup{}
Expand Down Expand Up @@ -845,7 +845,7 @@ func TestBlkioSetThrottleWriteIOpsDevice(t *testing.T) {
"blkio.throttle.write_iops_device": throttleBefore,
})

r := &configs.Resources{
r := &cgroups.Resources{
BlkioThrottleWriteIOPSDevice: []*configs.ThrottleDevice{td},
}
blkio := &BlkioGroup{}
Expand Down
7 changes: 3 additions & 4 deletions libcontainer/cgroups/fs/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
"golang.org/x/sys/unix"
)

Expand All @@ -19,7 +18,7 @@ func (s *CpuGroup) Name() string {
return "cpu"
}

func (s *CpuGroup) Apply(path string, r *configs.Resources, pid int) error {
func (s *CpuGroup) Apply(path string, r *cgroups.Resources, pid int) error {
if err := os.MkdirAll(path, 0o755); err != nil {
return err
}
Expand All @@ -34,7 +33,7 @@ func (s *CpuGroup) Apply(path string, r *configs.Resources, pid int) error {
return cgroups.WriteCgroupProc(path, pid)
}

func (s *CpuGroup) SetRtSched(path string, r *configs.Resources) error {
func (s *CpuGroup) SetRtSched(path string, r *cgroups.Resources) error {
var period string
if r.CpuRtPeriod != 0 {
period = strconv.FormatUint(r.CpuRtPeriod, 10)
Expand Down Expand Up @@ -64,7 +63,7 @@ func (s *CpuGroup) SetRtSched(path string, r *configs.Resources) error {
return nil
}

func (s *CpuGroup) Set(path string, r *configs.Resources) error {
func (s *CpuGroup) Set(path string, r *cgroups.Resources) error {
if r.CpuShares != 0 {
shares := r.CpuShares
if err := cgroups.WriteFile(path, "cpu.shares", strconv.FormatUint(shares, 10)); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions libcontainer/cgroups/fs/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"strconv"
"testing"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
)

func TestCpuSetShares(t *testing.T) {
Expand All @@ -22,7 +22,7 @@ func TestCpuSetShares(t *testing.T) {
"cpu.shares": strconv.Itoa(sharesBefore),
})

r := &configs.Resources{
r := &cgroups.Resources{
CpuShares: sharesAfter,
}
cpu := &CpuGroup{}
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestCpuSetBandWidth(t *testing.T) {
"cpu.rt_period_us": strconv.Itoa(rtPeriodBefore),
})

r := &configs.Resources{
r := &cgroups.Resources{
CpuQuota: quotaAfter,
CpuBurst: &burstAfter,
CpuPeriod: periodAfter,
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestCpuSetRtSchedAtApply(t *testing.T) {
"cpu.rt_period_us": strconv.Itoa(rtPeriodBefore),
})

r := &configs.Resources{
r := &cgroups.Resources{
CpuRtRuntime: rtRuntimeAfter,
CpuRtPeriod: rtPeriodAfter,
}
Expand Down
5 changes: 2 additions & 3 deletions libcontainer/cgroups/fs/cpuacct.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
)

const (
Expand All @@ -34,11 +33,11 @@ func (s *CpuacctGroup) Name() string {
return "cpuacct"
}

func (s *CpuacctGroup) Apply(path string, _ *configs.Resources, pid int) error {
func (s *CpuacctGroup) Apply(path string, _ *cgroups.Resources, pid int) error {
return apply(path, pid)
}

func (s *CpuacctGroup) Set(_ string, _ *configs.Resources) error {
func (s *CpuacctGroup) Set(_ string, _ *cgroups.Resources) error {
return nil
}

Expand Down
9 changes: 4 additions & 5 deletions libcontainer/cgroups/fs/cpuset.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
)

type CpusetGroup struct{}
Expand All @@ -20,11 +19,11 @@ func (s *CpusetGroup) Name() string {
return "cpuset"
}

func (s *CpusetGroup) Apply(path string, r *configs.Resources, pid int) error {
func (s *CpusetGroup) Apply(path string, r *cgroups.Resources, pid int) error {
return s.ApplyDir(path, r, pid)
}

func (s *CpusetGroup) Set(path string, r *configs.Resources) error {
func (s *CpusetGroup) Set(path string, r *cgroups.Resources) error {
if r.CpusetCpus != "" {
if err := cgroups.WriteFile(path, "cpuset.cpus", r.CpusetCpus); err != nil {
return err
Expand Down Expand Up @@ -141,7 +140,7 @@ func (s *CpusetGroup) GetStats(path string, stats *cgroups.Stats) error {
return nil
}

func (s *CpusetGroup) ApplyDir(dir string, r *configs.Resources, pid int) error {
func (s *CpusetGroup) ApplyDir(dir string, r *cgroups.Resources, pid int) error {
// This might happen if we have no cpuset cgroup mounted.
// Just do nothing and don't fail.
if dir == "" {
Expand Down Expand Up @@ -237,7 +236,7 @@ func isEmptyCpuset(str string) bool {
return str == "" || str == "\n"
}

func (s *CpusetGroup) ensureCpusAndMems(path string, r *configs.Resources) error {
func (s *CpusetGroup) ensureCpusAndMems(path string, r *cgroups.Resources) error {
if err := s.Set(path, r); err != nil {
return err
}
Expand Down
Loading

0 comments on commit 2db7837

Please sign in to comment.