From 6be6d0a3b3b4d38410e4329c92c4e197d5de2abc Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Mon, 14 Feb 2022 09:40:42 +0100 Subject: [PATCH] katautils: Pass the OCI annotations back to the called OCI hooks That allows us to amend those annotations with information that could be used when running those hooks. For example nerdctl will use those annotations to resolve the networking namespace path in where to run the CNI plugin, i.e. the created pod networking namespace. Fixes #3629 Signed-off-by: Samuel Ortiz --- src/runtime/pkg/katautils/hook.go | 19 ++++++++++--------- src/runtime/pkg/katautils/hook_test.go | 9 +++++---- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/runtime/pkg/katautils/hook.go b/src/runtime/pkg/katautils/hook.go index 62eb81169c..16c316fea9 100644 --- a/src/runtime/pkg/katautils/hook.go +++ b/src/runtime/pkg/katautils/hook.go @@ -32,15 +32,16 @@ func hookLogger() *logrus.Entry { return kataUtilsLogger.WithField("subsystem", "hook") } -func runHook(ctx context.Context, hook specs.Hook, cid, bundlePath string) error { +func runHook(ctx context.Context, spec specs.Spec, hook specs.Hook, cid, bundlePath string) error { span, _ := katatrace.Trace(ctx, hookLogger(), "runHook", hookTracingTags) defer span.End() katatrace.AddTags(span, "path", hook.Path, "args", hook.Args) state := specs.State{ - Pid: syscall.Gettid(), - Bundle: bundlePath, - ID: cid, + Pid: syscall.Gettid(), + Bundle: bundlePath, + ID: cid, + Annotations: spec.Annotations, } stateJSON, err := json.Marshal(state) @@ -90,13 +91,13 @@ func runHook(ctx context.Context, hook specs.Hook, cid, bundlePath string) error return nil } -func runHooks(ctx context.Context, hooks []specs.Hook, cid, bundlePath, hookType string) error { +func runHooks(ctx context.Context, spec specs.Spec, hooks []specs.Hook, cid, bundlePath, hookType string) error { span, ctx := katatrace.Trace(ctx, hookLogger(), "runHooks", hookTracingTags) katatrace.AddTags(span, "type", hookType) defer span.End() for _, hook := range hooks { - if err := runHook(ctx, hook, cid, bundlePath); err != nil { + if err := runHook(ctx, spec, hook, cid, bundlePath); err != nil { hookLogger().WithFields(logrus.Fields{ "hook-type": hookType, "error": err, @@ -116,7 +117,7 @@ func PreStartHooks(ctx context.Context, spec specs.Spec, cid, bundlePath string) return nil } - return runHooks(ctx, spec.Hooks.Prestart, cid, bundlePath, "pre-start") + return runHooks(ctx, spec, spec.Hooks.Prestart, cid, bundlePath, "pre-start") } // PostStartHooks run the hooks just after start container @@ -126,7 +127,7 @@ func PostStartHooks(ctx context.Context, spec specs.Spec, cid, bundlePath string return nil } - return runHooks(ctx, spec.Hooks.Poststart, cid, bundlePath, "post-start") + return runHooks(ctx, spec, spec.Hooks.Poststart, cid, bundlePath, "post-start") } // PostStopHooks run the hooks after stop container @@ -136,5 +137,5 @@ func PostStopHooks(ctx context.Context, spec specs.Spec, cid, bundlePath string) return nil } - return runHooks(ctx, spec.Hooks.Poststop, cid, bundlePath, "post-stop") + return runHooks(ctx, spec, spec.Hooks.Poststop, cid, bundlePath, "post-stop") } diff --git a/src/runtime/pkg/katautils/hook_test.go b/src/runtime/pkg/katautils/hook_test.go index 54acfecc06..6109b55499 100644 --- a/src/runtime/pkg/katautils/hook_test.go +++ b/src/runtime/pkg/katautils/hook_test.go @@ -57,26 +57,27 @@ func TestRunHook(t *testing.T) { assert := assert.New(t) ctx := context.Background() + spec := specs.Spec{} // Run with timeout 0 hook := createHook(0) - err := runHook(ctx, hook, testSandboxID, testBundlePath) + err := runHook(ctx, spec, hook, testSandboxID, testBundlePath) assert.NoError(err) // Run with timeout 1 hook = createHook(1) - err = runHook(ctx, hook, testSandboxID, testBundlePath) + err = runHook(ctx, spec, hook, testSandboxID, testBundlePath) assert.NoError(err) // Run timeout failure hook = createHook(1) hook.Args = append(hook.Args, "2") - err = runHook(ctx, hook, testSandboxID, testBundlePath) + err = runHook(ctx, spec, hook, testSandboxID, testBundlePath) assert.Error(err) // Failure due to wrong hook hook = createWrongHook() - err = runHook(ctx, hook, testSandboxID, testBundlePath) + err = runHook(ctx, spec, hook, testSandboxID, testBundlePath) assert.Error(err) }