Skip to content

Commit

Permalink
rm pod with podman run if ctr creation failed
Browse files Browse the repository at this point in the history
Currently, if the container creation failed with
either run or create and you've used --pod with new:
the pod would be created nonetheless. This change ensures
the pod just created is also cleaned up in case
of container creation failure

Fixes containers#21228

Signed-off-by: danishprakash <danish.prakash@suse.com>
  • Loading branch information
danishprakash committed Jan 16, 2024
1 parent 9fed92b commit 6aa026a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cmd/podman/containers/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ func create(cmd *cobra.Command, args []string) error {

report, err := registry.ContainerEngine().ContainerCreate(registry.GetContext(), s)
if err != nil {
// if pod was created as part of run
// remove it in case ctr creation fails
_ = rmPodIfNecessary(cmd, s)
return err
}

Expand Down Expand Up @@ -385,6 +388,18 @@ func PullImage(imageName string, cliVals *entities.ContainerCreateOptions) (stri
return imageName, nil
}

func rmPodIfNecessary(cmd *cobra.Command, s *specgen.SpecGenerator) error {
if !strings.HasPrefix(cmd.Flag("pod").Value.String(), "new:") {
return nil
}

// errcheck not necessary since
// pod creation would've failed
podName := strings.Replace(s.Pod, "new:", "", 1)
_, err := registry.ContainerEngine().PodRm(context.Background(), []string{podName}, entities.PodRmOptions{})
return err
}

// createPodIfNecessary automatically creates a pod when requested. if the pod name
// has the form new:ID, the pod ID is created and the name in the spec generator is replaced
// with ID.
Expand Down
3 changes: 3 additions & 0 deletions cmd/podman/containers/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ func run(cmd *cobra.Command, args []string) error {
registry.SetExitCode(report.ExitCode)
}
if err != nil {
// if pod was created as part of run
// remove it in case ctr creation fails
_ = rmPodIfNecessary(cmd, s)
return err
}

Expand Down
15 changes: 15 additions & 0 deletions test/system/030-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1399,4 +1399,19 @@ search | $IMAGE |
run_podman rm -f -t0 $cid
}

@test "podman run - rm pod if container creation failed with -pod new:" {
run_podman run -d --name foobar $IMAGE hostname
cid=$output

podname=pod$(random_string)
run_podman 125 run --rm --pod "new:$podname" --name foobar $IMAGE hostname
is "$output" ".*creating container storage: the container name \"foobar\" is already in use by"

# pod should've been cleaned up
# if container creation failed
run_podman 1 pod exists $podname

run_podman rm -f $cid
}

# vim: filetype=sh
Empty file added test/system/danish.bats
Empty file.

0 comments on commit 6aa026a

Please sign in to comment.