Skip to content

Commit d7c7174

Browse files
Print a warning on an unrecognised CDI hook
Signed-off-by: Carlos Eduardo Arango Gutierrez <[email protected]>
1 parent 6363f07 commit d7c7174

File tree

3 files changed

+77
-60
lines changed

3 files changed

+77
-60
lines changed

cmd/nvidia-cdi-hook/commands/commands.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package commands
1818

1919
import (
20+
"context"
21+
"strings"
22+
2023
"github.com/urfave/cli/v3"
2124

2225
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-cdi-hook/chmod"
@@ -51,3 +54,46 @@ func IssueUnsupportedHookWarning(logger logger.Interface, c *cli.Command) {
5154
logger.Warningf("Unsupported CDI hook: %v", args[0])
5255
}
5356
}
57+
58+
// ConfigureCDIHookCommand takes a base command and configures it to handle
59+
// unsupported CDI hooks gracefully.
60+
func ConfigureCDIHookCommand(base *cli.Command, logger logger.Interface) *cli.Command {
61+
// We set the default action for the command to issue a warning and
62+
// exit with no error.
63+
// This means that if an unsupported hook is run, a container will not
64+
// fail to launch. An unsupported hook could be the result of a CDI
65+
// specification referring to a new hook that is not yet supported by an
66+
// older NVIDIA Container Toolkit version or a hook that has been removed
67+
// in newer version.
68+
base.Action = func(ctx context.Context, cmd *cli.Command) error {
69+
IssueUnsupportedHookWarning(logger, cmd)
70+
return nil
71+
}
72+
// Handle unrecognized commands when help is requested (e.g., help
73+
// unknowncommand)
74+
base.CommandNotFound = func(ctx context.Context, cmd *cli.Command, commandName string) {
75+
IssueUnsupportedHookWarning(logger, cmd)
76+
}
77+
// Handle usage errors. For unrecognized commands with flags, we suppress
78+
// the error to maintain backwards compatibility.
79+
base.OnUsageError = func(ctx context.Context, cmd *cli.Command, err error, isSubcommand bool) error {
80+
// If this is a flag parsing error for an unrecognized command,
81+
// suppress it and let the default Action handle it
82+
errMsg := err.Error()
83+
if strings.HasPrefix(errMsg, "flag provided but not defined: -") {
84+
// Check if the first argument is an unrecognized command
85+
args := cmd.Args().Slice()
86+
if len(args) > 0 && cmd.Command(args[0]) == nil {
87+
// This is an unrecognized hook with flags - issue a warning
88+
// and let the default Action handle it
89+
logger.Warningf("Unsupported CDI hook: %v", args[0])
90+
return nil
91+
}
92+
}
93+
// For other usage errors, return the error as-is
94+
return err
95+
}
96+
base.Commands = New(logger)
97+
98+
return base
99+
}

cmd/nvidia-cdi-hook/main.go

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -44,54 +44,41 @@ func main() {
4444
// Create a options struct to hold the parsed environment variables or command line flags
4545
opts := options{}
4646

47-
// Create the top-level CLI
48-
c := cli.Command{
47+
c := &cli.Command{
4948
Name: "NVIDIA CDI Hook",
5049
Usage: "Command to structure files for usage inside a container, called as hooks from a container runtime, defined in a CDI yaml file",
5150
Version: info.GetVersionString(),
52-
// Set log-level for all subcommands
53-
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
54-
logLevel := logrus.InfoLevel
55-
if opts.Debug {
56-
logLevel = logrus.DebugLevel
57-
}
58-
if opts.Quiet {
59-
logLevel = logrus.ErrorLevel
60-
}
61-
logger.SetLevel(logLevel)
62-
return ctx, nil
63-
},
64-
// We set the default action for the `nvidia-cdi-hook` command to issue a
65-
// warning and exit with no error.
66-
// This means that if an unsupported hook is run, a container will not fail
67-
// to launch. An unsupported hook could be the result of a CDI specification
68-
// referring to a new hook that is not yet supported by an older NVIDIA
69-
// Container Toolkit version or a hook that has been removed in newer
70-
// version.
71-
Action: func(ctx context.Context, cmd *cli.Command) error {
72-
commands.IssueUnsupportedHookWarning(logger, cmd)
73-
return nil
51+
}
52+
// Set log-level for all subcommands
53+
c.Before = func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
54+
logLevel := logrus.InfoLevel
55+
if opts.Debug {
56+
logLevel = logrus.DebugLevel
57+
}
58+
if opts.Quiet {
59+
logLevel = logrus.ErrorLevel
60+
}
61+
logger.SetLevel(logLevel)
62+
return ctx, nil
63+
}
64+
c.Flags = []cli.Flag{
65+
&cli.BoolFlag{
66+
Name: "debug",
67+
Aliases: []string{"d"},
68+
Usage: "Enable debug-level logging",
69+
Destination: &opts.Debug,
70+
// TODO: Support for NVIDIA_CDI_DEBUG is deprecated and NVIDIA_CTK_DEBUG should be used instead.
71+
Sources: cli.EnvVars("NVIDIA_CTK_DEBUG", "NVIDIA_CDI_DEBUG"),
7472
},
75-
// Define the subcommands
76-
Commands: commands.New(logger),
77-
Flags: []cli.Flag{
78-
&cli.BoolFlag{
79-
Name: "debug",
80-
Aliases: []string{"d"},
81-
Usage: "Enable debug-level logging",
82-
Destination: &opts.Debug,
83-
// TODO: Support for NVIDIA_CDI_DEBUG is deprecated and NVIDIA_CTK_DEBUG should be used instead.
84-
Sources: cli.EnvVars("NVIDIA_CTK_DEBUG", "NVIDIA_CDI_DEBUG"),
85-
},
86-
&cli.BoolFlag{
87-
Name: "quiet",
88-
Usage: "Suppress all output except for errors; overrides --debug",
89-
Destination: &opts.Quiet,
90-
// TODO: Support for NVIDIA_CDI_QUIET is deprecated and NVIDIA_CTK_QUIET should be used instead.
91-
Sources: cli.EnvVars("NVIDIA_CTK_QUIET", "NVIDIA_CDI_QUIET"),
92-
},
73+
&cli.BoolFlag{
74+
Name: "quiet",
75+
Usage: "Suppress all output except for errors; overrides --debug",
76+
Destination: &opts.Quiet,
77+
// TODO: Support for NVIDIA_CDI_QUIET is deprecated and NVIDIA_CTK_QUIET should be used instead.
78+
Sources: cli.EnvVars("NVIDIA_CTK_QUIET", "NVIDIA_CDI_QUIET"),
9379
},
9480
}
81+
c = commands.ConfigureCDIHookCommand(c, logger)
9582

9683
// Run the CLI
9784
err := c.Run(context.Background(), os.Args)

cmd/nvidia-ctk/hook/hook.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package hook
1818

1919
import (
20-
"context"
21-
2220
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-cdi-hook/commands"
2321
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
2422

@@ -39,23 +37,9 @@ func NewCommand(logger logger.Interface) *cli.Command {
3937

4038
// build
4139
func (m hookCommand) build() *cli.Command {
42-
// Create the 'hook' subcommand
43-
hook := cli.Command{
40+
hook := &cli.Command{
4441
Name: "hook",
4542
Usage: "A collection of hooks that may be injected into an OCI spec",
46-
// We set the default action for the `hook` subcommand to issue a
47-
// warning and exit with no error.
48-
// This means that if an unsupported hook is run, a container will not fail
49-
// to launch. An unsupported hook could be the result of a CDI specification
50-
// referring to a new hook that is not yet supported by an older NVIDIA
51-
// Container Toolkit version or a hook that has been removed in newer
52-
// version.
53-
Action: func(ctx context.Context, cmd *cli.Command) error {
54-
commands.IssueUnsupportedHookWarning(m.logger, cmd)
55-
return nil
56-
},
57-
Commands: commands.New(m.logger),
5843
}
59-
60-
return &hook
44+
return commands.ConfigureCDIHookCommand(hook, m.logger)
6145
}

0 commit comments

Comments
 (0)