Skip to content

Add support for crun hook stdout/stderr annotations in precreate hooks - #1089

Open
ajoshua2004 wants to merge 1 commit into
podman-container-tools:mainfrom
ajoshua2004:hook-annotations
Open

Add support for crun hook stdout/stderr annotations in precreate hooks#1089
ajoshua2004 wants to merge 1 commit into
podman-container-tools:mainfrom
ajoshua2004:hook-annotations

Conversation

@ajoshua2004

Copy link
Copy Markdown
Contributor

OCI hooks can redirect their output to a file via 2 crun annotations run.oci.hooks.stdout=FILE/run.oci.hooks.stderr=FILE This works for every hook type except precreatehooks, since those are executed directly by podman before the OCI runtime is invoked so crun doesnt get a chance to honor the annotations. This adds the same annotation support to RuntimeConfigFilterWithOptions so precreate hook output anc be redirected too. PR in podman will be needed to bump vendor dependency.

Test

go test ./pkg/hooks/exec/... -run TestRuntimeConfigFilterOutputRedirection -v

@github-actions github-actions Bot added the common Related to "common" package label Aug 17, 2026
runStderr = stderrFile
}

hookErr, err = RunWithOptions(ctx, RunOptions{Hook: &hook, Dir: options.Dir, State: data, Stdout: runStdout, Stderr: runStderr, PostKillTimeout: options.PostKillTimeout})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless I am missing something Stderr: stderrFile should work here without having to decalre or do a nil check above. If stderrFile is nil then we can just pass it as nil

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried this and it being a nil os.File breaks it. passing it as a nil causes os/exec to close the stderr fd instead of redirecting to /dev/null. added a regression test and it fails without the check

}

func TestRuntimeConfigFilterOutputRedirection(t *testing.T) {
ctx := context.Background()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use t.Context() for a per test context which is cancelled at the end of a test in case something leaks

Comment on lines +281 to +283
if err != nil {
t.Fatal(err)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already use github.com/stretchr/testify/assert so this should use github.com/stretchr/testify/require and then call require.NoError() for nicer error messages, same in all the other places here

Comment on lines +319 to +322
info, err := os.Stat(stdoutPath)
if err != nil {
t.Fatal(err)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well you should read the file and make it is empty for better coverage.

Comment on lines +397 to +414
input := &spec.Spec{
Version: "1.0.0",
Root: &spec.Root{Path: "rootfs"},
Annotations: map[string]string{AnnotationHookStdout: stdoutPath},
}
expectedJSON, err := json.Marshal(input)
if err != nil {
t.Fatal(err)
}

hooks := []spec.Hook{{Path: path, Args: []string{"sh", "-c", "cat"}}}
hookErr, err := RuntimeConfigFilterWithOptions(ctx, RuntimeConfigFilterOptions{Hooks: hooks, Config: input, PostKillTimeout: DefaultPostKillTimeout})
if err != nil {
t.Fatal(err)
}
if hookErr != nil {
t.Fatal(hookErr)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a lot of duplication for these tests, I think this would be written a lot better as table driven test, i.e. a loop over all test cases defined as struct where the input/output are defined once. TestRuntimeConfigFilter can serve as example of what I mean.

Precreate hooks run directly by Podman before the OCI runtime is
invoked, so they never benefit from crun's run.oci.hooks.stdout/
run.oci.hooks.stderr annotations. This adds the same support to
RuntimeConfigFilterWithOptions, opening the annotated file(s) in
append mode (creating if missing, mode 0700 to match crun) and
wiring hook stdout/stderr into them.

Signed-off-by: Joshua Arrevillaga <2004jarrevillaga@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common Related to "common" package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants