Merge pull request #21267 from danishprakash/new-pod-behavior
rm pod with run, create if ctr creation failed with --pod new:
This commit is contained in:
commit
d38ee1364d
|
|
@ -180,6 +180,13 @@ func create(cmd *cobra.Command, args []string) error {
|
||||||
|
|
||||||
report, err := registry.ContainerEngine().ContainerCreate(registry.GetContext(), s)
|
report, err := registry.ContainerEngine().ContainerCreate(registry.GetContext(), s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// if pod was created as part of run
|
||||||
|
// remove it in case ctr creation fails
|
||||||
|
if err := rmPodIfNecessary(cmd, s); err != nil {
|
||||||
|
if !errors.Is(err, define.ErrNoSuchPod) {
|
||||||
|
logrus.Error(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -385,6 +392,18 @@ func PullImage(imageName string, cliVals *entities.ContainerCreateOptions) (stri
|
||||||
return imageName, nil
|
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
|
// 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
|
// has the form new:ID, the pod ID is created and the name in the spec generator is replaced
|
||||||
// with ID.
|
// with ID.
|
||||||
|
|
|
||||||
|
|
@ -228,6 +228,13 @@ func run(cmd *cobra.Command, args []string) error {
|
||||||
registry.SetExitCode(report.ExitCode)
|
registry.SetExitCode(report.ExitCode)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// if pod was created as part of run
|
||||||
|
// remove it in case ctr creation fails
|
||||||
|
if err := rmPodIfNecessary(cmd, s); err != nil {
|
||||||
|
if !errors.Is(err, define.ErrNoSuchPod) {
|
||||||
|
logrus.Error(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1399,4 +1399,20 @@ search | $IMAGE |
|
||||||
run_podman rm -f -t0 $cid
|
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 $cid
|
||||||
|
run_podman rmi $(pause_image)
|
||||||
|
}
|
||||||
|
|
||||||
# vim: filetype=sh
|
# vim: filetype=sh
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue