Skip to content

Commit c5f3eae

Browse files
committed
device-injector: add network device injection.
Signed-off-by: Krisztian Litkey <[email protected]>
1 parent 38f59bc commit c5f3eae

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

plugins/device-injector/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,33 @@ The annotation value syntax for mount injection is
101101
...
102102
```
103103

104+
### Network Device Annotations
105+
106+
Network devices are annotated in a similar manner to devices, but using the
107+
`network-devices.noderesource.dev` annotation key prefix. As with devices, the
108+
`network-devices.nri.io` annotation key prefix is also supported.
109+
110+
The annotation value for network devices is the list of host network interfaces
111+
to inject, together with their container interface names
112+
113+
For instance, the following annotation
114+
115+
```
116+
metadata:
117+
name: net-dev-test
118+
annotations:
119+
network-devices.noderesource.dev/container.c0: |
120+
- hostIf: ens2.100
121+
Name: netdev0
122+
network-devices.noderesource.dev/container.c1: |
123+
- hostIf: ens2.101
124+
Name: netdev1
125+
```
126+
127+
requests the injection of the host network interface `ens2.100` into container `c0`
128+
as the network interface `netdev0`, and the host network interface `ens2.101` into
129+
container `c1` as the network interface `netdev1`.
130+
104131
## Deployment
105132

106133
The NRI repository contains minimal kustomize overlays for this plugin at

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)