Skip to content

Commit 0a96043

Browse files
committed
device-injector: add network device injection.
Signed-off-by: Krisztian Litkey <krisztian.litkey@intel.com>
1 parent be57aa0 commit 0a96043

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

plugins/device-injector/device-injector.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ const (
4747
ioPrioKey = "io-priority.noderesource.dev"
4848
// Deprecated: Prefix of the key used for I/O priority adjustment.
4949
oldIoPrioKey = "io-priority.nri.io"
50+
// Prefix of the key used for network device injection.
51+
netDeviceKey = "network-devices.noderesource.dev"
52+
// Deprecated: Prefix of the key used for network device injection.
53+
oldNetDeviceKey = "network-devices.nri.io"
5054
)
5155

5256
var (
@@ -79,6 +83,12 @@ type ioPrio struct {
7983
Priority int32 `json:"priority"`
8084
}
8185

86+
// a network device to inject
87+
type netDevice struct {
88+
HostIf string `json:"hostIf"`
89+
Name string `json:"name"`
90+
}
91+
8292
// our injector plugin
8393
type plugin struct {
8494
stub stub.Stub
@@ -108,6 +118,10 @@ func (p *plugin) CreateContainer(_ context.Context, pod *api.PodSandbox, ctr *ap
108118
return nil, nil, err
109119
}
110120

121+
if err := injectNetDevices(pod, ctr, adjust); err != nil {
122+
return nil, nil, err
123+
}
124+
111125
if verbose {
112126
dump(containerName(pod, ctr), "ContainerAdjustment", adjust)
113127
}
@@ -286,6 +300,51 @@ func parseIOPriority(ctr string, annotations map[string]string) (*ioPrio, error)
286300
return priority, nil
287301
}
288302

303+
func injectNetDevices(pod *api.PodSandbox, ctr *api.Container, a *api.ContainerAdjustment) error {
304+
devices, err := parseNetDevices(ctr.Name, pod.Annotations)
305+
if err != nil {
306+
return err
307+
}
308+
309+
if len(devices) == 0 {
310+
log.Debugf("%s: no network devices annotated...", containerName(pod, ctr))
311+
return nil
312+
}
313+
314+
if verbose {
315+
dump(containerName(pod, ctr), "annotated network devices", devices)
316+
}
317+
318+
for _, d := range devices {
319+
a.AddLinuxNetDevice(d.HostIf, &api.LinuxNetDevice{
320+
Name: d.Name,
321+
})
322+
if !verbose {
323+
log.Infof("%s: injected network device %q -> %q...", containerName(pod, ctr),
324+
d.HostIf, d.Name)
325+
}
326+
}
327+
328+
return nil
329+
}
330+
331+
func parseNetDevices(ctr string, annotations map[string]string) ([]*netDevice, error) {
332+
var (
333+
devices []*netDevice
334+
)
335+
336+
annotation := getAnnotation(annotations, netDeviceKey, oldNetDeviceKey, ctr)
337+
if annotation == nil {
338+
return nil, nil
339+
}
340+
341+
if err := yaml.Unmarshal(annotation, &devices); err != nil {
342+
return nil, fmt.Errorf("invalid net device annotation %q: %w", string(annotation), err)
343+
}
344+
345+
return devices, nil
346+
}
347+
289348
func getAnnotation(annotations map[string]string, mainKey, oldKey, ctr string) []byte {
290349
for _, key := range []string{
291350
mainKey + "/container." + ctr,

0 commit comments

Comments
 (0)