Skip to content

Commit 967153b

Browse files
Load settings from config.toml file during CDI generation
Signed-off-by: Carlos Eduardo Arango Gutierrez <[email protected]>
1 parent a920f8b commit 967153b

File tree

29 files changed

+799
-204
lines changed

29 files changed

+799
-204
lines changed

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,25 @@ func (m command) build() *cli.Command {
6464
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
6565
return ctx, m.validateFlags(cmd, &cfg)
6666
},
67-
Action: func(ctx context.Context, cmd *cli.Command) error {
68-
return m.run(cmd, &cfg)
67+
Action: func(_ context.Context, _ *cli.Command) error {
68+
return m.run(nil, &cfg)
6969
},
70-
}
71-
72-
c.Flags = []cli.Flag{
73-
&cli.StringSliceFlag{
74-
Name: "path",
75-
Usage: "Specify a path to apply the specified mode to",
76-
Destination: &cfg.paths,
77-
},
78-
&cli.StringFlag{
79-
Name: "mode",
80-
Usage: "Specify the file mode",
81-
Destination: &cfg.modeStr,
82-
},
83-
&cli.StringFlag{
84-
Name: "container-spec",
85-
Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN",
86-
Destination: &cfg.containerSpec,
70+
Flags: []cli.Flag{
71+
&cli.StringSliceFlag{
72+
Name: "path",
73+
Usage: "Specify a path to apply the specified mode to",
74+
Destination: &cfg.paths,
75+
},
76+
&cli.StringFlag{
77+
Name: "mode",
78+
Usage: "Specify the file mode",
79+
Destination: &cfg.modeStr,
80+
},
81+
&cli.StringFlag{
82+
Name: "container-spec",
83+
Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN",
84+
Destination: &cfg.containerSpec,
85+
},
8786
},
8887
}
8988

cmd/nvidia-cdi-hook/create-soname-symlinks/soname-symlinks.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,26 @@ func (m command) build() *cli.Command {
7474
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
7575
return ctx, m.validateFlags(cmd, &cfg)
7676
},
77-
Action: func(ctx context.Context, cmd *cli.Command) error {
78-
return m.run(cmd, &cfg)
77+
Action: func(_ context.Context, _ *cli.Command) error {
78+
return m.run(nil, &cfg)
7979
},
80-
}
81-
82-
c.Flags = []cli.Flag{
83-
&cli.StringSliceFlag{
84-
Name: "folder",
85-
Usage: "Specify a directory to generate soname symlinks in. Can be specified multiple times",
86-
Destination: &cfg.folders,
87-
},
88-
&cli.StringFlag{
89-
Name: "ldconfig-path",
90-
Usage: "Specify the path to ldconfig on the host",
91-
Destination: &cfg.ldconfigPath,
92-
Value: "/sbin/ldconfig",
93-
},
94-
&cli.StringFlag{
95-
Name: "container-spec",
96-
Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN",
97-
Destination: &cfg.containerSpec,
80+
Flags: []cli.Flag{
81+
&cli.StringSliceFlag{
82+
Name: "folder",
83+
Usage: "Specify a directory to generate soname symlinks in. Can be specified multiple times",
84+
Destination: &cfg.folders,
85+
},
86+
&cli.StringFlag{
87+
Name: "ldconfig-path",
88+
Usage: "Specify the path to ldconfig on the host",
89+
Destination: &cfg.ldconfigPath,
90+
Value: "/sbin/ldconfig",
91+
},
92+
&cli.StringFlag{
93+
Name: "container-spec",
94+
Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN",
95+
Destination: &cfg.containerSpec,
96+
},
9897
},
9998
}
10099

cmd/nvidia-cdi-hook/create-symlinks/create-symlinks.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,22 @@ func (m command) build() *cli.Command {
5858
Usage: "A hook to create symlinks in the container.",
5959
UseShortOptionHandling: true,
6060
EnableShellCompletion: true,
61-
Action: func(_ context.Context, cmd *cli.Command) error {
62-
return m.run(cmd, &cfg)
61+
Action: func(_ context.Context, _ *cli.Command) error {
62+
return m.run(nil, &cfg)
6363
},
64-
}
65-
66-
c.Flags = []cli.Flag{
67-
&cli.StringSliceFlag{
68-
Name: "link",
69-
Usage: "Specify a specific link to create. The link is specified as target::link. If the link exists in the container root, it is removed.",
70-
Destination: &cfg.links,
71-
},
72-
// The following flags are testing-only flags.
73-
&cli.StringFlag{
74-
Name: "container-spec",
75-
Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN. This is only intended for testing.",
76-
Destination: &cfg.containerSpec,
77-
Hidden: true,
64+
Flags: []cli.Flag{
65+
&cli.StringSliceFlag{
66+
Name: "link",
67+
Usage: "Specify a specific link to create. The link is specified as target::link. If the link exists in the container root, it is removed.",
68+
Destination: &cfg.links,
69+
},
70+
// The following flags are testing-only flags.
71+
&cli.StringFlag{
72+
Name: "container-spec",
73+
Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN. This is only intended for testing.",
74+
Destination: &cfg.containerSpec,
75+
Hidden: true,
76+
},
7877
},
7978
}
8079

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,29 @@ func (m command) build() *cli.Command {
6969
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
7070
return ctx, m.validateFlags(cmd, &cfg)
7171
},
72-
Action: func(ctx context.Context, cmd *cli.Command) error {
73-
return m.run(cmd, &cfg)
72+
Action: func(_ context.Context, _ *cli.Command) error {
73+
return m.run(nil, &cfg)
7474
},
75-
}
76-
77-
c.Flags = []cli.Flag{
78-
&cli.StringFlag{
79-
Name: "host-driver-version",
80-
Usage: "Specify the host driver version. If the CUDA compat libraries detected in the container do not have a higher MAJOR version, the hook is a no-op.",
81-
Destination: &cfg.hostDriverVersion,
82-
},
83-
&cli.StringFlag{
84-
Name: "container-spec",
85-
Hidden: true,
86-
Category: "testing-only",
87-
Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN",
88-
Destination: &cfg.containerSpec,
75+
Flags: []cli.Flag{
76+
&cli.StringFlag{
77+
Name: "host-driver-version",
78+
Usage: "Specify the host driver version. If the CUDA compat libraries detected in the container do not have a higher MAJOR version, the hook is a no-op.",
79+
Destination: &cfg.hostDriverVersion,
80+
},
81+
&cli.StringFlag{
82+
Name: "container-spec",
83+
Hidden: true,
84+
Category: "testing-only",
85+
Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN",
86+
Destination: &cfg.containerSpec,
87+
},
8988
},
9089
}
9190

9291
return &c
9392
}
9493

95-
func (m command) validateFlags(cmd *cli.Command, cfg *options) error {
94+
func (m command) validateFlags(_ *cli.Command, _ *options) error {
9695
return nil
9796
}
9897

cmd/nvidia-cdi-hook/disable-device-node-modification/disable-device-node-modification.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,27 @@ func NewCommand(logger logger.Interface) *cli.Command {
5353
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
5454
return ctx, validateFlags(cmd, &cfg)
5555
},
56-
Action: func(ctx context.Context, cmd *cli.Command) error {
57-
return run(ctx, cmd, &cfg)
56+
Action: func(_ context.Context, _ *cli.Command) error {
57+
return run(nil, &cfg)
5858
},
59-
}
60-
61-
c.Flags = []cli.Flag{
62-
&cli.StringFlag{
63-
Name: "container-spec",
64-
Hidden: true,
65-
Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN",
66-
Destination: &cfg.containerSpec,
59+
Flags: []cli.Flag{
60+
&cli.StringFlag{
61+
Name: "container-spec",
62+
Hidden: true,
63+
Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN",
64+
Destination: &cfg.containerSpec,
65+
},
6766
},
6867
}
6968

7069
return &c
7170
}
7271

73-
func validateFlags(c *cli.Command, cfg *options) error {
72+
func validateFlags(_ *cli.Command, _ *options) error {
7473
return nil
7574
}
7675

77-
func run(ctx context.Context, cmd *cli.Command, cfg *options) error {
76+
func run(_ *cli.Command, cfg *options) error {
7877
modifiedParamsFileContents, err := getModifiedNVIDIAParamsContents()
7978
if err != nil {
8079
return fmt.Errorf("failed to get modified params file contents: %w", err)

cmd/nvidia-cdi-hook/main.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ import (
2121
"os"
2222

2323
"github.com/sirupsen/logrus"
24-
25-
"github.com/NVIDIA/nvidia-container-toolkit/internal/info"
26-
2724
cli "github.com/urfave/cli/v3"
2825

2926
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-cdi-hook/commands"
27+
"github.com/NVIDIA/nvidia-container-toolkit/internal/info"
3028
)
3129

3230
// options defines the options that can be set for the CLI through config files,
@@ -51,20 +49,7 @@ func main() {
5149
EnableShellCompletion: true,
5250
Usage: "Command to structure files for usage inside a container, called as hooks from a container runtime, defined in a CDI yaml file",
5351
Version: info.GetVersionString(),
54-
55-
// We set the default action for the `nvidia-cdi-hook` command to issue a
56-
// warning and exit with no error.
57-
// This means that if an unsupported hook is run, a container will not fail
58-
// to launch. An unsupported hook could be the result of a CDI specification
59-
// referring to a new hook that is not yet supported by an older NVIDIA
60-
// Container Toolkit version or a hook that has been removed in newer
61-
// version.
62-
Action: func(ctx context.Context, cmd *cli.Command) error {
63-
commands.IssueUnsupportedHookWarning(logger, cmd)
64-
return nil
65-
},
66-
67-
// Setup the flags for this command
52+
Commands: commands.New(logger),
6853
Flags: []cli.Flag{
6954
&cli.BoolFlag{
7055
Name: "debug",
@@ -83,6 +68,18 @@ func main() {
8368
},
8469
},
8570

71+
// We set the default action for the `nvidia-cdi-hook` command to issue a
72+
// warning and exit with no error.
73+
// This means that if an unsupported hook is run, a container will not fail
74+
// to launch. An unsupported hook could be the result of a CDI specification
75+
// referring to a new hook that is not yet supported by an older NVIDIA
76+
// Container Toolkit version or a hook that has been removed in newer
77+
// version.
78+
Action: func(ctx context.Context, cmd *cli.Command) error {
79+
commands.IssueUnsupportedHookWarning(logger, cmd)
80+
return nil
81+
},
82+
8683
// Set log-level for all subcommands
8784
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
8885
logLevel := logrus.InfoLevel
@@ -95,9 +92,6 @@ func main() {
9592
logger.SetLevel(logLevel)
9693
return ctx, nil
9794
},
98-
99-
// Define the subcommands
100-
Commands: commands.New(logger),
10195
}
10296

10397
// Run the CLI

cmd/nvidia-cdi-hook/update-ldcache/update-ldcache.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,27 +73,26 @@ func (m command) build() *cli.Command {
7373
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
7474
return ctx, m.validateFlags(cmd, &cfg)
7575
},
76-
Action: func(_ context.Context, cmd *cli.Command) error {
77-
return m.run(cmd, &cfg)
76+
Action: func(_ context.Context, _ *cli.Command) error {
77+
return m.run(nil, &cfg)
7878
},
79-
}
80-
81-
c.Flags = []cli.Flag{
82-
&cli.StringSliceFlag{
83-
Name: "folder",
84-
Usage: "Specify a folder to add to /etc/ld.so.conf before updating the ld cache",
85-
Destination: &cfg.folders,
86-
},
87-
&cli.StringFlag{
88-
Name: "ldconfig-path",
89-
Usage: "Specify the path to the ldconfig program",
90-
Destination: &cfg.ldconfigPath,
91-
Value: "/sbin/ldconfig",
92-
},
93-
&cli.StringFlag{
94-
Name: "container-spec",
95-
Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN",
96-
Destination: &cfg.containerSpec,
79+
Flags: []cli.Flag{
80+
&cli.StringSliceFlag{
81+
Name: "folder",
82+
Usage: "Specify a folder to add to /etc/ld.so.conf before updating the ld cache",
83+
Destination: &cfg.folders,
84+
},
85+
&cli.StringFlag{
86+
Name: "ldconfig-path",
87+
Usage: "Specify the path to the ldconfig program",
88+
Destination: &cfg.ldconfigPath,
89+
Value: "/sbin/ldconfig",
90+
},
91+
&cli.StringFlag{
92+
Name: "container-spec",
93+
Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN",
94+
Destination: &cfg.containerSpec,
95+
},
9796
},
9897
}
9998

cmd/nvidia-ctk/cdi/cdi.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,41 @@
1717
package cdi
1818

1919
import (
20+
"github.com/sirupsen/logrus"
2021
"github.com/urfave/cli/v3"
2122

2223
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk/cdi/generate"
2324
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk/cdi/list"
2425
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk/cdi/transform"
25-
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
2626
)
2727

2828
type command struct {
29-
logger logger.Interface
29+
logger *logrus.Logger
30+
configFile *string
3031
}
3132

32-
// NewCommand constructs an info command with the specified logger
33-
func NewCommand(logger logger.Interface) *cli.Command {
33+
// NewCommand constructs a cdi command with the specified logger
34+
func NewCommand(logger *logrus.Logger, configFile *string) *cli.Command {
3435
c := command{
35-
logger: logger,
36+
logger: logger,
37+
configFile: configFile,
3638
}
3739
return c.build()
3840
}
3941

4042
// build
4143
func (m command) build() *cli.Command {
42-
// Create the 'hook' command
43-
hook := cli.Command{
44+
// Create the 'cdi' command
45+
cdi := cli.Command{
4446
Name: "cdi",
4547
Usage: "Provide tools for interacting with Container Device Interface specifications",
4648
}
4749

48-
hook.Commands = []*cli.Command{
49-
generate.NewCommand(m.logger),
50-
transform.NewCommand(m.logger),
51-
list.NewCommand(m.logger),
50+
cdi.Commands = []*cli.Command{
51+
generate.NewCommand(m.logger, m.configFile),
52+
list.NewCommand(m.logger, m.configFile),
53+
transform.NewCommand(m.logger, m.configFile),
5254
}
5355

54-
return &hook
56+
return &cdi
5557
}

0 commit comments

Comments
 (0)