Skip to content

Commit 4616c6f

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

File tree

3 files changed

+79
-65
lines changed

3 files changed

+79
-65
lines changed

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

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

cmd/nvidia-cdi-hook/main.go

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -44,52 +44,37 @@ 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{
49-
Name: "NVIDIA CDI Hook",
50-
Usage: "Command to structure files for usage inside a container, called as hooks from a container runtime, defined in a CDI yaml file",
51-
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
47+
c := commands.NewHookCommand(logger)
48+
c.Name = "NVIDIA CDI Hook"
49+
c.Usage = "Command to structure files for usage inside a container, called as hooks from a container runtime, defined in a CDI yaml file"
50+
c.Version = info.GetVersionString()
51+
// Set log-level for all subcommands
52+
c.Before = func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
53+
logLevel := logrus.InfoLevel
54+
if opts.Debug {
55+
logLevel = logrus.DebugLevel
56+
}
57+
if opts.Quiet {
58+
logLevel = logrus.ErrorLevel
59+
}
60+
logger.SetLevel(logLevel)
61+
return ctx, nil
62+
}
63+
c.Flags = []cli.Flag{
64+
&cli.BoolFlag{
65+
Name: "debug",
66+
Aliases: []string{"d"},
67+
Usage: "Enable debug-level logging",
68+
Destination: &opts.Debug,
69+
// TODO: Support for NVIDIA_CDI_DEBUG is deprecated and NVIDIA_CTK_DEBUG should be used instead.
70+
Sources: cli.EnvVars("NVIDIA_CTK_DEBUG", "NVIDIA_CDI_DEBUG"),
7471
},
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-
},
72+
&cli.BoolFlag{
73+
Name: "quiet",
74+
Usage: "Suppress all output except for errors; overrides --debug",
75+
Destination: &opts.Quiet,
76+
// TODO: Support for NVIDIA_CDI_QUIET is deprecated and NVIDIA_CTK_QUIET should be used instead.
77+
Sources: cli.EnvVars("NVIDIA_CTK_QUIET", "NVIDIA_CDI_QUIET"),
9378
},
9479
}
9580

cmd/nvidia-ctk/hook/hook.go

Lines changed: 4 additions & 20 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{
44-
Name: "hook",
45-
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),
58-
}
40+
hook := commands.NewHookCommand(m.logger)
41+
hook.Name = "hook"
42+
hook.Usage = "A collection of hooks that may be injected into an OCI spec"
5943

60-
return &hook
44+
return hook
6145
}

0 commit comments

Comments
 (0)