@@ -37,6 +37,8 @@ const (
3737 mountKey = "mounts.nri.io"
3838 // Prefix of the key used for CDI device annotations.
3939 cdiDeviceKey = "cdi-devices.nri.io"
40+ // Prefix of the key used for network device injection.
41+ netDeviceKey = "network-devices.nri.io"
4042)
4143
4244var (
@@ -63,6 +65,12 @@ type mount struct {
6365 Options []string `json:"options"`
6466}
6567
68+ // a network device to inject
69+ type netDevice struct {
70+ HostIf string `json:"hostIf"`
71+ Name string `json:"name"`
72+ }
73+
6674// our injector plugin
6775type plugin struct {
6876 stub stub.Stub
@@ -88,6 +96,10 @@ func (p *plugin) CreateContainer(_ context.Context, pod *api.PodSandbox, ctr *ap
8896 return nil , nil , err
8997 }
9098
99+ if err := injectNetDevices (pod , ctr , adjust ); err != nil {
100+ return nil , nil , err
101+ }
102+
91103 if verbose {
92104 dump (containerName (pod , ctr ), "ContainerAdjustment" , adjust )
93105 }
@@ -226,6 +238,51 @@ func parseMounts(ctr string, annotations map[string]string) ([]mount, error) {
226238 return mounts , nil
227239}
228240
241+ func injectNetDevices (pod * api.PodSandbox , ctr * api.Container , a * api.ContainerAdjustment ) error {
242+ devices , err := parseNetDevices (ctr .Name , pod .Annotations )
243+ if err != nil {
244+ return err
245+ }
246+
247+ if len (devices ) == 0 {
248+ log .Debugf ("%s: no network devices annotated..." , containerName (pod , ctr ))
249+ return nil
250+ }
251+
252+ if verbose {
253+ dump (containerName (pod , ctr ), "annotated network devices" , devices )
254+ }
255+
256+ for _ , d := range devices {
257+ a .AddLinuxNetDevice (d .HostIf , & api.LinuxNetDevice {
258+ Name : d .Name ,
259+ })
260+ if ! verbose {
261+ log .Infof ("%s: injected network device %q -> %q..." , containerName (pod , ctr ),
262+ d .HostIf , d .Name )
263+ }
264+ }
265+
266+ return nil
267+ }
268+
269+ func parseNetDevices (ctr string , annotations map [string ]string ) ([]* netDevice , error ) {
270+ var (
271+ devices []* netDevice
272+ )
273+
274+ annotation := getAnnotation (annotations , netDeviceKey , ctr )
275+ if annotation == nil {
276+ return nil , nil
277+ }
278+
279+ if err := yaml .Unmarshal (annotation , & devices ); err != nil {
280+ return nil , fmt .Errorf ("invalid net device annotation %q: %w" , string (annotation ), err )
281+ }
282+
283+ return devices , nil
284+ }
285+
229286func getAnnotation (annotations map [string ]string , mainKey , ctr string ) []byte {
230287 for _ , key := range []string {
231288 mainKey + "/container." + ctr ,
0 commit comments