Skip to content

Commit be57aa0

Browse files
committed
api,adaptation,generate: allow adjusting linux net devices.
Allow adding and removing container linux net devices. Signed-off-by: Krisztian Litkey <krisztian.litkey@intel.com>
1 parent 80de972 commit be57aa0

File tree

10 files changed

+1538
-722
lines changed

10 files changed

+1538
-722
lines changed

pkg/adaptation/adaptation_suite_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,17 @@ var _ = Describe("Plugin container creation adjustments", func() {
532532
Class: api.IOPrioClass_IOPRIO_CLASS_NONE,
533533
})
534534

535+
case "linux net device":
536+
if overwrite {
537+
a.RemoveLinuxNetDevice("hostIf")
538+
}
539+
a.AddLinuxNetDevice(
540+
"hostIf",
541+
&api.LinuxNetDevice{
542+
Name: "containerIf",
543+
},
544+
)
545+
535546
case "resources/cpu":
536547
a.SetLinuxCPUShares(123)
537548
a.SetLinuxCPUQuota(456)
@@ -777,6 +788,19 @@ var _ = Describe("Plugin container creation adjustments", func() {
777788
},
778789
},
779790
),
791+
792+
Entry("adjust linux net devices", "linux net device",
793+
&api.ContainerAdjustment{
794+
Linux: &api.LinuxContainerAdjustment{
795+
NetDevices: map[string]*api.LinuxNetDevice{
796+
"hostIf": {
797+
Name: "containerIf",
798+
},
799+
},
800+
},
801+
},
802+
),
803+
780804
Entry("clear I/O priority", "clear I/O priority",
781805
&api.ContainerAdjustment{
782806
Linux: &api.LinuxContainerAdjustment{
@@ -1045,7 +1069,21 @@ var _ = Describe("Plugin container creation adjustments", func() {
10451069
},
10461070
),
10471071
Entry("adjust resources", "resources/classes", false, true, nil),
1072+
10481073
Entry("adjust I/O priority (conflicts)", "I/O priority", false, true, nil),
1074+
Entry("adjust linux net devices", "linux net device", true, false,
1075+
&api.ContainerAdjustment{
1076+
Linux: &api.LinuxContainerAdjustment{
1077+
NetDevices: map[string]*api.LinuxNetDevice{
1078+
"-hostIf": nil,
1079+
"hostIf": {
1080+
Name: "containerIf",
1081+
},
1082+
},
1083+
},
1084+
},
1085+
),
1086+
Entry("adjust linux net devices (conflicts)", "linux net device", false, true, nil),
10491087
)
10501088
})
10511089

pkg/adaptation/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ type (
9292
LinuxDeviceCgroup = api.LinuxDeviceCgroup
9393
LinuxIOPriority = api.LinuxIOPriority
9494
LinuxSeccomp = api.LinuxSeccomp
95+
LinuxNetDevice = api.LinuxNetDevice
9596
CDIDevice = api.CDIDevice
9697
HugepageLimit = api.HugepageLimit
9798
Hooks = api.Hooks

pkg/adaptation/result.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ func collectCreateContainerResult(request *CreateContainerRequest) *result {
8282
if request.Container.Linux.Namespaces == nil {
8383
request.Container.Linux.Namespaces = []*LinuxNamespace{}
8484
}
85+
if request.Container.Linux.NetDevices == nil {
86+
request.Container.Linux.NetDevices = map[string]*LinuxNetDevice{}
87+
}
8588

8689
return &result{
8790
request: resultRequest{
@@ -104,6 +107,7 @@ func collectCreateContainerResult(request *CreateContainerRequest) *result {
104107
Unified: map[string]string{},
105108
},
106109
Namespaces: []*LinuxNamespace{},
110+
NetDevices: map[string]*LinuxNetDevice{},
107111
},
108112
},
109113
},
@@ -235,6 +239,9 @@ func (r *result) adjust(rpl *ContainerAdjustment, plugin string) error {
235239
if err := r.adjustNamespaces(rpl.Linux.Namespaces, plugin); err != nil {
236240
return err
237241
}
242+
if err := r.adjustLinuxNetDevices(rpl.Linux.NetDevices, plugin); err != nil {
243+
return err
244+
}
238245
}
239246
if err := r.adjustRlimits(rpl.Rlimits, plugin); err != nil {
240247
return err
@@ -922,6 +929,41 @@ func (r *result) adjustRlimits(rlimits []*POSIXRlimit, plugin string) error {
922929
return nil
923930
}
924931

932+
func (r *result) adjustLinuxNetDevices(devices map[string]*LinuxNetDevice, plugin string) error {
933+
if len(devices) == 0 {
934+
return nil
935+
}
936+
937+
create, id := r.request.create, r.request.create.Container.Id
938+
del := map[string]struct{}{}
939+
for k := range devices {
940+
if key, marked := IsMarkedForRemoval(k); marked {
941+
del[key] = struct{}{}
942+
delete(devices, k)
943+
}
944+
}
945+
946+
for k, v := range devices {
947+
if _, ok := del[k]; ok {
948+
r.owners.ClearLinuxNetDevice(id, k, plugin)
949+
delete(create.Container.Linux.NetDevices, k)
950+
r.reply.adjust.Linux.NetDevices[MarkForRemoval(k)] = nil
951+
}
952+
if err := r.owners.ClaimLinuxNetDevice(id, k, plugin); err != nil {
953+
return err
954+
}
955+
create.Container.Linux.NetDevices[k] = v
956+
r.reply.adjust.Linux.NetDevices[k] = v
957+
delete(del, k)
958+
}
959+
960+
for k := range del {
961+
r.reply.adjust.Linux.NetDevices[MarkForRemoval(k)] = nil
962+
}
963+
964+
return nil
965+
}
966+
925967
func (r *result) updateResources(reply, u *ContainerUpdate, plugin string) error {
926968
if u.Linux == nil || u.Linux.Resources == nil {
927969
return nil

pkg/api/adjustment.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,24 @@ func (a *ContainerAdjustment) RemoveNamespace(n *LinuxNamespace) {
162162
})
163163
}
164164

165+
// AddLinuxNetDevice records the addition of the given network device to a container.
166+
func (a *ContainerAdjustment) AddLinuxNetDevice(hostDev string, d *LinuxNetDevice) {
167+
if d == nil {
168+
return
169+
}
170+
a.initLinuxNetDevices()
171+
a.Linux.NetDevices[hostDev] = d
172+
}
173+
174+
// RemoveLinuxNetDevice records the removal of a network device from a container.
175+
// Normally it is an error for a plugin to try and alter a network device
176+
// touched by another container. However, this is not an error if
177+
// the plugin removes that device prior to touching it.
178+
func (a *ContainerAdjustment) RemoveLinuxNetDevice(hostDev string) {
179+
a.initLinuxNetDevices()
180+
a.Linux.NetDevices[MarkForRemoval(hostDev)] = nil
181+
}
182+
165183
// SetLinuxMemoryLimit records setting the memory limit for a container.
166184
func (a *ContainerAdjustment) SetLinuxMemoryLimit(value int64) {
167185
a.initLinuxResourcesMemory()
@@ -379,3 +397,10 @@ func (a *ContainerAdjustment) initLinuxResourcesUnified() {
379397
a.Linux.Resources.Unified = make(map[string]string)
380398
}
381399
}
400+
401+
func (a *ContainerAdjustment) initLinuxNetDevices() {
402+
a.initLinux()
403+
if a.Linux.NetDevices == nil {
404+
a.Linux.NetDevices = make(map[string]*LinuxNetDevice)
405+
}
406+
}

0 commit comments

Comments
 (0)