Skip to content

Commit

Permalink
When removing objects specifying --force,podman should exit with 0
Browse files Browse the repository at this point in the history
This Patch will cause podman COMMAND rm --force bogus not fail

This is how Docker works, so Podman should follow this to allow existing
scripts to convert from Docker to Podman.

Fixes: containers#14612
Oprignal version of this patch came from wufan [email protected]

Signed-off-by: Daniel J Walsh <[email protected]>
  • Loading branch information
rhatdan committed Jul 26, 2022
1 parent 242639f commit 75419c5
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 4 deletions.
6 changes: 6 additions & 0 deletions cmd/podman/containers/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ func removeContainers(namesOrIDs []string, rmOptions entities.RmOptions, setExit
var errs utils.OutputErrors
responses, err := registry.ContainerEngine().ContainerRm(context.Background(), namesOrIDs, rmOptions)
if err != nil {
if rmOptions.Force && strings.Contains(err.Error(), define.ErrNoSuchCtr.Error()) {
return nil
}
if setExit {
setExitCode(err)
}
Expand All @@ -136,6 +139,9 @@ func removeContainers(namesOrIDs []string, rmOptions entities.RmOptions, setExit
if errors.Is(r.Err, define.ErrWillDeadlock) {
logrus.Errorf("Potential deadlock detected - please run 'podman system renumber' to resolve")
}
if rmOptions.Force && strings.Contains(r.Err.Error(), define.ErrNoSuchCtr.Error()) {
continue
}
if setExit {
setExitCode(r.Err)
}
Expand Down
18 changes: 16 additions & 2 deletions cmd/podman/images/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package images
import (
"errors"
"fmt"
"strings"

"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/cmd/podman/utils"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/errorhandling"
"github.com/containers/storage/types"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -81,8 +84,19 @@ func rm(cmd *cobra.Command, args []string) error {
fmt.Println("Deleted: " + d)
}
}
registry.SetExitCode(report.ExitCode)
for _, err := range rmErrors {
if !imageOpts.Force || !strings.Contains(err.Error(), types.ErrImageUnknown.Error()) {
registry.SetExitCode(report.ExitCode)
}
}
}

return errorhandling.JoinErrors(rmErrors)
var errs utils.OutputErrors
for _, err := range rmErrors {
if imageOpts.Force && strings.Contains(err.Error(), types.ErrImageUnknown.Error()) {
continue
}
errs = append(errs, err)
}
return errorhandling.JoinErrors(errs)
}
6 changes: 6 additions & 0 deletions cmd/podman/networks/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,19 @@ func networkRm(cmd *cobra.Command, args []string) error {
}
responses, err := registry.ContainerEngine().NetworkRm(registry.Context(), args, networkRmOptions)
if err != nil {
if networkRmOptions.Force && strings.Contains(err.Error(), define.ErrNoSuchNetwork.Error()) {
return nil
}
setExitCode(err)
return err
}
for _, r := range responses {
if r.Err == nil {
fmt.Println(r.Name)
} else {
if networkRmOptions.Force && strings.Contains(r.Err.Error(), define.ErrNoSuchNetwork.Error()) {
continue
}
setExitCode(r.Err)
errs = append(errs, r.Err)
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/podman/pods/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ func removePods(namesOrIDs []string, rmOptions entities.PodRmOptions, printIDs b

responses, err := registry.ContainerEngine().PodRm(context.Background(), namesOrIDs, rmOptions)
if err != nil {
if rmOptions.Force && strings.Contains(err.Error(), define.ErrNoSuchPod.Error()) {
return nil
}
setExitCode(err)
return err
}
Expand All @@ -104,13 +107,15 @@ func removePods(namesOrIDs []string, rmOptions entities.PodRmOptions, printIDs b
fmt.Println(r.Id)
}
} else {
if rmOptions.Force && strings.Contains(r.Err.Error(), define.ErrNoSuchPod.Error()) {
continue
}
setExitCode(r.Err)
errs = append(errs, r.Err)
}
}
return errs.PrintErrors()
}

func setExitCode(err error) {
if errors.Is(err, define.ErrNoSuchPod) || strings.Contains(err.Error(), define.ErrNoSuchPod.Error()) {
registry.SetExitCode(1)
Expand Down
6 changes: 6 additions & 0 deletions cmd/podman/volumes/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,19 @@ func rm(cmd *cobra.Command, args []string) error {
}
responses, err := registry.ContainerEngine().VolumeRm(context.Background(), args, rmOptions)
if err != nil {
if rmOptions.Force && strings.Contains(err.Error(), define.ErrNoSuchVolume.Error()) {
return nil
}
setExitCode(err)
return err
}
for _, r := range responses {
if r.Err == nil {
fmt.Println(r.Id)
} else {
if rmOptions.Force && strings.Contains(r.Err.Error(), define.ErrNoSuchVolume.Error()) {
continue
}
setExitCode(r.Err)
errs = append(errs, r.Err)
}
Expand Down
7 changes: 7 additions & 0 deletions test/system/010-images.bats
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,11 @@ Deleted: $pauseID"
is "$output" ""
}

@test "podman image rm --force bogus" {
run_podman 1 image rm bogus
is "$output" "Error: bogus: image not known" "Should print error"
run_podman image rm --force bogus
is "$output" "" "Should print no output"
}

# vim: filetype=sh
7 changes: 7 additions & 0 deletions test/system/055-rm.bats
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,11 @@ load helpers
run_podman 137 run --name $rand $IMAGE sleep 30
}

@test "podman container rm --force bogus" {
run_podman 1 container rm bogus
is "$output" "Error: no container with name or ID \"bogus\" found: no such container" "Should print error"
run_podman container rm --force bogus
is "$output" "" "Should print no output"
}

# vim: filetype=sh
7 changes: 7 additions & 0 deletions test/system/160-volumes.bats
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,11 @@ EOF
run_podman image rm --force localhost/volume_image
}

@test "podman volume rm --force bogus" {
run_podman 1 volume rm bogus
is "$output" "Error: no volume with name \"bogus\" found: no such volume" "Should print error"
run_podman volume rm --force bogus
is "$output" "" "Should print no output"
}

# vim: filetype=sh
7 changes: 7 additions & 0 deletions test/system/200-pod.bats
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,11 @@ io.max | $lomajmin rbps=1048576 wbps=1048576 riops=max wiops=max
wait
}

@test "podman pod rm --force bogus" {
run_podman 1 pod rm bogus
is "$output" "Error: .*bogus.*: no such pod" "Should print error"
run_podman pod rm --force bogus
is "$output" "" "Should print no output"
}

# vim: filetype=sh
9 changes: 8 additions & 1 deletion test/system/500-networking.bats
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ load helpers

run_podman rm -t 0 -f $cid
run_podman network rm $mynetname
run_podman 1 network rm -f $mynetname
run_podman 1 network rm $mynetname
}

@test "podman network reload" {
Expand Down Expand Up @@ -760,4 +760,11 @@ EOF
done
}

@test "podman network rm --force bogus" {
run_podman 1 network rm bogus
is "$output" "Error: unable to find network with name or ID bogus: network not found" "Should print error"
run_podman network rm --force bogus
is "$output" "" "Should print no output"
}

# vim: filetype=sh

0 comments on commit 75419c5

Please sign in to comment.