Skip to content

Commit

Permalink
oci: cleanup log path if the container failed to create
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Hunt <pehunt@redhat.com>
  • Loading branch information
haircommander authored and openshift-cherrypick-robot committed Apr 22, 2022
1 parent 9a36bb8 commit 2f4aedf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/oci/runtime_oci.go
Expand Up @@ -199,6 +199,14 @@ func (r *runtimeOCI) CreateContainer(ctx context.Context, c *Container, cgroupPa
}(); err != nil {
return err
}

defer func() {
if retErr != nil {
if err := os.Remove(c.logPath); err != nil {
log.Warnf(ctx, "Failed to remove log path %s after failing to create container: %v", c.logPath, err)
}
}
}()
/* Wait for initial setup and fork, and reap child */
err = cmd.Wait()
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/oci/runtime_vm.go
Expand Up @@ -134,6 +134,9 @@ func (r *runtimeVM) CreateContainer(ctx context.Context, c *Container, cgroupPar
if cleanupErr := r.deleteContainer(c, true); cleanupErr != nil {
log.Infof(ctx, "DeleteContainer failed for container %s: %v", c.ID(), cleanupErr)
}
if err := os.Remove(c.logPath); err != nil {
log.Warnf(ctx, "Failed to remove log path %s after failing to create container: %v", c.logPath, err)
}
}
}()

Expand Down
15 changes: 15 additions & 0 deletions test/ctr.bats
Expand Up @@ -171,6 +171,21 @@ function check_oci_annotation() {
grep -E "^[^\n]+ stderr F and some from stderr$" "$logpath"
}

@test "ctr log cleaned up if container create failed" {
start_crio
pod_id=$(crictl runp "$TESTDATA"/sandbox_config.json)

# Create a new container.
jq ' .command = ["invalid"]' \
"$TESTDATA"/container_config.json > "$newconfig"
! crictl create "$pod_id" "$newconfig" "$TESTDATA"/sandbox_config.json

# CRI-O should cleanup the log if the container failed to create
for file in "$DEFAULT_LOG_PATH/$pod_id"/*; do
[[ "$file" != "$pod_id" ]]
done
}

@test "ctr journald logging" {
if ! check_journald; then
skip "journald logging not supported"
Expand Down

0 comments on commit 2f4aedf

Please sign in to comment.