Skip to content

Commit a895b02

Browse files
committed
[no-relnote] Refactor construction of hooks
This change moves the common logic for the nvidia-cdi-hook and nvidia-ctk hook commands to the commands package. Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent f5b4c15 commit a895b02

File tree

3 files changed

+28
-39
lines changed

3 files changed

+28
-39
lines changed

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

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

1919
import (
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
}

cmd/nvidia-cdi-hook/main.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func main() {
4545
opts := options{}
4646

4747
// Create the top-level CLI
48-
c := cli.Command{
48+
c := commands.ConfigureCDIHookCommand(logger, &cli.Command{
4949
Name: "NVIDIA CDI Hook",
5050
Usage: "Command to structure files for usage inside a container, called as hooks from a container runtime, defined in a CDI yaml file",
5151
Version: info.GetVersionString(),
@@ -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",
@@ -91,7 +78,7 @@ func main() {
9178
Sources: cli.EnvVars("NVIDIA_CTK_QUIET", "NVIDIA_CDI_QUIET"),
9279
},
9380
},
94-
}
81+
})
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

@@ -40,22 +38,8 @@ func NewCommand(logger logger.Interface) *cli.Command {
4038
// build
4139
func (m hookCommand) build() *cli.Command {
4240
// Create the 'hook' subcommand
43-
hook := cli.Command{
41+
return commands.ConfigureCDIHookCommand(m.logger, &cli.Command{
4442
Name: "hook",
4543
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-
}
59-
60-
return &hook
44+
})
6145
}

0 commit comments

Comments
 (0)