Define better error message for container name conflicts with external storage.

Updated the error message to suggest user to use --replace option to instruct Podman to replace the existsing external container with a newly created one.

closes #16759

Signed-off-by: Chetan Giradkar <cgiradka@redhat.com>
This commit is contained in:
Chetan Giradkar
2023-10-16 11:29:32 +01:00
parent b5fec41f26
commit 2d65e57ae6
3 changed files with 36 additions and 2 deletions

View File

@ -21,6 +21,7 @@ import (
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/parallel"
"github.com/containers/podman/v4/version"
"github.com/containers/storage"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
@ -626,7 +627,8 @@ func rootFlags(cmd *cobra.Command, podmanConfig *entities.PodmanConfig) {
func formatError(err error) string {
var message string
if errors.Is(err, define.ErrOCIRuntime) {
switch {
case errors.Is(err, define.ErrOCIRuntime):
// OCIRuntimeErrors include the reason for the failure in the
// second to last message in the error chain.
message = fmt.Sprintf(
@ -634,7 +636,9 @@ func formatError(err error) string {
define.ErrOCIRuntime.Error(),
strings.TrimSuffix(err.Error(), ": "+define.ErrOCIRuntime.Error()),
)
} else {
case errors.Is(err, storage.ErrDuplicateName):
message = fmt.Sprintf("Error: %s, or use --replace to instruct Podman to do so.", err.Error())
default:
if logrus.IsLevelEnabled(logrus.TraceLevel) {
message = fmt.Sprintf("Error: %+v", err)
} else {

View File

@ -496,6 +496,11 @@ func (c *Container) setupStorage(ctx context.Context) error {
}
}
if containerInfoErr != nil {
if errors.Is(containerInfoErr, storage.ErrDuplicateName) {
if _, err := c.runtime.LookupContainer(c.config.Name); errors.Is(err, define.ErrNoSuchCtr) {
return fmt.Errorf("creating container storage: %w by an external entity", containerInfoErr)
}
}
return fmt.Errorf("creating container storage: %w", containerInfoErr)
}

View File

@ -1303,4 +1303,29 @@ search | $IMAGE |
run_podman rm -f -t0 $cid
}
@test "podman create container with conflicting name" {
local cname="$(random_string 10 | tr A-Z a-z)"
local output_msg_ext="^Error: .*: the container name \"$cname\" is already in use by .* You have to remove that container to be able to reuse that name: that name is already in use by an external entity, or use --replace to instruct Podman to do so."
local output_msg="^Error: .*: the container name \"$cname\" is already in use by .* You have to remove that container to be able to reuse that name: that name is already in use, or use --replace to instruct Podman to do so."
if is_remote; then
output_msg_ext="^Error: .*: the container name \"$cname\" is already in use by .* You have to remove that container to be able to reuse that name: that name is already in use by an external entity"
output_msg="^Error: .*: the container name \"$cname\" is already in use by .* You have to remove that container to be able to reuse that name: that name is already in use"
fi
# external container
buildah from --name $cname scratch
run_podman 125 create --name $cname $IMAGE
assert "$output" =~ "$output_msg_ext" "Trying to create two containers with same name"
run_podman container rm $cname
run_podman --noout create --name $cname $IMAGE
run_podman 125 create --name $cname $IMAGE
assert "$output" =~ "$output_msg" "Trying to create two containers with same name"
run_podman container rm $cname
}
# vim: filetype=sh