mirror of
https://github.com/containers/podman.git
synced 2025-10-17 19:24:04 +08:00
Check for duplicate names when generating new container and pod names.
This fixes the situation where we fail to create a container when a name already exists. Signed-off-by: Daniel J Walsh <dwalsh@redhat.com> Closes: #517 Approved by: baude
This commit is contained in:

committed by
Atomic Bot

parent
f936b745b6
commit
c54816dfc3
@ -706,7 +706,7 @@ func (s *BoltState) LookupPod(idOrName string) (*Pod, error) {
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !exists {
|
||||
return errors.Wrapf(ErrNoSuchCtr, "no pod with name or ID %s found", idOrName)
|
||||
return errors.Wrapf(ErrNoSuchPod, "no pod with name or ID %s found", idOrName)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,6 @@ import (
|
||||
"github.com/containers/storage/pkg/archive"
|
||||
"github.com/containers/storage/pkg/chrootarchive"
|
||||
"github.com/docker/docker/pkg/mount"
|
||||
"github.com/docker/docker/pkg/namesgenerator"
|
||||
"github.com/docker/docker/pkg/stringid"
|
||||
spec "github.com/opencontainers/runtime-spec/specs-go"
|
||||
"github.com/opencontainers/runtime-tools/generate"
|
||||
@ -150,7 +149,6 @@ func newContainer(rspec *spec.Spec, lockDir string) (*Container, error) {
|
||||
ctr.state = new(containerState)
|
||||
|
||||
ctr.config.ID = stringid.GenerateNonCryptoID()
|
||||
ctr.config.Name = namesgenerator.GetRandomName(0)
|
||||
|
||||
ctr.config.Spec = new(spec.Spec)
|
||||
deepcopier.Copy(rspec).To(ctr.config.Spec)
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/containers/storage"
|
||||
"github.com/docker/docker/pkg/namesgenerator"
|
||||
"github.com/docker/docker/pkg/stringid"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
@ -52,7 +51,6 @@ func newPod(lockDir string, runtime *Runtime) (*Pod, error) {
|
||||
pod := new(Pod)
|
||||
pod.config = new(PodConfig)
|
||||
pod.config.ID = stringid.GenerateNonCryptoID()
|
||||
pod.config.Name = namesgenerator.GetRandomName(0)
|
||||
pod.config.Labels = make(map[string]string)
|
||||
pod.runtime = runtime
|
||||
|
||||
|
@ -12,6 +12,7 @@ import (
|
||||
"github.com/containers/image/types"
|
||||
"github.com/containers/storage"
|
||||
"github.com/cri-o/ocicni/pkg/ocicni"
|
||||
"github.com/docker/docker/pkg/namesgenerator"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/projectatomic/libpod/libpod/image"
|
||||
"github.com/sirupsen/logrus"
|
||||
@ -562,6 +563,31 @@ func (r *Runtime) Info() ([]InfoData, error) {
|
||||
return info, nil
|
||||
}
|
||||
|
||||
// generateName generates a unigue name for a container or pod.
|
||||
func (r *Runtime) generateName() (string, error) {
|
||||
for {
|
||||
name := namesgenerator.GetRandomName(0)
|
||||
// Make sure container with this name does not exist
|
||||
if _, err := r.state.LookupContainer(name); err == nil {
|
||||
continue
|
||||
} else {
|
||||
if errors.Cause(err) != ErrNoSuchCtr {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
// Make sure pod with this name does not exist
|
||||
if _, err := r.state.LookupPod(name); err == nil {
|
||||
continue
|
||||
} else {
|
||||
if errors.Cause(err) != ErrNoSuchPod {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
// The code should never reach here.
|
||||
}
|
||||
|
||||
// SaveDefaultConfig saves a copy of the default config at the given path
|
||||
func SaveDefaultConfig(path string) error {
|
||||
var w bytes.Buffer
|
||||
|
@ -50,6 +50,15 @@ func (r *Runtime) NewContainer(rSpec *spec.Spec, options ...CtrCreateOption) (c
|
||||
ctr.state.State = ContainerStateConfigured
|
||||
ctr.runtime = r
|
||||
|
||||
if ctr.config.Name == "" {
|
||||
name, err := r.generateName()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctr.config.Name = name
|
||||
}
|
||||
|
||||
// Set up storage for the container
|
||||
if err := ctr.setupStorage(); err != nil {
|
||||
return nil, errors.Wrapf(err, "error configuring storage for container")
|
||||
|
@ -35,6 +35,14 @@ func (r *Runtime) NewPod(options ...PodCreateOption) (*Pod, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if pod.config.Name == "" {
|
||||
name, err := r.generateName()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pod.config.Name = name
|
||||
}
|
||||
|
||||
pod.valid = true
|
||||
|
||||
if err := r.state.AddPod(pod); err != nil {
|
||||
|
Reference in New Issue
Block a user