From bc4e19aa48309acdc77e70bcff3afc6751fd9954 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Mon, 27 Nov 2023 11:17:55 +0100 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + cmd/nvidia-ctk/cdi/transform/root/root.go | 32 ++++++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f7389e0..c4812fde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * 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. * 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) diff --git a/cmd/nvidia-ctk/cdi/transform/root/root.go b/cmd/nvidia-ctk/cdi/transform/root/root.go index 8933a815..4de8d9c6 100644 --- a/cmd/nvidia-ctk/cdi/transform/root/root.go +++ b/cmd/nvidia-ctk/cdi/transform/root/root.go @@ -23,7 +23,7 @@ import ( "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/transform" + transformroot "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform/root" "github.com/urfave/cli/v2" "tags.cncf.io/container-device-interface/pkg/cdi" ) @@ -39,8 +39,9 @@ type transformOptions struct { type options struct { transformOptions - from string - to string + from string + to string + relativeTo string } // NewCommand constructs a generate-cdi command with the specified logger @@ -67,6 +68,11 @@ func (m command) build() *cli.Command { } c.Flags = []cli.Flag{ + &cli.StringFlag{ + Name: "from", + Usage: "specify the root to be transformed", + Destination: &opts.from, + }, &cli.StringFlag{ Name: "input", 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, }, &cli.StringFlag{ - Name: "from", - Usage: "specify the root to be transformed", - Destination: &opts.from, + Name: "relative-to", + Usage: "specify whether the transform is relative to the host or to the container. One of [ host | container ]", + Value: "host", + Destination: &opts.relativeTo, }, &cli.StringFlag{ Name: "to", @@ -95,6 +102,12 @@ func (m command) build() *cli.Command { } 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 } @@ -104,9 +117,10 @@ func (m command) run(c *cli.Context, opts *options) error { return fmt.Errorf("failed to load CDI specification: %w", err) } - err = transform.NewHostRootTransformer( - opts.from, - opts.to, + err = transformroot.New( + transformroot.WithRoot(opts.from), + transformroot.WithTargetRoot(opts.to), + transformroot.WithRelativeTo(opts.relativeTo), ).Transform(spec.Raw()) if err != nil { return fmt.Errorf("failed to transform CDI specification: %w", err)