Add --relative-to option to nvidia-ctk transform root

This change adds a --relative-to option to the nvidia-ctk transform root
command. This defaults to "host" maintaining the existing behaviour.

If --relative-to=container is specified, the root transform is applied to
container paths in the CDI specification instead of host paths.

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2023-11-27 11:17:55 +01:00
parent 879cc99aac
commit bc4e19aa48
2 changed files with 24 additions and 9 deletions

View File

@ -9,6 +9,7 @@
* Add support for injecting /dev/nvidia-nvswitch* devices if the NVIDIA_NVSWITCH=enabled envvar is specified. * Add support for injecting /dev/nvidia-nvswitch* devices if the NVIDIA_NVSWITCH=enabled envvar is specified.
* Added support for `nvidia-ctk runtime configure --enable-cdi` for the `docker` runtime. Note that this requires Docker >= 25. * Added support for `nvidia-ctk runtime configure --enable-cdi` for the `docker` runtime. Note that this requires Docker >= 25.
* Fixed bug in `nvidia-ctk config` command when using `--set`. The types of applied config options are now applied correctly. * Fixed bug in `nvidia-ctk config` command when using `--set`. The types of applied config options are now applied correctly.
* Add `--relative-to` option to `nvidia-ctk transform root` command. This controls whether the root transformation is applied to host or container paths.
* [libnvidia-container] Fix device permission check when using cgroupv2 (fixes #227) * [libnvidia-container] Fix device permission check when using cgroupv2 (fixes #227)

View File

@ -23,7 +23,7 @@ import (
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec" "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform" transformroot "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform/root"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"tags.cncf.io/container-device-interface/pkg/cdi" "tags.cncf.io/container-device-interface/pkg/cdi"
) )
@ -39,8 +39,9 @@ type transformOptions struct {
type options struct { type options struct {
transformOptions transformOptions
from string from string
to string to string
relativeTo string
} }
// NewCommand constructs a generate-cdi command with the specified logger // NewCommand constructs a generate-cdi command with the specified logger
@ -67,6 +68,11 @@ func (m command) build() *cli.Command {
} }
c.Flags = []cli.Flag{ c.Flags = []cli.Flag{
&cli.StringFlag{
Name: "from",
Usage: "specify the root to be transformed",
Destination: &opts.from,
},
&cli.StringFlag{ &cli.StringFlag{
Name: "input", Name: "input",
Usage: "Specify the file to read the CDI specification from. If this is '-' the specification is read from STDIN", Usage: "Specify the file to read the CDI specification from. If this is '-' the specification is read from STDIN",
@ -79,9 +85,10 @@ func (m command) build() *cli.Command {
Destination: &opts.output, Destination: &opts.output,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "from", Name: "relative-to",
Usage: "specify the root to be transformed", Usage: "specify whether the transform is relative to the host or to the container. One of [ host | container ]",
Destination: &opts.from, Value: "host",
Destination: &opts.relativeTo,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "to", Name: "to",
@ -95,6 +102,12 @@ func (m command) build() *cli.Command {
} }
func (m command) validateFlags(c *cli.Context, opts *options) error { func (m command) validateFlags(c *cli.Context, opts *options) error {
switch opts.relativeTo {
case "host":
case "container":
default:
return fmt.Errorf("invalid --relative-to value: %v", opts.relativeTo)
}
return nil return nil
} }
@ -104,9 +117,10 @@ func (m command) run(c *cli.Context, opts *options) error {
return fmt.Errorf("failed to load CDI specification: %w", err) return fmt.Errorf("failed to load CDI specification: %w", err)
} }
err = transform.NewHostRootTransformer( err = transformroot.New(
opts.from, transformroot.WithRoot(opts.from),
opts.to, transformroot.WithTargetRoot(opts.to),
transformroot.WithRelativeTo(opts.relativeTo),
).Transform(spec.Raw()) ).Transform(spec.Raw())
if err != nil { if err != nil {
return fmt.Errorf("failed to transform CDI specification: %w", err) return fmt.Errorf("failed to transform CDI specification: %w", err)