Skip to content

Commit b09e75c

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 8654000 commit b09e75c

File tree

10 files changed

+1416
-604
lines changed

10 files changed

+1416
-604
lines changed

pkg/adaptation/adaptation_suite_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,17 @@ var _ = Describe("Plugin container creation adjustments", func() {
514514
},
515515
)
516516

517+
case "linux net device":
518+
if overwrite {
519+
a.RemoveLinuxNetDevice("hostIf")
520+
}
521+
a.AddLinuxNetDevice(
522+
"hostIf",
523+
&api.LinuxNetDevice{
524+
Name: "containerIf",
525+
},
526+
)
527+
517528
case "resources/cpu":
518529
a.SetLinuxCPUShares(123)
519530
a.SetLinuxCPUQuota(456)
@@ -700,6 +711,17 @@ var _ = Describe("Plugin container creation adjustments", func() {
700711
},
701712
},
702713
),
714+
Entry("adjust linux net devices", "linux net device",
715+
&api.ContainerAdjustment{
716+
Linux: &api.LinuxContainerAdjustment{
717+
NetDevices: map[string]*api.LinuxNetDevice{
718+
"hostIf": {
719+
Name: "containerIf",
720+
},
721+
},
722+
},
723+
},
724+
),
703725
Entry("adjust CPU resources", "resources/cpu",
704726
&api.ContainerAdjustment{
705727
Linux: &api.LinuxContainerAdjustment{
@@ -921,6 +943,19 @@ var _ = Describe("Plugin container creation adjustments", func() {
921943
},
922944
),
923945
Entry("adjust resources", "resources/classes", false, true, nil),
946+
Entry("adjust linux net devices", "linux net device", true, false,
947+
&api.ContainerAdjustment{
948+
Linux: &api.LinuxContainerAdjustment{
949+
NetDevices: map[string]*api.LinuxNetDevice{
950+
"-hostIf": nil,
951+
"hostIf": {
952+
Name: "containerIf",
953+
},
954+
},
955+
},
956+
},
957+
),
958+
Entry("adjust linux net devices (conflicts)", "linux net device", false, true, nil),
924959
)
925960
})
926961

pkg/adaptation/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type (
8989
LinuxMemory = api.LinuxMemory
9090
LinuxDevice = api.LinuxDevice
9191
LinuxDeviceCgroup = api.LinuxDeviceCgroup
92+
LinuxNetDevice = api.LinuxNetDevice
9293
CDIDevice = api.CDIDevice
9394
HugepageLimit = api.HugepageLimit
9495
Hooks = api.Hooks

pkg/adaptation/result.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ func collectCreateContainerResult(request *CreateContainerRequest) *result {
7878
if request.Container.Linux.Resources.Unified == nil {
7979
request.Container.Linux.Resources.Unified = map[string]string{}
8080
}
81+
if request.Container.Linux.NetDevices == nil {
82+
request.Container.Linux.NetDevices = map[string]*LinuxNetDevice{}
83+
}
8184

8285
return &result{
8386
request: resultRequest{
@@ -99,6 +102,7 @@ func collectCreateContainerResult(request *CreateContainerRequest) *result {
99102
HugepageLimits: []*HugepageLimit{},
100103
Unified: map[string]string{},
101104
},
105+
NetDevices: map[string]*LinuxNetDevice{},
102106
},
103107
},
104108
},
@@ -221,6 +225,9 @@ func (r *result) adjust(rpl *ContainerAdjustment, plugin string) error {
221225
if err := r.adjustOomScoreAdj(rpl.Linux.OomScoreAdj, plugin); err != nil {
222226
return err
223227
}
228+
if err := r.adjustLinuxNetDevices(rpl.Linux.NetDevices, plugin); err != nil {
229+
return err
230+
}
224231
}
225232
if err := r.adjustRlimits(rpl.Rlimits, plugin); err != nil {
226233
return err
@@ -786,6 +793,41 @@ func (r *result) adjustRlimits(rlimits []*POSIXRlimit, plugin string) error {
786793
return nil
787794
}
788795

796+
func (r *result) adjustLinuxNetDevices(devices map[string]*LinuxNetDevice, plugin string) error {
797+
if len(devices) == 0 {
798+
return nil
799+
}
800+
801+
create, id := r.request.create, r.request.create.Container.Id
802+
del := map[string]struct{}{}
803+
for k := range devices {
804+
if key, marked := IsMarkedForRemoval(k); marked {
805+
del[key] = struct{}{}
806+
delete(devices, k)
807+
}
808+
}
809+
810+
for k, v := range devices {
811+
if _, ok := del[k]; ok {
812+
r.owners.ClearLinuxNetDevice(id, k, plugin)
813+
delete(create.Container.Linux.NetDevices, k)
814+
r.reply.adjust.Linux.NetDevices[MarkForRemoval(k)] = nil
815+
}
816+
if err := r.owners.ClaimLinuxNetDevice(id, k, plugin); err != nil {
817+
return err
818+
}
819+
create.Container.Linux.NetDevices[k] = v
820+
r.reply.adjust.Linux.NetDevices[k] = v
821+
delete(del, k)
822+
}
823+
824+
for k := range del {
825+
r.reply.adjust.Linux.NetDevices[MarkForRemoval(k)] = nil
826+
}
827+
828+
return nil
829+
}
830+
789831
func (r *result) updateResources(reply, u *ContainerUpdate, plugin string) error {
790832
if u.Linux == nil || u.Linux.Resources == nil {
791833
return nil

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)