Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libct/configs: move cgroup configs to libct/cgroups #4472

Merged
merged 5 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions libcontainer/cgroups/cgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package cgroups

import (
"errors"

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

var (
Expand All @@ -21,8 +19,8 @@ var (
// [github.com/opencontainers/runc/libcontainer/cgroups/devices]
// package is imported, it is set to nil, so cgroup managers can't
// manage devices.
DevicesSetV1 func(path string, r *configs.Resources) error
DevicesSetV2 func(path string, r *configs.Resources) error
DevicesSetV1 func(path string, r *Resources) error
DevicesSetV2 func(path string, r *Resources) error
)

type Manager interface {
Expand All @@ -42,7 +40,7 @@ type Manager interface {
GetStats() (*Stats, error)

// Freeze sets the freezer cgroup to the specified state.
Freeze(state configs.FreezerState) error
Freeze(state FreezerState) error

// Destroy removes cgroup.
Destroy() error
Expand All @@ -54,7 +52,7 @@ type Manager interface {
// Set sets cgroup resources parameters/limits. If the argument is nil,
// the resources specified during Manager creation (or the previous call
// to Set) are used.
Set(r *configs.Resources) error
Set(r *Resources) error

// GetPaths returns cgroup path(s) to save in a state file in order to
// restore later.
Expand All @@ -67,10 +65,10 @@ type Manager interface {
GetPaths() map[string]string

// GetCgroups returns the cgroup data as configured.
GetCgroups() (*configs.Cgroup, error)
GetCgroups() (*Cgroup, error)

// GetFreezerState retrieves the current FreezerState of the cgroup.
GetFreezerState() (configs.FreezerState, error)
GetFreezerState() (FreezerState, error)

// Exists returns whether the cgroup path exists or not.
Exists() bool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package configs
package cgroups

import "fmt"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package configs
package cgroups

type HugepageLimit struct {
// which type of hugepage to limit.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package configs
package cgroups

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package configs
package cgroups

import (
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package configs
package cgroups

// LinuxRdma for Linux cgroup 'rdma' resource management (Linux 4.11)
type LinuxRdma struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build !linux

package configs
package cgroups

// Cgroup holds properties of a cgroup on Linux
// TODO Windows: This can ultimately be entirely factored out on Windows as
Expand Down
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
19 changes: 9 additions & 10 deletions libcontainer/cgroups/devices/systemd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"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 +27,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 +46,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 +123,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 +139,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 +261,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
3 changes: 1 addition & 2 deletions libcontainer/cgroups/devices/v1_test.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"
"github.com/opencontainers/runc/libcontainer/devices"
)

Expand All @@ -35,7 +34,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
41 changes: 20 additions & 21 deletions libcontainer/cgroups/fs/blkio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"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 +183,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 @@ -224,10 +223,10 @@ func TestBlkioSetWeightDevice(t *testing.T) {
weightDeviceFilename: weightDeviceBefore,
})
// Apply new configuration
wd := configs.NewWeightDevice(8, 0, 500, 0)
wd := cgroups.NewWeightDevice(8, 0, 500, 0)
weightDeviceAfter := wd.WeightString()
r := &configs.Resources{
BlkioWeightDevice: []*configs.WeightDevice{wd},
r := &cgroups.Resources{
BlkioWeightDevice: []*cgroups.WeightDevice{wd},
}
blkio := &BlkioGroup{}
if err := blkio.Set(path, r); err != nil {
Expand Down Expand Up @@ -255,8 +254,8 @@ func TestBlkioSetMultipleWeightDevice(t *testing.T) {
weightDeviceBefore = "8:0 400"
)

wd1 := configs.NewWeightDevice(8, 0, 500, 0)
wd2 := configs.NewWeightDevice(8, 16, 500, 0)
wd1 := cgroups.NewWeightDevice(8, 0, 500, 0)
wd2 := cgroups.NewWeightDevice(8, 16, 500, 0)
// we cannot actually set and check both because normal os.WriteFile
// when writing to cgroup file will overwrite the whole file content instead
// of updating it as the kernel is doing. Just check the second device
Expand All @@ -272,8 +271,8 @@ func TestBlkioSetMultipleWeightDevice(t *testing.T) {
blkio.weightDeviceFilename: weightDeviceBefore,
})

r := &configs.Resources{
BlkioWeightDevice: []*configs.WeightDevice{wd1, wd2},
r := &cgroups.Resources{
BlkioWeightDevice: []*cgroups.WeightDevice{wd1, wd2},
}
if err := blkio.Set(path, r); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -745,15 +744,15 @@ func TestBlkioSetThrottleReadBpsDevice(t *testing.T) {
throttleBefore = `8:0 1024`
)

td := configs.NewThrottleDevice(8, 0, 2048)
td := cgroups.NewThrottleDevice(8, 0, 2048)
throttleAfter := td.String()

writeFileContents(t, path, map[string]string{
"blkio.throttle.read_bps_device": throttleBefore,
})

r := &configs.Resources{
BlkioThrottleReadBpsDevice: []*configs.ThrottleDevice{td},
r := &cgroups.Resources{
BlkioThrottleReadBpsDevice: []*cgroups.ThrottleDevice{td},
}
blkio := &BlkioGroup{}
if err := blkio.Set(path, r); err != nil {
Expand All @@ -776,15 +775,15 @@ func TestBlkioSetThrottleWriteBpsDevice(t *testing.T) {
throttleBefore = `8:0 1024`
)

td := configs.NewThrottleDevice(8, 0, 2048)
td := cgroups.NewThrottleDevice(8, 0, 2048)
throttleAfter := td.String()

writeFileContents(t, path, map[string]string{
"blkio.throttle.write_bps_device": throttleBefore,
})

r := &configs.Resources{
BlkioThrottleWriteBpsDevice: []*configs.ThrottleDevice{td},
r := &cgroups.Resources{
BlkioThrottleWriteBpsDevice: []*cgroups.ThrottleDevice{td},
}
blkio := &BlkioGroup{}
if err := blkio.Set(path, r); err != nil {
Expand All @@ -807,15 +806,15 @@ func TestBlkioSetThrottleReadIOpsDevice(t *testing.T) {
throttleBefore = `8:0 1024`
)

td := configs.NewThrottleDevice(8, 0, 2048)
td := cgroups.NewThrottleDevice(8, 0, 2048)
throttleAfter := td.String()

writeFileContents(t, path, map[string]string{
"blkio.throttle.read_iops_device": throttleBefore,
})

r := &configs.Resources{
BlkioThrottleReadIOPSDevice: []*configs.ThrottleDevice{td},
r := &cgroups.Resources{
BlkioThrottleReadIOPSDevice: []*cgroups.ThrottleDevice{td},
}
blkio := &BlkioGroup{}
if err := blkio.Set(path, r); err != nil {
Expand All @@ -838,15 +837,15 @@ func TestBlkioSetThrottleWriteIOpsDevice(t *testing.T) {
throttleBefore = `8:0 1024`
)

td := configs.NewThrottleDevice(8, 0, 2048)
td := cgroups.NewThrottleDevice(8, 0, 2048)
throttleAfter := td.String()

writeFileContents(t, path, map[string]string{
"blkio.throttle.write_iops_device": throttleBefore,
})

r := &configs.Resources{
BlkioThrottleWriteIOPSDevice: []*configs.ThrottleDevice{td},
r := &cgroups.Resources{
BlkioThrottleWriteIOPSDevice: []*cgroups.ThrottleDevice{td},
}
blkio := &BlkioGroup{}
if err := blkio.Set(path, r); err != nil {
Expand Down
Loading
Loading