From 45160b88a40171ff907224bb9fc8e7bde7b9a13e Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Wed, 6 Apr 2022 16:18:56 +0200 Subject: [PATCH] Remove exsiting NVIDIA Container Runtime Hooks from the spec Signed-off-by: Evan Lezar --- .../modifier/experimental_test.go | 14 ++++++-------- .../modifier/hook_remover.go | 12 ++++++++++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/cmd/nvidia-container-runtime/modifier/experimental_test.go b/cmd/nvidia-container-runtime/modifier/experimental_test.go index 9e09873a..34f83bf4 100644 --- a/cmd/nvidia-container-runtime/modifier/experimental_test.go +++ b/cmd/nvidia-container-runtime/modifier/experimental_test.go @@ -231,7 +231,7 @@ func TestExperimentalModifier(t *testing.T) { }, }, { - description: "modification fails for existing nvidia-container-runtime-hook", + description: "modification removes existing nvidia-container-runtime-hook", spec: &specs.Spec{ Hooks: &specs.Hooks{ Prestart: []specs.Hook{ @@ -254,20 +254,19 @@ func TestExperimentalModifier(t *testing.T) { return hooks, nil }, }, - expectedError: fmt.Errorf("nvidia-container-runtime-hook already exists"), expectedSpec: &specs.Spec{ Hooks: &specs.Hooks{ Prestart: []specs.Hook{ { - Path: "/path/to/nvidia-container-runtime-hook", - Args: []string{"/path/to/nvidia-container-runtime-hook", "prestart"}, + Path: "/hook/b", + Args: []string{"/hook/b", "argb"}, }, }, }, }, }, { - description: "modification fails for existing nvidia-container-toolkit", + description: "modification removes existing nvidia-container-toolkit", spec: &specs.Spec{ Hooks: &specs.Hooks{ Prestart: []specs.Hook{ @@ -290,13 +289,12 @@ func TestExperimentalModifier(t *testing.T) { return hooks, nil }, }, - expectedError: fmt.Errorf("nvidia-container-toolkit already exists"), expectedSpec: &specs.Spec{ Hooks: &specs.Hooks{ Prestart: []specs.Hook{ { - Path: "/path/to/nvidia-container-toolkit", - Args: []string{"/path/to/nvidia-container-toolkit", "prestart"}, + Path: "/hook/b", + Args: []string{"/hook/b", "argb"}, }, }, }, diff --git a/cmd/nvidia-container-runtime/modifier/hook_remover.go b/cmd/nvidia-container-runtime/modifier/hook_remover.go index 7df4b2f1..16fe82d4 100644 --- a/cmd/nvidia-container-runtime/modifier/hook_remover.go +++ b/cmd/nvidia-container-runtime/modifier/hook_remover.go @@ -17,7 +17,6 @@ package modifier import ( - "fmt" "path/filepath" "github.com/NVIDIA/nvidia-container-toolkit/internal/oci" @@ -46,10 +45,19 @@ func (m nvidiaContainerRuntimeHookRemover) Modify(spec *specs.Spec) error { return nil } + var newPrestart []specs.Hook + for _, hook := range spec.Hooks.Prestart { if isNVIDIAContainerRuntimeHook(&hook) { - return fmt.Errorf("spec already contains required 'prestart' hook") + m.logger.Debugf("Removing hook %v", hook) + continue } + newPrestart = append(newPrestart, hook) + } + + if len(newPrestart) != len(spec.Hooks.Prestart) { + m.logger.Debugf("Updating 'prestart' hooks to %v", newPrestart) + spec.Hooks.Prestart = newPrestart } return nil