Skip to content

Commit 0a64ad5

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

File tree

3 files changed

+49
-31
lines changed

3 files changed

+49
-31
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 configures a base command with supported CDI
59+
// hooks and error handling for unsupported hooks.
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: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,6 @@ func main() {
6161
logger.SetLevel(logLevel)
6262
return ctx, nil
6363
},
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
74-
},
75-
// Define the subcommands
76-
Commands: commands.New(logger),
7764
Flags: []cli.Flag{
7865
&cli.BoolFlag{
7966
Name: "debug",
@@ -92,6 +79,7 @@ func main() {
9279
},
9380
},
9481
}
82+
commands.ConfigureCDIHookCommand(&c, logger)
9583

9684
// Run the CLI
9785
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)