Skip to content

Commit

Permalink
libcontainer: switch to libct/cg/configs
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 2, 2024
1 parent 997e540 commit e90eb15
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 34 deletions.
9 changes: 5 additions & 4 deletions libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"golang.org/x/sys/unix"

"github.com/opencontainers/runc/libcontainer/cgroups"
cgConfig "github.com/opencontainers/runc/libcontainer/cgroups/configs"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/dmz"
"github.com/opencontainers/runc/libcontainer/intelrdt"
Expand Down Expand Up @@ -417,7 +418,7 @@ func (c *Container) signal(s os.Signal) error {
// does nothing until it's thawed. Only thaw the cgroup
// for SIGKILL.
if paused, _ := c.isPaused(); paused {
_ = c.cgroupManager.Freeze(configs.Thawed)
_ = c.cgroupManager.Freeze(cgConfig.Thawed)
}
}
return nil
Expand Down Expand Up @@ -742,7 +743,7 @@ func (c *Container) Pause() error {
}
switch status {
case Running, Created:
if err := c.cgroupManager.Freeze(configs.Frozen); err != nil {
if err := c.cgroupManager.Freeze(cgConfig.Frozen); err != nil {
return err
}
return c.state.transition(&pausedState{
Expand All @@ -766,7 +767,7 @@ func (c *Container) Resume() error {
if status != Paused {
return ErrNotPaused
}
if err := c.cgroupManager.Freeze(configs.Thawed); err != nil {
if err := c.cgroupManager.Freeze(cgConfig.Thawed); err != nil {
return err
}
return c.state.transition(&runningState{
Expand Down Expand Up @@ -886,7 +887,7 @@ func (c *Container) isPaused() (bool, error) {
if err != nil {
return false, err
}
return state == configs.Frozen, nil
return state == cgConfig.Frozen, nil
}

func (c *Container) currentState() *State {
Expand Down
15 changes: 8 additions & 7 deletions libcontainer/container_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/opencontainers/runc/libcontainer/cgroups"
cgConfig "github.com/opencontainers/runc/libcontainer/cgroups/configs"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/system"
)
Expand All @@ -32,7 +33,7 @@ func (m *mockCgroupManager) Apply(pid int) error {
return nil
}

func (m *mockCgroupManager) Set(_ *configs.Resources) error {
func (m *mockCgroupManager) Set(_ *cgConfig.Resources) error {
return nil
}

Expand All @@ -57,16 +58,16 @@ func (m *mockCgroupManager) Path(subsys string) string {
return m.paths[subsys]
}

func (m *mockCgroupManager) Freeze(state configs.FreezerState) error {
func (m *mockCgroupManager) Freeze(_ cgConfig.FreezerState) error {
return nil
}

func (m *mockCgroupManager) GetCgroups() (*configs.Cgroup, error) {
func (m *mockCgroupManager) GetCgroups() (*cgConfig.Cgroup, error) {
return nil, nil
}

func (m *mockCgroupManager) GetFreezerState() (configs.FreezerState, error) {
return configs.Thawed, nil
func (m *mockCgroupManager) GetFreezerState() (cgConfig.FreezerState, error) {
return cgConfig.Thawed, nil
}

type mockProcess struct {
Expand Down Expand Up @@ -243,8 +244,8 @@ func TestGetContainerStateAfterUpdate(t *testing.T) {
{Type: configs.NEWUTS},
{Type: configs.NEWIPC},
},
Cgroups: &configs.Cgroup{
Resources: &configs.Resources{
Cgroups: &cgConfig.Cgroup{
Resources: &cgConfig.Resources{
Memory: 1024,
},
},
Expand Down
3 changes: 2 additions & 1 deletion libcontainer/factory_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
securejoin "github.com/cyphar/filepath-securejoin"
"golang.org/x/sys/unix"

cgConfig "github.com/opencontainers/runc/libcontainer/cgroups/configs"
"github.com/opencontainers/runc/libcontainer/cgroups/manager"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/configs/validate"
Expand Down Expand Up @@ -82,7 +83,7 @@ func Create(root, id string, config *configs.Config) (*Container, error) {
if err != nil {
return nil, fmt.Errorf("unable to get cgroup freezer state: %w", err)
}
if st == configs.Frozen {
if st == cgConfig.Frozen {
return nil, errors.New("container's cgroup unexpectedly frozen")
}

Expand Down
5 changes: 3 additions & 2 deletions libcontainer/factory_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"
"testing"

cgConfig "github.com/opencontainers/runc/libcontainer/cgroups/configs"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/utils"
"github.com/opencontainers/runtime-spec/specs-go"
Expand Down Expand Up @@ -43,8 +44,8 @@ func TestFactoryLoadContainer(t *testing.T) {
expectedConfig = &configs.Config{
Rootfs: "/mycontainer/root",
Hooks: expectedHooks,
Cgroups: &configs.Cgroup{
Resources: &configs.Resources{},
Cgroups: &cgConfig.Cgroup{
Resources: &cgConfig.Resources{},
},
}
expectedState = &State{
Expand Down
7 changes: 4 additions & 3 deletions libcontainer/init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/opencontainers/runc/libcontainer/capabilities"
"github.com/opencontainers/runc/libcontainer/cgroups"
cgConfig "github.com/opencontainers/runc/libcontainer/cgroups/configs"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/system"
"github.com/opencontainers/runc/libcontainer/utils"
Expand Down Expand Up @@ -696,12 +697,12 @@ func signalAllProcesses(m cgroups.Manager, s unix.Signal) error {
}
}

if err := m.Freeze(configs.Frozen); err != nil {
if err := m.Freeze(cgConfig.Frozen); err != nil {
logrus.Warn(err)
}
pids, err := m.GetAllPids()
if err != nil {
if err := m.Freeze(configs.Thawed); err != nil {
if err := m.Freeze(cgConfig.Thawed); err != nil {
logrus.Warn(err)
}
return err
Expand All @@ -712,7 +713,7 @@ func signalAllProcesses(m cgroups.Manager, s unix.Signal) error {
logrus.Warnf("kill %d: %v", pid, err)
}
}
if err := m.Freeze(configs.Thawed); err != nil {
if err := m.Freeze(cgConfig.Thawed); err != nil {
logrus.Warn(err)
}

Expand Down
5 changes: 3 additions & 2 deletions libcontainer/integration/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/opencontainers/runc/libcontainer"
"github.com/opencontainers/runc/libcontainer/cgroups"
cgConfig "github.com/opencontainers/runc/libcontainer/cgroups/configs"
"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/internal/userns"
Expand Down Expand Up @@ -472,7 +473,7 @@ func testFreeze(t *testing.T, withSystemd bool, useSet bool) {
if !useSet {
err = container.Pause()
} else {
config.Cgroups.Resources.Freezer = configs.Frozen
config.Cgroups.Resources.Freezer = cgConfig.Frozen
err = container.Set(*config)
}
ok(t, err)
Expand All @@ -486,7 +487,7 @@ func testFreeze(t *testing.T, withSystemd bool, useSet bool) {
if !useSet {
err = container.Resume()
} else {
config.Cgroups.Resources.Freezer = configs.Thawed
config.Cgroups.Resources.Freezer = cgConfig.Thawed
err = container.Set(*config)
}
ok(t, err)
Expand Down
5 changes: 3 additions & 2 deletions libcontainer/integration/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

cgConfig "github.com/opencontainers/runc/libcontainer/cgroups/configs"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/devices"
"github.com/opencontainers/runc/libcontainer/specconv"
Expand Down Expand Up @@ -99,9 +100,9 @@ func newTemplateConfig(t testing.TB, p *tParam) *configs.Config {
{Type: configs.NEWPID},
{Type: configs.NEWNET},
}),
Cgroups: &configs.Cgroup{
Cgroups: &cgConfig.Cgroup{
Systemd: p.systemd,
Resources: &configs.Resources{
Resources: &cgConfig.Resources{
MemorySwappiness: nil,
Devices: allowedDevices,
},
Expand Down
25 changes: 13 additions & 12 deletions libcontainer/specconv/spec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
dbus "github.com/godbus/dbus/v5"
"github.com/opencontainers/runc/libcontainer/cgroups"
cgConfig "github.com/opencontainers/runc/libcontainer/cgroups/configs"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/devices"
"github.com/opencontainers/runc/libcontainer/internal/userns"
Expand Down Expand Up @@ -697,7 +698,7 @@ func initSystemdProps(spec *specs.Spec) ([]systemdDbus.Property, error) {
return sp, nil
}

func CreateCgroupConfig(opts *CreateOpts, defaultDevs []*devices.Device) (*configs.Cgroup, error) {
func CreateCgroupConfig(opts *CreateOpts, defaultDevs []*devices.Device) (*cgConfig.Cgroup, error) {
var (
myCgroupPath string

Expand All @@ -706,10 +707,10 @@ func CreateCgroupConfig(opts *CreateOpts, defaultDevs []*devices.Device) (*confi
name = opts.CgroupName
)

c := &configs.Cgroup{
c := &cgConfig.Cgroup{
Systemd: useSystemdCgroup,
Rootless: opts.RootlessCgroups,
Resources: &configs.Resources{},
Resources: &cgConfig.Resources{},
}

if useSystemdCgroup {
Expand Down Expand Up @@ -853,40 +854,40 @@ func CreateCgroupConfig(opts *CreateOpts, defaultDevs []*devices.Device) (*confi
if wd.LeafWeight != nil {
leafWeight = *wd.LeafWeight
}
weightDevice := configs.NewWeightDevice(wd.Major, wd.Minor, weight, leafWeight)
weightDevice := cgConfig.NewWeightDevice(wd.Major, wd.Minor, weight, leafWeight)
c.Resources.BlkioWeightDevice = append(c.Resources.BlkioWeightDevice, weightDevice)
}
for _, td := range r.BlockIO.ThrottleReadBpsDevice {
rate := td.Rate
throttleDevice := configs.NewThrottleDevice(td.Major, td.Minor, rate)
throttleDevice := cgConfig.NewThrottleDevice(td.Major, td.Minor, rate)
c.Resources.BlkioThrottleReadBpsDevice = append(c.Resources.BlkioThrottleReadBpsDevice, throttleDevice)
}
for _, td := range r.BlockIO.ThrottleWriteBpsDevice {
rate := td.Rate
throttleDevice := configs.NewThrottleDevice(td.Major, td.Minor, rate)
throttleDevice := cgConfig.NewThrottleDevice(td.Major, td.Minor, rate)
c.Resources.BlkioThrottleWriteBpsDevice = append(c.Resources.BlkioThrottleWriteBpsDevice, throttleDevice)
}
for _, td := range r.BlockIO.ThrottleReadIOPSDevice {
rate := td.Rate
throttleDevice := configs.NewThrottleDevice(td.Major, td.Minor, rate)
throttleDevice := cgConfig.NewThrottleDevice(td.Major, td.Minor, rate)
c.Resources.BlkioThrottleReadIOPSDevice = append(c.Resources.BlkioThrottleReadIOPSDevice, throttleDevice)
}
for _, td := range r.BlockIO.ThrottleWriteIOPSDevice {
rate := td.Rate
throttleDevice := configs.NewThrottleDevice(td.Major, td.Minor, rate)
throttleDevice := cgConfig.NewThrottleDevice(td.Major, td.Minor, rate)
c.Resources.BlkioThrottleWriteIOPSDevice = append(c.Resources.BlkioThrottleWriteIOPSDevice, throttleDevice)
}
}
for _, l := range r.HugepageLimits {
c.Resources.HugetlbLimit = append(c.Resources.HugetlbLimit, &configs.HugepageLimit{
c.Resources.HugetlbLimit = append(c.Resources.HugetlbLimit, &cgConfig.HugepageLimit{
Pagesize: l.Pagesize,
Limit: l.Limit,
})
}
if len(r.Rdma) > 0 {
c.Resources.Rdma = make(map[string]configs.LinuxRdma, len(r.Rdma))
c.Resources.Rdma = make(map[string]cgConfig.LinuxRdma, len(r.Rdma))
for k, v := range r.Rdma {
c.Resources.Rdma[k] = configs.LinuxRdma{
c.Resources.Rdma[k] = cgConfig.LinuxRdma{
HcaHandles: v.HcaHandles,
HcaObjects: v.HcaObjects,
}
Expand All @@ -897,7 +898,7 @@ func CreateCgroupConfig(opts *CreateOpts, defaultDevs []*devices.Device) (*confi
c.Resources.NetClsClassid = *r.Network.ClassID
}
for _, m := range r.Network.Priorities {
c.Resources.NetPrioIfpriomap = append(c.Resources.NetPrioIfpriomap, &configs.IfPrioMap{
c.Resources.NetPrioIfpriomap = append(c.Resources.NetPrioIfpriomap, &cgConfig.IfPrioMap{
Interface: m.Name,
Priority: int64(m.Priority),
})
Expand Down
3 changes: 2 additions & 1 deletion libcontainer/state_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"

cgConfig "github.com/opencontainers/runc/libcontainer/cgroups/configs"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -185,7 +186,7 @@ func (p *pausedState) destroy() error {
if p.c.hasInit() {
return ErrPaused
}
if err := p.c.cgroupManager.Freeze(configs.Thawed); err != nil {
if err := p.c.cgroupManager.Freeze(cgConfig.Thawed); err != nil {
return err
}
return destroy(p.c)
Expand Down

0 comments on commit e90eb15

Please sign in to comment.