Skip to content

Commit

Permalink
Add logic to filter out CDI devices based on a config option
Browse files Browse the repository at this point in the history
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 <elezar@nvidia.com>
  • Loading branch information
elezar committed Jun 27, 2024
1 parent 779966f commit 0719394
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions cmd/plugins/cdi-device-injector/cdi-device-injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"flag"
"fmt"
"path/filepath"
"strings"

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

// our injector plugin
type plugin struct {
stub stub.Stub
cdiCache *cdiCache
stub stub.Stub
allowedCDIDevicePattern string
cdiCache *cdiCache
}

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

if p.allowedCDIDevicePattern == "" {
return nil, nil, nil
}

cdiDevices, err := parseCdiDevices(pod.Annotations, container.Name)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse CDI Device annotations: %w", err)
Expand All @@ -69,8 +75,17 @@ func (p *plugin) CreateContainer(_ context.Context, pod *api.PodSandbox, contain
return nil, nil, nil
}

var allowedCDIDevices []string
for _, cdiDevice := range cdiDevices {
match, _ := filepath.Match(p.allowedCDIDevicePattern, cdiDevice)
if !match {
continue
}
allowedCDIDevices = append(allowedCDIDevices, cdiDevice)
}

adjust := &api.ContainerAdjustment{}
if _, err := p.cdiCache.InjectDevices(adjust, cdiDevices...); err != nil {
if _, err := p.cdiCache.InjectDevices(adjust, allowedCDIDevices...); err != nil {
return nil, nil, fmt.Errorf("CDI device injection failed: %w", err)
}

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

func main() {
var (
pluginName string
pluginIdx string
opts []stub.Option
err error
pluginName string
pluginIdx string
allowedCDIDevicePattern string
opts []stub.Option
err error
)

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

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

Expand All @@ -167,6 +184,7 @@ func main() {
}

p := &plugin{
allowedCDIDevicePattern: allowedCDIDevicePattern,
cdiCache: &cdiCache{
// TODO: We should allow this to be configured
Cache: cdi.GetDefaultCache(),
Expand Down

0 comments on commit 0719394

Please sign in to comment.