Skip to content

Commit a03dcca

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

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
@@ -43,6 +43,10 @@ const (
4343
cdiDeviceKey = "cdi-devices.noderesource.dev"
4444
// Deprecated: Prefix of the key used for CDI device annotations.
4545
oldCDIDeviceKey = "cdi-devices.nri.io"
46+
// Prefix of the key used for network device injection.
47+
netDeviceKey = "network-devices.noderesource.dev"
48+
// Deprecated: Prefix of the key used for network device injection.
49+
oldNetDeviceKey = "network-devices.nri.io"
4650
)
4751

4852
var (
@@ -69,6 +73,12 @@ type mount struct {
6973
Options []string `json:"options"`
7074
}
7175

76+
// a network device to inject
77+
type netDevice struct {
78+
HostIf string `json:"hostIf"`
79+
Name string `json:"name"`
80+
}
81+
7282
// our injector plugin
7383
type plugin struct {
7484
stub stub.Stub
@@ -94,6 +104,10 @@ func (p *plugin) CreateContainer(_ context.Context, pod *api.PodSandbox, ctr *ap
94104
return nil, nil, err
95105
}
96106

107+
if err := injectNetDevices(pod, ctr, adjust); err != nil {
108+
return nil, nil, err
109+
}
110+
97111
if verbose {
98112
dump(containerName(pod, ctr), "ContainerAdjustment", adjust)
99113
}
@@ -232,6 +246,51 @@ func parseMounts(ctr string, annotations map[string]string) ([]mount, error) {
232246
return mounts, nil
233247
}
234248

249+
func injectNetDevices(pod *api.PodSandbox, ctr *api.Container, a *api.ContainerAdjustment) error {
250+
devices, err := parseNetDevices(ctr.Name, pod.Annotations)
251+
if err != nil {
252+
return err
253+
}
254+
255+
if len(devices) == 0 {
256+
log.Debugf("%s: no network devices annotated...", containerName(pod, ctr))
257+
return nil
258+
}
259+
260+
if verbose {
261+
dump(containerName(pod, ctr), "annotated network devices", devices)
262+
}
263+
264+
for _, d := range devices {
265+
a.AddLinuxNetDevice(d.HostIf, &api.LinuxNetDevice{
266+
Name: d.Name,
267+
})
268+
if !verbose {
269+
log.Infof("%s: injected network device %q -> %q...", containerName(pod, ctr),
270+
d.HostIf, d.Name)
271+
}
272+
}
273+
274+
return nil
275+
}
276+
277+
func parseNetDevices(ctr string, annotations map[string]string) ([]*netDevice, error) {
278+
var (
279+
devices []*netDevice
280+
)
281+
282+
annotation := getAnnotation(annotations, netDeviceKey, oldNetDeviceKey, ctr)
283+
if annotation == nil {
284+
return nil, nil
285+
}
286+
287+
if err := yaml.Unmarshal(annotation, &devices); err != nil {
288+
return nil, fmt.Errorf("invalid net device annotation %q: %w", string(annotation), err)
289+
}
290+
291+
return devices, nil
292+
}
293+
235294
func getAnnotation(annotations map[string]string, mainKey, oldKey, ctr string) []byte {
236295
for _, key := range []string{
237296
mainKey + "/container." + ctr,

0 commit comments

Comments
 (0)