Skip to content
Draft
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
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ issues:
# We dot-import ginkgo and gomega in some tests. Silence any related errors.
- path: 'pkg/adaptation|pkg/runtime-tools/generate|pkg/net/multiplex'
text: "dot-imports:"
# Ignore deprecation errors related to us haveing adjustment support for
# obsoleted Prestart OCI hook and kernel memory limit.
- path: 'pkg/api/hooks.go'
linters:
- staticcheck
text: "is deprecated"

run:
timeout: 2m
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/moby/sys/mountinfo v0.6.2
github.com/onsi/ginkgo/v2 v2.19.1
github.com/onsi/gomega v1.34.0
github.com/opencontainers/runtime-spec v1.1.0
github.com/opencontainers/runtime-spec v1.2.2-0.20250401095657-e935f995dd67
github.com/opencontainers/runtime-tools v0.9.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ github.com/onsi/ginkgo/v2 v2.19.1/go.mod h1:O3DtEWQkPa/F7fBMgmZQKKsluAy8pd3rEQdr
github.com/onsi/gomega v1.34.0 h1:eSSPsPNp6ZpsG8X1OVmOTxig+CblTc4AxpPBykhe2Os=
github.com/onsi/gomega v1.34.0/go.mod h1:MIKI8c+f+QLWk+hxbePD4i0LMJSExPaZOVfkoex4cAo=
github.com/opencontainers/runtime-spec v1.0.3-0.20220825212826-86290f6a00fb/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
github.com/opencontainers/runtime-spec v1.1.0 h1:HHUyrt9mwHUjtasSbXSMvs4cyFxh+Bll4AjJ9odEGpg=
github.com/opencontainers/runtime-spec v1.1.0/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
github.com/opencontainers/runtime-spec v1.2.2-0.20250401095657-e935f995dd67 h1:Q+KewUGTMamIe6Q39xCD/T1NC1POmaTlWnhjikCrZHA=
github.com/opencontainers/runtime-spec v1.2.2-0.20250401095657-e935f995dd67/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
github.com/opencontainers/runtime-tools v0.0.0-20221026201742-946c877fa809 h1:WSwkWIIS4s+E/dPF6HuVZ/hnq1WfXN371eESjREnU8k=
github.com/opencontainers/runtime-tools v0.0.0-20221026201742-946c877fa809/go.mod h1:BRHJJd0E+cx42OybVYSgUvZmU0B8P9gZuRXlZUP7TKI=
github.com/opencontainers/selinux v1.9.1 h1:b4VPEF3O5JLZgdTDBmGepaaIbAo0GqoF6EBRq5f/g3Y=
Expand Down
35 changes: 35 additions & 0 deletions pkg/adaptation/adaptation_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,17 @@ var _ = Describe("Plugin container creation adjustments", func() {
},
)

case "linux net device":
if overwrite {
a.RemoveLinuxNetDevice("hostIf")
}
a.AddLinuxNetDevice(
"hostIf",
&api.LinuxNetDevice{
Name: "containerIf",
},
)

case "resources/cpu":
a.SetLinuxCPUShares(123)
a.SetLinuxCPUQuota(456)
Expand Down Expand Up @@ -700,6 +711,17 @@ var _ = Describe("Plugin container creation adjustments", func() {
},
},
),
Entry("adjust linux net devices", "linux net device",
&api.ContainerAdjustment{
Linux: &api.LinuxContainerAdjustment{
NetDevices: map[string]*api.LinuxNetDevice{
"hostIf": {
Name: "containerIf",
},
},
},
},
),
Entry("adjust CPU resources", "resources/cpu",
&api.ContainerAdjustment{
Linux: &api.LinuxContainerAdjustment{
Expand Down Expand Up @@ -921,6 +943,19 @@ var _ = Describe("Plugin container creation adjustments", func() {
},
),
Entry("adjust resources", "resources/classes", false, true, nil),
Entry("adjust linux net devices", "linux net device", true, false,
&api.ContainerAdjustment{
Linux: &api.LinuxContainerAdjustment{
NetDevices: map[string]*api.LinuxNetDevice{
"-hostIf": nil,
"hostIf": {
Name: "containerIf",
},
},
},
},
),
Entry("adjust linux net devices (conflicts)", "linux net device", false, true, nil),
)
})

Expand Down
1 change: 1 addition & 0 deletions pkg/adaptation/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type (
LinuxMemory = api.LinuxMemory
LinuxDevice = api.LinuxDevice
LinuxDeviceCgroup = api.LinuxDeviceCgroup
LinuxNetDevice = api.LinuxNetDevice
CDIDevice = api.CDIDevice
HugepageLimit = api.HugepageLimit
Hooks = api.Hooks
Expand Down
42 changes: 42 additions & 0 deletions pkg/adaptation/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func collectCreateContainerResult(request *CreateContainerRequest) *result {
if request.Container.Linux.Resources.Unified == nil {
request.Container.Linux.Resources.Unified = map[string]string{}
}
if request.Container.Linux.NetDevices == nil {
request.Container.Linux.NetDevices = map[string]*LinuxNetDevice{}
}

return &result{
request: resultRequest{
Expand All @@ -99,6 +102,7 @@ func collectCreateContainerResult(request *CreateContainerRequest) *result {
HugepageLimits: []*HugepageLimit{},
Unified: map[string]string{},
},
NetDevices: map[string]*LinuxNetDevice{},
},
},
},
Expand Down Expand Up @@ -221,6 +225,9 @@ func (r *result) adjust(rpl *ContainerAdjustment, plugin string) error {
if err := r.adjustOomScoreAdj(rpl.Linux.OomScoreAdj, plugin); err != nil {
return err
}
if err := r.adjustLinuxNetDevices(rpl.Linux.NetDevices, plugin); err != nil {
return err
}
}
if err := r.adjustRlimits(rpl.Rlimits, plugin); err != nil {
return err
Expand Down Expand Up @@ -786,6 +793,41 @@ func (r *result) adjustRlimits(rlimits []*POSIXRlimit, plugin string) error {
return nil
}

func (r *result) adjustLinuxNetDevices(devices map[string]*LinuxNetDevice, plugin string) error {
if len(devices) == 0 {
return nil
}

create, id := r.request.create, r.request.create.Container.Id
del := map[string]struct{}{}
for k := range devices {
if key, marked := IsMarkedForRemoval(k); marked {
del[key] = struct{}{}
delete(devices, k)
}
}

for k, v := range devices {
if _, ok := del[k]; ok {
r.owners.ClearLinuxNetDevice(id, k, plugin)
delete(create.Container.Linux.NetDevices, k)
r.reply.adjust.Linux.NetDevices[MarkForRemoval(k)] = nil
}
if err := r.owners.ClaimLinuxNetDevice(id, k, plugin); err != nil {
return err
}
create.Container.Linux.NetDevices[k] = v
r.reply.adjust.Linux.NetDevices[k] = v
delete(del, k)
}

for k := range del {
r.reply.adjust.Linux.NetDevices[MarkForRemoval(k)] = nil
}

return nil
}

func (r *result) updateResources(reply, u *ContainerUpdate, plugin string) error {
if u.Linux == nil || u.Linux.Resources == nil {
return nil
Expand Down
25 changes: 25 additions & 0 deletions pkg/api/adjustment.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@ func (a *ContainerAdjustment) AddCDIDevice(d *CDIDevice) {
a.CDIDevices = append(a.CDIDevices, d) // TODO: should we dup d here ?
}

// AddLinuxNetDevice records the addition of the given network device to a container.
func (a *ContainerAdjustment) AddLinuxNetDevice(hostDev string, d *LinuxNetDevice) {
if d == nil {
return
}
a.initLinuxNetDevices()
a.Linux.NetDevices[hostDev] = d
}

// RemoveNetLinuxDevice records the removal of a network device from a container.
// Normally it is an error for a plugin to try and alter a network device
// touched by another container. However, this is not an error if
// the plugin removes that device prior to touching it.
func (a *ContainerAdjustment) RemoveLinuxNetDevice(hostDev string) {
a.initLinuxNetDevices()
a.Linux.NetDevices[MarkForRemoval(hostDev)] = nil
}

// SetLinuxMemoryLimit records setting the memory limit for a container.
func (a *ContainerAdjustment) SetLinuxMemoryLimit(value int64) {
a.initLinuxResourcesMemory()
Expand Down Expand Up @@ -345,3 +363,10 @@ func (a *ContainerAdjustment) initLinuxResourcesUnified() {
a.Linux.Resources.Unified = make(map[string]string)
}
}

func (a *ContainerAdjustment) initLinuxNetDevices() {
a.initLinux()
if a.Linux.NetDevices == nil {
a.Linux.NetDevices = make(map[string]*LinuxNetDevice)
}
}
Loading
Loading