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 <s.ortiz@apple.com>
This commit is contained in:
Samuel Ortiz 2022-02-14 09:40:42 +01:00
parent a3b3274121
commit 6be6d0a3b3
2 changed files with 15 additions and 13 deletions

View File

@ -32,7 +32,7 @@ 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)
@ -41,6 +41,7 @@ func runHook(ctx context.Context, hook specs.Hook, cid, bundlePath string) error
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")
}

View File

@ -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)
}