-
Notifications
You must be signed in to change notification settings - Fork 445
implement NRI plugin server to inject management CDI devices #1498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,146 @@ | ||||||||||||||||
| package nri | ||||||||||||||||
|
|
||||||||||||||||
| import ( | ||||||||||||||||
| "context" | ||||||||||||||||
| "fmt" | ||||||||||||||||
| "os" | ||||||||||||||||
|
|
||||||||||||||||
| "github.com/containerd/nri/pkg/api" | ||||||||||||||||
| nriplugin "github.com/containerd/nri/pkg/stub" | ||||||||||||||||
| "sigs.k8s.io/yaml" | ||||||||||||||||
|
|
||||||||||||||||
| "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| // Compile-time interface checks | ||||||||||||||||
| var ( | ||||||||||||||||
| _ nriplugin.Plugin = (*Plugin)(nil) | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| const ( | ||||||||||||||||
| // nodeResourceCDIDeviceKey is the prefix of the key used for CDI device annotations. | ||||||||||||||||
| nodeResourceCDIDeviceKey = "cdi-devices.noderesource.dev" | ||||||||||||||||
| // nriCDIDeviceKey is the prefix of the key used for CDI device annotations. | ||||||||||||||||
| nriCDIDeviceKey = "cdi-devices.nri.io" | ||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you comment on why we need two prefixes here? Are these not determined by US? (https://github.com/NVIDIA/gpu-operator/pull/1950/files#diff-e6f52ba1392796db4c79e078d3f1067c50e3bfde9d90f3aaaad3eb3e3f4d84fbR20-R21). Why not only respond to one of them?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't. Yes, these should be defined by us and one annotation should be sufficient. This is taken the from example plugin code of |
||||||||||||||||
| // defaultNRISocket represents the default path of the NRI socket | ||||||||||||||||
| defaultNRISocket = "/var/run/nri/nri.sock" | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| type Plugin struct { | ||||||||||||||||
| logger logger.Interface | ||||||||||||||||
|
|
||||||||||||||||
| stub nriplugin.Stub | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // NewPlugin creates a new NRI plugin for injecting CDI devices | ||||||||||||||||
| func NewPlugin(logger logger.Interface) *Plugin { | ||||||||||||||||
| return &Plugin{ | ||||||||||||||||
| logger: logger, | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // CreateContainer handles container creation requests. | ||||||||||||||||
| func (p *Plugin) CreateContainer(_ context.Context, pod *api.PodSandbox, ctr *api.Container) (*api.ContainerAdjustment, []*api.ContainerUpdate, error) { | ||||||||||||||||
| adjust := &api.ContainerAdjustment{} | ||||||||||||||||
|
|
||||||||||||||||
| if err := p.injectCDIDevices(pod, ctr, adjust); err != nil { | ||||||||||||||||
| return nil, nil, err | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| return adjust, nil, nil | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func (p *Plugin) injectCDIDevices(pod *api.PodSandbox, ctr *api.Container, a *api.ContainerAdjustment) error { | ||||||||||||||||
| devices, err := parseCDIDevices(ctr.Name, pod.Annotations) | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| return err | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| if len(devices) == 0 { | ||||||||||||||||
| p.logger.Debugf("%s: no CDI devices annotated...", containerName(pod, ctr)) | ||||||||||||||||
| return nil | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| for _, name := range devices { | ||||||||||||||||
| a.AddCDIDevice( | ||||||||||||||||
| &api.CDIDevice{ | ||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I am aware, this introduces restructions on compatible containerd / cri-o versions.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that we've moved to native CDI, the minimum supported containerd is now v1.7. Will that be affected by this change? |
||||||||||||||||
| Name: name, | ||||||||||||||||
| }, | ||||||||||||||||
| ) | ||||||||||||||||
| p.logger.Infof("%s: injected CDI device %q...", containerName(pod, ctr), name) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| return nil | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func parseCDIDevices(ctr string, annotations map[string]string) ([]string, error) { | ||||||||||||||||
| var ( | ||||||||||||||||
| cdiDevices []string | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| annotation := getAnnotation(annotations, nodeResourceCDIDeviceKey, nriCDIDeviceKey, ctr) | ||||||||||||||||
| if len(annotation) == 0 { | ||||||||||||||||
| return nil, nil | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| if err := yaml.Unmarshal(annotation, &cdiDevices); err != nil { | ||||||||||||||||
| return nil, fmt.Errorf("invalid CDI device annotation %q: %w", string(annotation), err) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| return cdiDevices, nil | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func getAnnotation(annotations map[string]string, mainKey, oldKey, ctr string) []byte { | ||||||||||||||||
| for _, key := range []string{ | ||||||||||||||||
| mainKey + "/container." + ctr, | ||||||||||||||||
| oldKey + "/container." + ctr, | ||||||||||||||||
| mainKey + "/pod", | ||||||||||||||||
| oldKey + "/pod", | ||||||||||||||||
| mainKey, | ||||||||||||||||
| oldKey, | ||||||||||||||||
| } { | ||||||||||||||||
|
Comment on lines
+94
to
+101
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of having to deal with two keys, could we rather have a single function that we call for each of the keys?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The plan is to definitely deal with just one annotation key for now. This was taken from the example plugin code |
||||||||||||||||
| if value, ok := annotations[key]; ok { | ||||||||||||||||
| return []byte(value) | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| return nil | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // Construct a container name for log messages. | ||||||||||||||||
| func containerName(pod *api.PodSandbox, container *api.Container) string { | ||||||||||||||||
| if pod != nil { | ||||||||||||||||
| return pod.Name + "/" + container.Name | ||||||||||||||||
| } | ||||||||||||||||
| return container.Name | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // Start starts the NRI plugin | ||||||||||||||||
| func (p *Plugin) Start(ctx context.Context, nriSocketPath, nriPluginIdx string) error { | ||||||||||||||||
| if len(nriSocketPath) == 0 { | ||||||||||||||||
| nriSocketPath = defaultNRISocket | ||||||||||||||||
| } | ||||||||||||||||
| _, err := os.Stat(nriSocketPath) | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| return fmt.Errorf("failed to find valid nri socket in %s: %w", nriSocketPath, err) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| var pluginOpts []nriplugin.Option | ||||||||||||||||
| pluginOpts = append(pluginOpts, nriplugin.WithPluginIdx(nriPluginIdx)) | ||||||||||||||||
| pluginOpts = append(pluginOpts, nriplugin.WithSocketPath(nriSocketPath)) | ||||||||||||||||
|
Comment on lines
+128
to
+130
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks |
||||||||||||||||
| if p.stub, err = nriplugin.New(p, pluginOpts...); err != nil { | ||||||||||||||||
| return fmt.Errorf("failed to initialise plugin at %s: %w", nriSocketPath, err) | ||||||||||||||||
| } | ||||||||||||||||
| err = p.stub.Start(ctx) | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| return fmt.Errorf("plugin exited with error: %w", err) | ||||||||||||||||
| } | ||||||||||||||||
| return nil | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // Stop stops the NRI plugin | ||||||||||||||||
| func (p *Plugin) Stop() { | ||||||||||||||||
| if p != nil && p.stub != nil { | ||||||||||||||||
| p.stub.Stop() | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,11 +7,13 @@ import ( | |
| "os/signal" | ||
| "path/filepath" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "github.com/urfave/cli/v3" | ||
| "golang.org/x/sys/unix" | ||
|
|
||
| "github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container/runtime" | ||
| "github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container/runtime/nri" | ||
| "github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/toolkit" | ||
| "github.com/NVIDIA/nvidia-container-toolkit/internal/info" | ||
| "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" | ||
|
|
@@ -26,6 +28,9 @@ const ( | |
| toolkitSubDir = "toolkit" | ||
|
|
||
| defaultRuntime = "docker" | ||
|
|
||
| retryBackoff = 2 * time.Second | ||
| maxRetryAttempts = 5 | ||
| ) | ||
|
|
||
| var availableRuntimes = map[string]struct{}{"docker": {}, "crio": {}, "containerd": {}} | ||
|
|
@@ -70,13 +75,15 @@ func main() { | |
| type app struct { | ||
| logger logger.Interface | ||
|
|
||
| toolkit *toolkit.Installer | ||
| nriPlugin *nri.Plugin | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need to embed this here? Does it make sense to just instantiate it when required and use a deferred shutdown?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, this is much better. Thanks for the suggestion! |
||
| toolkit *toolkit.Installer | ||
| } | ||
|
|
||
| // NewApp creates the CLI app fro the specified options. | ||
| func NewApp(logger logger.Interface) *cli.Command { | ||
| a := app{ | ||
| logger: logger, | ||
| logger: logger, | ||
| nriPlugin: nri.NewPlugin(logger), | ||
| } | ||
| return a.build() | ||
| } | ||
|
|
@@ -93,8 +100,8 @@ func (a app) build() *cli.Command { | |
| Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) { | ||
| return ctx, a.Before(cmd, &options) | ||
| }, | ||
| Action: func(_ context.Context, cmd *cli.Command) error { | ||
| return a.Run(cmd, &options) | ||
| Action: func(ctx context.Context, cmd *cli.Command) error { | ||
| return a.Run(ctx, cmd, &options) | ||
| }, | ||
| Flags: []cli.Flag{ | ||
| &cli.BoolFlag{ | ||
|
|
@@ -194,7 +201,7 @@ func (a *app) validateFlags(c *cli.Command, o *options) error { | |
| // Run installs the NVIDIA Container Toolkit and updates the requested runtime. | ||
| // If the application is run as a daemon, the application waits and unconfigures | ||
| // the runtime on termination. | ||
| func (a *app) Run(c *cli.Command, o *options) error { | ||
| func (a *app) Run(ctx context.Context, c *cli.Command, o *options) error { | ||
| err := a.initialize(o.pidFile) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to initialize: %v", err) | ||
|
|
@@ -222,6 +229,11 @@ func (a *app) Run(c *cli.Command, o *options) error { | |
| } | ||
|
|
||
| if !o.noDaemon { | ||
| if o.runtimeOptions.EnableNRI { | ||
| if err = a.startNRIPluginServer(ctx, o.runtimeOptions); err != nil { | ||
| a.logger.Errorf("unable to start NRI plugin server: %v", err) | ||
| } | ||
| } | ||
| err = a.waitForSignal() | ||
| if err != nil { | ||
| return fmt.Errorf("unable to wait for signal: %v", err) | ||
|
|
@@ -287,9 +299,38 @@ func (a *app) waitForSignal() error { | |
| return nil | ||
| } | ||
|
|
||
| func (a *app) startNRIPluginServer(ctx context.Context, opts runtime.Options) error { | ||
| a.logger.Infof("Starting the NRI Plugin server....") | ||
|
|
||
| retriable := func() error { | ||
| return a.nriPlugin.Start(ctx, opts.NRISocket, opts.NRIPluginIndex) | ||
| } | ||
| var err error | ||
| for i := 0; i < maxRetryAttempts; i++ { | ||
| err = retriable() | ||
| if err == nil { | ||
| break | ||
| } | ||
| if i == maxRetryAttempts-1 { | ||
| break | ||
| } | ||
| time.Sleep(retryBackoff) | ||
| } | ||
| if err != nil { | ||
| a.logger.Errorf("Max retries reached %d/%d, aborting", maxRetryAttempts, maxRetryAttempts) | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (a *app) shutdown(pidFile string) { | ||
| a.logger.Infof("Shutting Down") | ||
|
|
||
| if a.nriPlugin != nil { | ||
| a.logger.Infof("Stopping NRI plugin server...") | ||
| a.nriPlugin.Stop() | ||
| } | ||
|
|
||
| err := os.Remove(pidFile) | ||
| if err != nil { | ||
| a.logger.Warningf("Unable to remove pidfile: %v", err) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to use a comma-separated list of devices instead of having to parse the YAML?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I am open to that