Skip to content

Commit

Permalink
Allow '/' to prefix container names to match Docker
Browse files Browse the repository at this point in the history
Fixes: containers#16663

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
  • Loading branch information
rhatdan committed Dec 22, 2022
1 parent d20dbcd commit 7d5038c
Show file tree
Hide file tree
Showing 31 changed files with 69 additions and 15 deletions.
3 changes: 2 additions & 1 deletion cmd/podman/containers/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package containers
import (
"errors"
"os"
"strings"

"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/registry"
Expand Down Expand Up @@ -75,7 +76,7 @@ func attach(cmd *cobra.Command, args []string) error {

var name string
if len(args) > 0 {
name = args[0]
name = strings.TrimPrefix(args[0], "/")
}
attachOpts.Stdin = os.Stdin
if attachOpts.NoStdin {
Expand Down
1 change: 1 addition & 0 deletions cmd/podman/containers/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func init() {

func checkpoint(cmd *cobra.Command, args []string) error {
var errs utils.OutputErrors
args = utils.RemoveSlash(args)
podmanStart := time.Now()
if cmd.Flags().Changed("compress") {
if checkpointOptions.Export == "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/containers/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func init() {
}

func commit(cmd *cobra.Command, args []string) error {
container := args[0]
container := strings.TrimPrefix(args[0], "/")
if len(args) == 2 {
commitOptions.ImageName = args[1]
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/podman/containers/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strings"

"github.com/containers/common/pkg/completion"
"github.com/containers/podman/v4/cmd/podman/common"
Expand Down Expand Up @@ -112,7 +113,7 @@ func exec(_ *cobra.Command, args []string) error {
execOpts.Cmd = args
if !execOpts.Latest {
execOpts.Cmd = args[1:]
nameOrID = args[0]
nameOrID = strings.TrimPrefix(args[0], "/")
}
// Validate given environment variables
execOpts.Envs = make(map[string]string)
Expand Down
3 changes: 2 additions & 1 deletion cmd/podman/containers/exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package containers

import (
"context"
"strings"

"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/registry"
Expand Down Expand Up @@ -41,7 +42,7 @@ func exists(cmd *cobra.Command, args []string) error {
options := entities.ContainerExistsOptions{
External: external,
}
response, err := registry.ContainerEngine().ContainerExists(context.Background(), args[0], options)
response, err := registry.ContainerEngine().ContainerExists(context.Background(), strings.TrimPrefix(args[0], "/"), options)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/podman/containers/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"os"
"strings"

"github.com/containers/common/pkg/completion"
"github.com/containers/podman/v4/cmd/podman/common"
Expand Down Expand Up @@ -87,5 +88,5 @@ func export(cmd *cobra.Command, args []string) error {
defer file.Close()
exportOpts.Output = file
}
return registry.ContainerEngine().ContainerExport(context.Background(), args[0], exportOpts)
return registry.ContainerEngine().ContainerExport(context.Background(), strings.TrimPrefix(args[0], "/"), exportOpts)
}
1 change: 1 addition & 0 deletions cmd/podman/containers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func init() {

func initContainer(cmd *cobra.Command, args []string) error {
var errs utils.OutputErrors
args = utils.RemoveSlash(args)
report, err := registry.ContainerEngine().ContainerInit(registry.GetContext(), args, initOptions)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions cmd/podman/containers/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func kill(_ *cobra.Command, args []string) error {
err error
errs utils.OutputErrors
)
args = utils.RemoveSlash(args)
// Check if the signalString provided by the user is valid
// Invalid signals will return err
sig, err := signal.ParseSignalNameOrNumber(killOptions.Signal)
Expand Down
2 changes: 2 additions & 0 deletions cmd/podman/containers/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/containers/common/pkg/completion"
"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/cmd/podman/validate"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/util"
Expand Down Expand Up @@ -122,6 +123,7 @@ func logsFlags(cmd *cobra.Command) {
}

func logs(_ *cobra.Command, args []string) error {
args = utils.RemoveSlash(args)
if logsOptions.SinceRaw != "" {
// parse time, error out if something is wrong
since, err := util.ParseInputTime(logsOptions.SinceRaw, true)
Expand Down
2 changes: 2 additions & 0 deletions cmd/podman/containers/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func mount(cmd *cobra.Command, args []string) error {
if len(args) > 0 && mountOpts.Latest {
return errors.New("--latest and containers cannot be used together")
}
args = utils.RemoveSlash(args)

reports, err := registry.ContainerEngine().ContainerMount(registry.GetContext(), args, mountOpts)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions cmd/podman/containers/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func pause(cmd *cobra.Command, args []string) error {
var (
errs utils.OutputErrors
)
args = utils.RemoveSlash(args)

for _, cidFile := range pauseCidFiles {
content, err := os.ReadFile(cidFile)
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/containers/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func port(_ *cobra.Command, args []string) error {
return errors.New("you must supply a running container name or id")
}
if !portOpts.Latest && len(args) >= 1 {
container = args[0]
container = strings.TrimPrefix(args[0], "/")
}
port := ""
if len(args) > 2 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/containers/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {
_ = pruneCommand.RegisterFlagCompletionFunc(filterFlagName, common.AutocompletePruneFilters)
}

func prune(cmd *cobra.Command, args []string) error {
func prune(cmd *cobra.Command, _ []string) error {
var (
pruneOptions = entities.ContainerPruneOptions{}
err error
Expand Down
2 changes: 2 additions & 0 deletions cmd/podman/containers/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"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/spf13/cobra"
)
Expand Down Expand Up @@ -47,6 +48,7 @@ func rename(cmd *cobra.Command, args []string) error {
if len(args) > 2 {
return errors.New("must provide at least two arguments to rename")
}
args = utils.RemoveSlash(args)
renameOpts := entities.ContainerRenameOptions{
NewName: args[1],
}
Expand Down
1 change: 1 addition & 0 deletions cmd/podman/containers/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func restart(cmd *cobra.Command, args []string) error {
var (
errs utils.OutputErrors
)
args = utils.RemoveSlash(args)

if cmd.Flag("time").Changed {
restartOpts.Timeout = &restartTimeout
Expand Down
2 changes: 2 additions & 0 deletions cmd/podman/containers/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func restore(cmd *cobra.Command, args []string) error {
e error
errs utils.OutputErrors
)
args = utils.RemoveSlash(args)

podmanStart := time.Now()
if rootless.IsRootless() {
return fmt.Errorf("restoring a container requires root")
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/containers/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func rm(cmd *cobra.Command, args []string) error {
rmOptions.Depend = true
}

return removeContainers(args, rmOptions, true)
return removeContainers(utils.RemoveSlash(args), rmOptions, true)
}

// removeContainers will remove the specified containers (names or IDs).
Expand Down
3 changes: 2 additions & 1 deletion cmd/podman/containers/runlabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package containers
import (
"context"
"os"
"strings"

"github.com/containers/common/pkg/auth"
"github.com/containers/common/pkg/completion"
Expand Down Expand Up @@ -95,5 +96,5 @@ func runlabel(cmd *cobra.Command, args []string) error {
return err
}
}
return registry.ContainerEngine().ContainerRunlabel(context.Background(), args[0], args[1], args[2:], runlabelOptions.ContainerRunlabelOptions)
return registry.ContainerEngine().ContainerRunlabel(context.Background(), strings.TrimPrefix(args[0], "/"), args[1], args[2:], runlabelOptions.ContainerRunlabelOptions)
}
2 changes: 1 addition & 1 deletion cmd/podman/containers/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func start(cmd *cobra.Command, args []string) error {
startOptions.Stdout = os.Stdout
}

containers := args
containers := utils.RemoveSlash(args)
for _, f := range filters {
split := strings.SplitN(f, "=", 2)
if len(split) < 2 {
Expand Down
2 changes: 2 additions & 0 deletions cmd/podman/containers/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/containers/common/pkg/report"
"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/registry"
putils "github.com/containers/podman/v4/cmd/podman/utils"
"github.com/containers/podman/v4/cmd/podman/validate"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/domain/entities"
Expand Down Expand Up @@ -120,6 +121,7 @@ func stats(cmd *cobra.Command, args []string) error {
Stream: !statsOptions.NoStream,
Interval: statsOptions.Interval,
}
args = putils.RemoveSlash(args)
statsChan, err := registry.ContainerEngine().ContainerStats(registry.Context(), args, opts)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions cmd/podman/containers/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ func stop(cmd *cobra.Command, args []string) error {
var (
errs utils.OutputErrors
)
args = utils.RemoveSlash(args)

if cmd.Flag("time").Changed {
stopOptions.Timeout = &stopTimeout
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/containers/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func top(cmd *cobra.Command, args []string) error {
if topOptions.Latest {
topOptions.Descriptors = args
} else {
topOptions.NameOrID = args[0]
topOptions.NameOrID = strings.TrimPrefix(args[0], "/")
topOptions.Descriptors = args[1:]
}

Expand Down
1 change: 1 addition & 0 deletions cmd/podman/containers/unmount.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func init() {

func unmount(cmd *cobra.Command, args []string) error {
var errs utils.OutputErrors
args = utils.RemoveSlash(args)
reports, err := registry.ContainerEngine().ContainerUnmount(registry.GetContext(), args, unmountOpts)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions cmd/podman/containers/unpause.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func unpause(cmd *cobra.Command, args []string) error {
var (
errs utils.OutputErrors
)
args = utils.RemoveSlash(args)

if rootless.IsRootless() && !registry.IsRemote() {
cgroupv2, _ := cgroups.IsCgroup2UnifiedMode()
if !cgroupv2 {
Expand Down
3 changes: 2 additions & 1 deletion cmd/podman/containers/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package containers
import (
"context"
"fmt"
"strings"

"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/registry"
Expand Down Expand Up @@ -71,7 +72,7 @@ func update(cmd *cobra.Command, args []string) error {
}

opts := &entities.ContainerUpdateOptions{
NameOrID: args[0],
NameOrID: strings.TrimPrefix(args[0], "/"),
Specgen: s,
}
rep, err := registry.ContainerEngine().ContainerUpdate(context.Background(), opts)
Expand Down
1 change: 1 addition & 0 deletions cmd/podman/containers/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func wait(cmd *cobra.Command, args []string) error {
err error
errs utils.OutputErrors
)
args = utils.RemoveSlash(args)
if waitOptions.Interval, err = time.ParseDuration(waitInterval); err != nil {
var err1 error
if waitOptions.Interval, err1 = time.ParseDuration(waitInterval + "ms"); err1 != nil {
Expand Down
9 changes: 9 additions & 0 deletions cmd/podman/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"strings"

"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/libpod/define"
Expand Down Expand Up @@ -139,3 +140,11 @@ func IsCheckpointImage(ctx context.Context, namesOrIDs []string) (bool, error) {
}
return true, nil
}

func RemoveSlash(input []string) []string {
var output []string
for _, in := range input {
output = append(output, strings.TrimPrefix(in, "/"))
}
return output
}
2 changes: 2 additions & 0 deletions libpod/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"os"
"path/filepath"
"strings"
"syscall"

"github.com/containers/buildah/pkg/parse"
Expand Down Expand Up @@ -776,6 +777,7 @@ func WithName(name string) CtrCreateOption {
return define.ErrCtrFinalized
}

name = strings.TrimPrefix(name, "/")
// Check the name against a regex
if !define.NameRegex.MatchString(name) {
return define.RegexError
Expand Down
1 change: 1 addition & 0 deletions libpod/runtime_ctr.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (r *Runtime) RenameContainer(ctx context.Context, ctr *Container, newName s
return nil, err
}

newName = strings.TrimPrefix(newName, "/")
if newName == "" || !define.NameRegex.MatchString(newName) {
return nil, define.RegexError
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/domain/filters/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
case "name":
// we only have to match one name
return func(c *libpod.Container) bool {
return util.StringMatchRegexSlice(c.Name(), filterValues)
var filters []string
for _, f := range filterValues {
filters = append(filters, strings.ReplaceAll(f, "/", ""))

}
return util.StringMatchRegexSlice(c.Name(), filters)
}, nil
case "exited":
var exitCodes []int32
Expand Down
14 changes: 12 additions & 2 deletions test/system/030-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ echo $rand | 0 | $rand
run_podman 1 image exists $NONLOCAL_IMAGE

# Run a container, without --rm; this should block subsequent --rmi
run_podman run --name keepme $NONLOCAL_IMAGE /bin/true
run_podman run --name /keepme $NONLOCAL_IMAGE /bin/true
run_podman image exists $NONLOCAL_IMAGE

# Now try running with --rmi : it should succeed, but not remove the image
run_podman run --rmi --rm $NONLOCAL_IMAGE /bin/true
run_podman image exists $NONLOCAL_IMAGE

# Remove the stray container, and run one more time with --rmi.
run_podman rm keepme
run_podman rm /keepme
run_podman run --rmi --rm $NONLOCAL_IMAGE /bin/true
run_podman 1 image exists $NONLOCAL_IMAGE
}
Expand Down Expand Up @@ -951,4 +951,14 @@ $IMAGE--c_ok" \
run_podman stop -t 0 $cid
}

@test "podman run bad --name" {
randomname=$(random_string 30)
run_podman 125 create --name "$randomname/bad" $IMAGE
run_podman create --name "/$randomname" $IMAGE
run_podman ps -a --filter name="^/$randomname$" --format '{{ .Names }}'
is $output "$randomname" "Should be able to find container by name"
run_podman rm "/$randomname"
run_podman 125 create --name "$randomname/" $IMAGE
}

# vim: filetype=sh

0 comments on commit 7d5038c

Please sign in to comment.