Skip to content

Commit

Permalink
libct/*: 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 2db7837 commit 4923bbb
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 34 deletions.
8 changes: 4 additions & 4 deletions libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,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(cgroups.Thawed)
}
}
return nil
Expand Down Expand Up @@ -742,7 +742,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(cgroups.Frozen); err != nil {
return err
}
return c.state.transition(&pausedState{
Expand All @@ -766,7 +766,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(cgroups.Thawed); err != nil {
return err
}
return c.state.transition(&runningState{
Expand Down Expand Up @@ -886,7 +886,7 @@ func (c *Container) isPaused() (bool, error) {
if err != nil {
return false, err
}
return state == configs.Frozen, nil
return state == cgroups.Frozen, nil
}

func (c *Container) currentState() *State {
Expand Down
14 changes: 7 additions & 7 deletions libcontainer/container_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (m *mockCgroupManager) Apply(pid int) error {
return nil
}

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

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

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

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

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

type mockProcess struct {
Expand Down Expand Up @@ -243,8 +243,8 @@ func TestGetContainerStateAfterUpdate(t *testing.T) {
{Type: configs.NEWUTS},
{Type: configs.NEWIPC},
},
Cgroups: &configs.Cgroup{
Resources: &configs.Resources{
Cgroups: &cgroups.Cgroup{
Resources: &cgroups.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"

"github.com/opencontainers/runc/libcontainer/cgroups"
"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 == cgroups.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"

"github.com/opencontainers/runc/libcontainer/cgroups"
"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: &cgroups.Cgroup{
Resources: &cgroups.Resources{},
},
}
expectedState = &State{
Expand Down
6 changes: 3 additions & 3 deletions libcontainer/init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,12 +696,12 @@ func signalAllProcesses(m cgroups.Manager, s unix.Signal) error {
}
}

if err := m.Freeze(configs.Frozen); err != nil {
if err := m.Freeze(cgroups.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(cgroups.Thawed); err != nil {
logrus.Warn(err)
}
return err
Expand All @@ -712,7 +712,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(cgroups.Thawed); err != nil {
logrus.Warn(err)
}

Expand Down
4 changes: 2 additions & 2 deletions libcontainer/integration/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,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 = cgroups.Frozen
err = container.Set(*config)
}
ok(t, err)
Expand All @@ -486,7 +486,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 = cgroups.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"

"github.com/opencontainers/runc/libcontainer/cgroups"
"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: &cgroups.Cgroup{
Systemd: p.systemd,
Resources: &configs.Resources{
Resources: &cgroups.Resources{
MemorySwappiness: nil,
Devices: allowedDevices,
},
Expand Down
24 changes: 12 additions & 12 deletions libcontainer/specconv/spec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,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) (*cgroups.Cgroup, error) {
var (
myCgroupPath string

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

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

if useSystemdCgroup {
Expand Down Expand Up @@ -853,40 +853,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 := cgroups.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 := cgroups.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 := cgroups.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 := cgroups.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 := cgroups.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, &cgroups.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]cgroups.LinuxRdma, len(r.Rdma))
for k, v := range r.Rdma {
c.Resources.Rdma[k] = configs.LinuxRdma{
c.Resources.Rdma[k] = cgroups.LinuxRdma{
HcaHandles: v.HcaHandles,
HcaObjects: v.HcaObjects,
}
Expand All @@ -897,7 +897,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, &cgroups.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"

"github.com/opencontainers/runc/libcontainer/cgroups"
"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(cgroups.Thawed); err != nil {
return err
}
return destroy(p.c)
Expand Down

0 comments on commit 4923bbb

Please sign in to comment.