Skip to content

Commit 0719394

Browse files
committed
Add logic to filter out CDI devices based on a config option
This change adds an allowed-cdi-device-pattern command line option to the CDI device injector to allow CDI device names to be filtered. Signed-off-by: Evan Lezar <[email protected]>
1 parent 779966f commit 0719394

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

cmd/plugins/cdi-device-injector/cdi-device-injector.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"errors"
2020
"flag"
2121
"fmt"
22+
"path/filepath"
2223
"strings"
2324

2425
"github.com/sirupsen/logrus"
@@ -41,12 +42,13 @@ var (
4142

4243
// our injector plugin
4344
type plugin struct {
44-
stub stub.Stub
45-
cdiCache *cdiCache
45+
stub stub.Stub
46+
allowedCDIDevicePattern string
47+
cdiCache *cdiCache
4648
}
4749

4850
// CreateContainer handles container creation requests.
49-
func (p *plugin) CreateContainer(_ context.Context, pod *api.PodSandbox, container *api.Container) (_ *api.ContainerAdjustment, _ []*api.ContainerUpdate, err error) {
51+
func (p *plugin) CreateContainer(ctx context.Context, pod *api.PodSandbox, container *api.Container) (_ *api.ContainerAdjustment, _ []*api.ContainerUpdate, err error) {
5052
defer func() {
5153
if err != nil {
5254
log.Error(err)
@@ -60,6 +62,10 @@ func (p *plugin) CreateContainer(_ context.Context, pod *api.PodSandbox, contain
6062
log.Infof("CreateContainer %s", name)
6163
}
6264

65+
if p.allowedCDIDevicePattern == "" {
66+
return nil, nil, nil
67+
}
68+
6369
cdiDevices, err := parseCdiDevices(pod.Annotations, container.Name)
6470
if err != nil {
6571
return nil, nil, fmt.Errorf("failed to parse CDI Device annotations: %w", err)
@@ -69,8 +75,17 @@ func (p *plugin) CreateContainer(_ context.Context, pod *api.PodSandbox, contain
6975
return nil, nil, nil
7076
}
7177

78+
var allowedCDIDevices []string
79+
for _, cdiDevice := range cdiDevices {
80+
match, _ := filepath.Match(p.allowedCDIDevicePattern, cdiDevice)
81+
if !match {
82+
continue
83+
}
84+
allowedCDIDevices = append(allowedCDIDevices, cdiDevice)
85+
}
86+
7287
adjust := &api.ContainerAdjustment{}
73-
if _, err := p.cdiCache.InjectDevices(adjust, cdiDevices...); err != nil {
88+
if _, err := p.cdiCache.InjectDevices(adjust, allowedCDIDevices...); err != nil {
7489
return nil, nil, fmt.Errorf("CDI device injection failed: %w", err)
7590
}
7691

@@ -143,10 +158,11 @@ func dump(args ...interface{}) {
143158

144159
func main() {
145160
var (
146-
pluginName string
147-
pluginIdx string
148-
opts []stub.Option
149-
err error
161+
pluginName string
162+
pluginIdx string
163+
allowedCDIDevicePattern string
164+
opts []stub.Option
165+
err error
150166
)
151167

152168
log = logrus.StandardLogger()
@@ -156,6 +172,7 @@ func main() {
156172

157173
flag.StringVar(&pluginName, "name", "", "plugin name to register to NRI")
158174
flag.StringVar(&pluginIdx, "idx", "", "plugin index to register to NRI")
175+
flag.StringVar(&allowedCDIDevicePattern, "allowed-cdi-device-pattern", "*", "glob pattern for allowed CDI device names")
159176
flag.BoolVar(&verbose, "verbose", false, "enable (more) verbose logging")
160177
flag.Parse()
161178

@@ -167,6 +184,7 @@ func main() {
167184
}
168185

169186
p := &plugin{
187+
allowedCDIDevicePattern: allowedCDIDevicePattern,
170188
cdiCache: &cdiCache{
171189
// TODO: We should allow this to be configured
172190
Cache: cdi.GetDefaultCache(),

0 commit comments

Comments
 (0)