Handle hook arguments for creation of symlinks

Signed-off-by: Christopher Desiniotis <cdesiniotis@nvidia.com>
This commit is contained in:
Christopher Desiniotis 2023-02-22 13:11:10 -08:00
parent 0516fc96ca
commit 45ed3b0412
3 changed files with 98 additions and 9 deletions

View File

@ -0,0 +1,35 @@
/**
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
**/
package transform
import (
"github.com/container-orchestrated-devices/container-device-interface/specs-go"
)
type noop struct{}
var _ Transformer = (*noop)(nil)
// NewNoopTransformer returns a no-op transformer
func NewNoopTransformer() Transformer {
return noop{}
}
// Transform is a no-op
func (n noop) Transform(spec *specs.Spec) error {
return nil
}

View File

@ -32,8 +32,13 @@ type rootTransformer struct {
var _ Transformer = (*rootTransformer)(nil) var _ Transformer = (*rootTransformer)(nil)
// NewRootTransformer creates a new transformer for modifying // NewRootTransformer creates a new transformer for modifying
// the root for paths in a CDI spec. // the root for paths in a CDI spec. If both roots are identical,
// this tranformer is a no-op.
func NewRootTransformer(root string, targetRoot string) Transformer { func NewRootTransformer(root string, targetRoot string) Transformer {
if root == targetRoot {
return NewNoopTransformer()
}
t := rootTransformer{ t := rootTransformer{
root: root, root: root,
targetRoot: targetRoot, targetRoot: targetRoot,
@ -69,10 +74,23 @@ func (t rootTransformer) applyToEdits(edits *specs.ContainerEdits) error {
for i, hook := range edits.Hooks { for i, hook := range edits.Hooks {
hook.Path = t.transformPath(hook.Path) hook.Path = t.transformPath(hook.Path)
// TODO: The args need more attention. For the hooks that create symlinks we would have to transform these too.
var args []string var args []string
for _, arg := range hook.Args { for _, arg := range hook.Args {
args = append(args, t.transformPath(arg)) if !strings.Contains(arg, "::") {
args = append(args, t.transformPath(arg))
continue
}
// For the 'create-symlinks' hook, special care is taken for the
// '--link' flag argument which takes the form <target>::<link>.
// Both paths, the target and link paths, are transformed.
split := strings.Split(arg, "::")
if len(split) != 2 {
return fmt.Errorf("unexpected number of '::' separators in hook argument")
}
split[0] = t.transformPath(split[0])
split[1] = t.transformPath(split[1])
args = append(args, strings.Join(split, "::"))
} }
hook.Args = args hook.Args = args
edits.Hooks[i] = hook edits.Hooks[i] = hook

View File

@ -98,18 +98,54 @@ func TestRootTransformer(t *testing.T) {
spec: &specs.Spec{ spec: &specs.Spec{
ContainerEdits: specs.ContainerEdits{ ContainerEdits: specs.ContainerEdits{
Hooks: []*specs.Hook{ Hooks: []*specs.Hook{
{Path: "/root/usr/bin/nvidia-ctk"}, {
{Path: "/target-root/usr/bin/nvidia-ctk"}, Path: "/root/usr/bin/nvidia-ctk",
{Path: "/different-root/usr/bin/nvidia-ctk"}, Args: []string{
"--link",
"/root/path/to/target::/root/path/to/link",
},
},
{
Path: "/target-root/usr/bin/nvidia-ctk",
Args: []string{
"--link",
"/target-root/path/to/target::/target-root/path/to/link",
},
},
{
Path: "/different-root/usr/bin/nvidia-ctk",
Args: []string{
"--link",
"/different-root/path/to/target::/different-root/path/to/link",
},
},
}, },
}, },
}, },
expectedSpec: &specs.Spec{ expectedSpec: &specs.Spec{
ContainerEdits: specs.ContainerEdits{ ContainerEdits: specs.ContainerEdits{
Hooks: []*specs.Hook{ Hooks: []*specs.Hook{
{Path: "/target-root/usr/bin/nvidia-ctk"}, {
{Path: "/target-root/usr/bin/nvidia-ctk"}, Path: "/target-root/usr/bin/nvidia-ctk",
{Path: "/different-root/usr/bin/nvidia-ctk"}, Args: []string{
"--link",
"/target-root/path/to/target::/target-root/path/to/link",
},
},
{
Path: "/target-root/usr/bin/nvidia-ctk",
Args: []string{
"--link",
"/target-root/path/to/target::/target-root/path/to/link",
},
},
{
Path: "/different-root/usr/bin/nvidia-ctk",
Args: []string{
"--link",
"/different-root/path/to/target::/different-root/path/to/link",
},
},
}, },
}, },
}, },