Skip to content

Commit

Permalink
remove pod if creation has failed
Browse files Browse the repository at this point in the history
Make sure to remove the pod if its creation has failed.

Fixes: containers#16502
Signed-off-by: Valentin Rothberg <[email protected]>
  • Loading branch information
vrothberg committed Nov 15, 2022
1 parent b73c0f2 commit 9610d4c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pkg/specgen/generate/pod_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ import (
"github.com/sirupsen/logrus"
)

func MakePod(p *entities.PodSpec, rt *libpod.Runtime) (*libpod.Pod, error) {
func MakePod(p *entities.PodSpec, rt *libpod.Runtime) (_ *libpod.Pod, finalErr error) {
var createdPod *libpod.Pod
defer func() {
if finalErr != nil && createdPod != nil {
if err := rt.RemovePod(context.Background(), createdPod, true, true, nil); err != nil {
logrus.Errorf("Removing pod: %v", err)
}
}
}()
if err := p.PodSpecGen.Validate(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -69,6 +77,8 @@ func MakePod(p *entities.PodSpec, rt *libpod.Runtime) (*libpod.Pod, error) {
if err != nil {
return nil, err
}
createdPod = pod

if !p.PodSpecGen.NoInfra && p.PodSpecGen.InfraContainerSpec != nil {
if p.PodSpecGen.InfraContainerSpec.Name == "" {
p.PodSpecGen.InfraContainerSpec.Name = pod.ID()[:12] + "-infra"
Expand Down
13 changes: 13 additions & 0 deletions test/system/200-pod.bats
Original file line number Diff line number Diff line change
Expand Up @@ -554,4 +554,17 @@ io.max | $lomajmin rbps=1048576 wbps=1048576 riops=max wiops=max
is "$output" "" "Should print no output"
}

@test "podman pod create on failure" {
podname=pod$(random_string)
nwname=pod$(random_string)

run_podman 125 pod create --network $nwname --name $podname
# FIXME: podman and podman-remote do not return the same error message
# but consistency would be nice
is "$output" "Error: .*unable to find network with name or ID $nwname: network not found"

# Make sure the pod doesn't get created on failure
run_podman 1 pod exists $podname
}

# vim: filetype=sh

0 comments on commit 9610d4c

Please sign in to comment.