Skip to content

Commit 02f0b68

Browse files
committed
api,adaptation,generate: allow adjusting linux net devices.
Allow adding and removing container linux net devices. Signed-off-by: Krisztian Litkey <[email protected]>
1 parent 6420bd6 commit 02f0b68

File tree

9 files changed

+1288
-493
lines changed

9 files changed

+1288
-493
lines changed

pkg/adaptation/adaptation_suite_test.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,17 @@ var _ = Describe("Plugin container creation adjustments", func() {
508508
},
509509
)
510510

511+
case "linux net device":
512+
if overwrite {
513+
a.RemoveLinuxNetDevice("hostIf")
514+
}
515+
a.AddLinuxNetDevice(
516+
"hostIf",
517+
&api.LinuxNetDevice{
518+
Name: "containerIf",
519+
},
520+
)
521+
511522
case "resources/cpu":
512523
a.SetLinuxCPUShares(123)
513524
a.SetLinuxCPUQuota(456)
@@ -693,6 +704,17 @@ var _ = Describe("Plugin container creation adjustments", func() {
693704
},
694705
},
695706
),
707+
Entry("adjust linux net devices", "linux net device",
708+
&api.ContainerAdjustment{
709+
Linux: &api.LinuxContainerAdjustment{
710+
NetDevices: map[string]*api.LinuxNetDevice{
711+
"hostIf": {
712+
Name: "containerIf",
713+
},
714+
},
715+
},
716+
},
717+
),
696718
Entry("adjust CPU resources", "resources/cpu",
697719
&api.ContainerAdjustment{
698720
Linux: &api.LinuxContainerAdjustment{
@@ -914,6 +936,19 @@ var _ = Describe("Plugin container creation adjustments", func() {
914936
},
915937
),
916938
Entry("adjust resources", "resources/classes", false, true, nil),
939+
Entry("adjust linux net devices", "linux net device", true, false,
940+
&api.ContainerAdjustment{
941+
Linux: &api.LinuxContainerAdjustment{
942+
NetDevices: map[string]*api.LinuxNetDevice{
943+
"-hostIf": nil,
944+
"hostIf": {
945+
Name: "containerIf",
946+
},
947+
},
948+
},
949+
},
950+
),
951+
Entry("adjust linux net devices (conflicts)", "linux net device", false, true, nil),
917952
)
918953
})
919954

@@ -2055,7 +2090,9 @@ func stripLinuxAdjustment(a *api.ContainerAdjustment) {
20552090
}
20562091
stripLinuxDevices(a)
20572092
a.Linux.Resources = stripLinuxResources(a.Linux.Resources)
2058-
if a.Linux.Devices == nil && a.Linux.Resources == nil && a.Linux.CgroupsPath == "" {
2093+
stripLinuxNetDevices(a)
2094+
if a.Linux.Devices == nil && a.Linux.Resources == nil && a.Linux.CgroupsPath == "" &&
2095+
a.Linux.OomScoreAdj == nil && a.Linux.NetDevices == nil {
20592096
a.Linux = nil
20602097
}
20612098
}
@@ -2066,6 +2103,12 @@ func stripLinuxDevices(a *api.ContainerAdjustment) {
20662103
}
20672104
}
20682105

2106+
func stripLinuxNetDevices(a *api.ContainerAdjustment) {
2107+
if len(a.Linux.NetDevices) == 0 {
2108+
a.Linux.NetDevices = nil
2109+
}
2110+
}
2111+
20692112
func stripCDIDevices(a *api.ContainerAdjustment) {
20702113
if len(a.CDIDevices) == 0 {
20712114
a.CDIDevices = nil

pkg/adaptation/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ type (
8282
LinuxMemory = api.LinuxMemory
8383
LinuxDevice = api.LinuxDevice
8484
LinuxDeviceCgroup = api.LinuxDeviceCgroup
85+
LinuxNetDevice = api.LinuxNetDevice
8586
CDIDevice = api.CDIDevice
8687
HugepageLimit = api.HugepageLimit
8788
Hooks = api.Hooks

pkg/adaptation/result.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ func collectCreateContainerResult(request *CreateContainerRequest) *result {
8080
if request.Container.Linux.Resources.Unified == nil {
8181
request.Container.Linux.Resources.Unified = map[string]string{}
8282
}
83+
if request.Container.Linux.NetDevices == nil {
84+
request.Container.Linux.NetDevices = map[string]*LinuxNetDevice{}
85+
}
8386

8487
return &result{
8588
request: resultRequest{
@@ -101,6 +104,7 @@ func collectCreateContainerResult(request *CreateContainerRequest) *result {
101104
HugepageLimits: []*HugepageLimit{},
102105
Unified: map[string]string{},
103106
},
107+
NetDevices: map[string]*LinuxNetDevice{},
104108
},
105109
},
106110
},
@@ -223,6 +227,9 @@ func (r *result) adjust(rpl *ContainerAdjustment, plugin string) error {
223227
if err := r.adjustOomScoreAdj(rpl.Linux.OomScoreAdj, plugin); err != nil {
224228
return err
225229
}
230+
if err := r.adjustLinuxNetDevices(rpl.Linux.NetDevices, plugin); err != nil {
231+
return err
232+
}
226233
}
227234
if err := r.adjustRlimits(rpl.Rlimits, plugin); err != nil {
228235
return err
@@ -777,6 +784,41 @@ func (r *result) adjustRlimits(rlimits []*POSIXRlimit, plugin string) error {
777784
return nil
778785
}
779786

787+
func (r *result) adjustLinuxNetDevices(devices map[string]*LinuxNetDevice, plugin string) error {
788+
if len(devices) == 0 {
789+
return nil
790+
}
791+
792+
create, id := r.request.create, r.request.create.Container.Id
793+
del := map[string]struct{}{}
794+
for k := range devices {
795+
if key, marked := IsMarkedForRemoval(k); marked {
796+
del[key] = struct{}{}
797+
delete(devices, k)
798+
}
799+
}
800+
801+
for k, v := range devices {
802+
if _, ok := del[k]; ok {
803+
r.owners.clearLinuxNetDevice(id, k)
804+
delete(create.Container.Linux.NetDevices, k)
805+
r.reply.adjust.Linux.NetDevices[MarkForRemoval(k)] = nil
806+
}
807+
if err := r.owners.claimLinuxNetDevice(id, k, plugin); err != nil {
808+
return err
809+
}
810+
create.Container.Linux.NetDevices[k] = v
811+
r.reply.adjust.Linux.NetDevices[k] = v
812+
delete(del, k)
813+
}
814+
815+
for k := range del {
816+
r.reply.adjust.Linux.NetDevices[MarkForRemoval(k)] = nil
817+
}
818+
819+
return nil
820+
}
821+
780822
func (r *result) updateResources(reply, u *ContainerUpdate, plugin string) error {
781823
if u.Linux == nil || u.Linux.Resources == nil {
782824
return nil
@@ -1004,6 +1046,7 @@ type owners struct {
10041046
cgroupsPath string
10051047
oomScoreAdj string
10061048
rlimits map[string]string
1049+
linuxNetDevices map[string]string
10071050
}
10081051

10091052
func (ro resultOwners) ownersFor(id string) *owners {
@@ -1131,6 +1174,10 @@ func (ro resultOwners) claimRlimits(id, typ, plugin string) error {
11311174
return ro.ownersFor(id).claimRlimit(typ, plugin)
11321175
}
11331176

1177+
func (ro resultOwners) claimLinuxNetDevice(id, key, plugin string) error {
1178+
return ro.ownersFor(id).claimLinuxNetDevice(key, plugin)
1179+
}
1180+
11341181
func (o *owners) claimAnnotation(key, plugin string) error {
11351182
if o.annotations == nil {
11361183
o.annotations = make(map[string]string)
@@ -1388,6 +1435,17 @@ func (o *owners) claimOomScoreAdj(plugin string) error {
13881435
return nil
13891436
}
13901437

1438+
func (o *owners) claimLinuxNetDevice(key, plugin string) error {
1439+
if o.linuxNetDevices == nil {
1440+
o.linuxNetDevices = make(map[string]string)
1441+
}
1442+
if other, taken := o.linuxNetDevices[key]; taken {
1443+
return conflict(plugin, other, "linux net device", key)
1444+
}
1445+
o.linuxNetDevices[key] = plugin
1446+
return nil
1447+
}
1448+
13911449
func (ro resultOwners) clearAnnotation(id, key string) {
13921450
ro.ownersFor(id).clearAnnotation(key)
13931451
}
@@ -1408,6 +1466,10 @@ func (ro resultOwners) clearArgs(id string) {
14081466
ro.ownersFor(id).clearArgs()
14091467
}
14101468

1469+
func (ro resultOwners) clearLinuxNetDevice(id, key string) {
1470+
ro.ownersFor(id).clearLinuxNetDevice(key)
1471+
}
1472+
14111473
func (o *owners) clearAnnotation(key string) {
14121474
if o.annotations == nil {
14131475
return
@@ -1440,6 +1502,13 @@ func (o *owners) clearArgs() {
14401502
o.args = ""
14411503
}
14421504

1505+
func (o *owners) clearLinuxNetDevice(key string) {
1506+
if o.linuxNetDevices == nil {
1507+
return
1508+
}
1509+
delete(o.linuxNetDevices, key)
1510+
}
1511+
14431512
func conflict(plugin, other, subject string, qualif ...string) error {
14441513
return fmt.Errorf("plugins %q and %q both tried to set %s",
14451514
plugin, other, strings.Join(append([]string{subject}, qualif...), " "))

pkg/api/adjustment.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,24 @@ func (a *ContainerAdjustment) AddCDIDevice(d *CDIDevice) {
147147
a.CDIDevices = append(a.CDIDevices, d) // TODO: should we dup d here ?
148148
}
149149

150+
// AddLinuxNetDevice records the addition of the given network device to a container.
151+
func (a *ContainerAdjustment) AddLinuxNetDevice(hostDev string, d *LinuxNetDevice) {
152+
if d == nil {
153+
return
154+
}
155+
a.initLinuxNetDevices()
156+
a.Linux.NetDevices[hostDev] = d
157+
}
158+
159+
// RemoveNetLinuxDevice records the removal of a network device from a container.
160+
// Normally it is an error for a plugin to try and alter a network device
161+
// touched by another container. However, this is not an error if
162+
// the plugin removes that device prior to touching it.
163+
func (a *ContainerAdjustment) RemoveLinuxNetDevice(hostDev string) {
164+
a.initLinuxNetDevices()
165+
a.Linux.NetDevices[MarkForRemoval(hostDev)] = nil
166+
}
167+
150168
// SetLinuxMemoryLimit records setting the memory limit for a container.
151169
func (a *ContainerAdjustment) SetLinuxMemoryLimit(value int64) {
152170
a.initLinuxResourcesMemory()
@@ -345,3 +363,10 @@ func (a *ContainerAdjustment) initLinuxResourcesUnified() {
345363
a.Linux.Resources.Unified = make(map[string]string)
346364
}
347365
}
366+
367+
func (a *ContainerAdjustment) initLinuxNetDevices() {
368+
a.initLinux()
369+
if a.Linux.NetDevices == nil {
370+
a.Linux.NetDevices = make(map[string]*LinuxNetDevice)
371+
}
372+
}

0 commit comments

Comments
 (0)