1717package commands
1818
1919import (
20+ "context"
21+
2022 "github.com/urfave/cli/v3"
2123
2224 "github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-cdi-hook/chmod"
@@ -27,27 +29,43 @@ import (
2729 "github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
2830)
2931
30- // New creates the commands associated with supported CDI hooks.
31- // These are shared by the nvidia-cdi-hook and nvidia-ctk hook commands.
32- func New (logger logger.Interface ) []* cli.Command {
33- return []* cli.Command {
32+ // ConfigureCDIHookCommand configures a base command with supported CDI hooks
33+ // and error handling for unsupported hooks.
34+ // This allows the same command to be used for the nvidia-cdi-hook and
35+ // nvidia-ctk hook commands.
36+ func ConfigureCDIHookCommand (logger logger.Interface , cmd * cli.Command ) * cli.Command {
37+ // We set the default action for the command to issue a warning and exit
38+ // with no error.
39+ // This means that if an unsupported hook is run, a container will not fail
40+ // to launch. An unsupported hook could be the result of a CDI specification
41+ // referring to a new hook that is not yet supported by an older NVIDIA
42+ // Container Toolkit version or a hook that has been removed in newer
43+ // version.
44+ cmd .Action = func (ctx context.Context , cmd * cli.Command ) error {
45+ return issueUnsupportedHookWarning (logger , cmd )
46+ }
47+ // Define the subcommands
48+ cmd .Commands = []* cli.Command {
3449 ldcache .NewCommand (logger ),
3550 symlinks .NewCommand (logger ),
3651 chmod .NewCommand (logger ),
3752 cudacompat .NewCommand (logger ),
3853 disabledevicenodemodification .NewCommand (logger ),
3954 }
55+
56+ return cmd
4057}
4158
42- // IssueUnsupportedHookWarning logs a warning that no hook or an unsupported
59+ // issueUnsupportedHookWarning logs a warning that no hook or an unsupported
4360// hook has been specified.
4461// This happens if a subcommand is provided that does not match one of the
4562// subcommands that has been explicitly specified.
46- func IssueUnsupportedHookWarning (logger logger.Interface , c * cli.Command ) {
63+ func issueUnsupportedHookWarning (logger logger.Interface , c * cli.Command ) error {
4764 args := c .Args ().Slice ()
4865 if len (args ) == 0 {
4966 logger .Warningf ("No CDI hook specified" )
5067 } else {
5168 logger .Warningf ("Unsupported CDI hook: %v" , args [0 ])
5269 }
70+ return nil
5371}
0 commit comments