Skip to content
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ The following pieces of container metadata are available to plugins in NRI:
- RDT class
- Unified cgroup v2 parameter map
- Linux seccomp profile and policy
- Linux network devices
- container (init) process ID
- container (init process) exit status
- timestamp of container creation
Expand Down Expand Up @@ -257,6 +258,7 @@ container parameters:
- RDT class
- Unified cgroup v2 parameter map
- Linux seccomp policy
- Linux network devices
- Linux namespaces

### Container Updates
Expand Down
38 changes: 38 additions & 0 deletions pkg/adaptation/adaptation_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,17 @@ var _ = Describe("Plugin container creation adjustments", func() {
Class: api.IOPrioClass_IOPRIO_CLASS_NONE,
})

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 @@ -777,6 +788,19 @@ 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("clear I/O priority", "clear I/O priority",
&api.ContainerAdjustment{
Linux: &api.LinuxContainerAdjustment{
Expand Down Expand Up @@ -1045,7 +1069,21 @@ var _ = Describe("Plugin container creation adjustments", func() {
},
),
Entry("adjust resources", "resources/classes", false, true, nil),

Entry("adjust I/O priority (conflicts)", "I/O priority", 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 @@ -92,6 +92,7 @@ type (
LinuxDeviceCgroup = api.LinuxDeviceCgroup
LinuxIOPriority = api.LinuxIOPriority
LinuxSeccomp = api.LinuxSeccomp
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 @@ -82,6 +82,9 @@ func collectCreateContainerResult(request *CreateContainerRequest) *result {
if request.Container.Linux.Namespaces == nil {
request.Container.Linux.Namespaces = []*LinuxNamespace{}
}
if request.Container.Linux.NetDevices == nil {
request.Container.Linux.NetDevices = map[string]*LinuxNetDevice{}
}

return &result{
request: resultRequest{
Expand All @@ -104,6 +107,7 @@ func collectCreateContainerResult(request *CreateContainerRequest) *result {
Unified: map[string]string{},
},
Namespaces: []*LinuxNamespace{},
NetDevices: map[string]*LinuxNetDevice{},
},
},
},
Expand Down Expand Up @@ -238,6 +242,9 @@ func (r *result) adjust(rpl *ContainerAdjustment, plugin string) error {
if err := r.adjustSysctl(rpl.Linux.Sysctl, 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 @@ -960,6 +967,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 @@ -162,6 +162,24 @@ func (a *ContainerAdjustment) RemoveNamespace(n *LinuxNamespace) {
})
}

// 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
}

// RemoveLinuxNetDevice 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 @@ -388,3 +406,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