diff options
author | Matthew Heon <matthew.heon@gmail.com> | 2017-12-13 16:58:14 -0500 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2017-12-14 18:18:16 +0000 |
commit | 6d297688644a73c23fce7fbbaa5b402eca4d85d8 (patch) | |
tree | 8219e44ba61f50a913031d85b68fdaa23ee09606 /libpod/pod.go | |
parent | d8f099bb5a38b85bdea2bf6d99e0dbd0457ab666 (diff) | |
download | podman-6d297688644a73c23fce7fbbaa5b402eca4d85d8.tar.gz podman-6d297688644a73c23fce7fbbaa5b402eca4d85d8.tar.bz2 podman-6d297688644a73c23fce7fbbaa5b402eca4d85d8.zip |
Update pods to use file locks
Also includes misc other fixes - adding labels, fixing pod names
Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
Closes: #138
Approved by: rhatdan
Diffstat (limited to 'libpod/pod.go')
-rw-r--r-- | libpod/pod.go | 57 |
1 files changed, 44 insertions, 13 deletions
diff --git a/libpod/pod.go b/libpod/pod.go index 48a761d57..b336c2677 100644 --- a/libpod/pod.go +++ b/libpod/pod.go @@ -1,21 +1,25 @@ package libpod import ( - "sync" + "os" + "path/filepath" + "github.com/containers/storage" + "github.com/docker/docker/pkg/namesgenerator" "github.com/docker/docker/pkg/stringid" "github.com/pkg/errors" ) // Pod represents a group of containers that may share namespaces type Pod struct { - id string - name string + id string + name string + labels map[string]string containers map[string]*Container valid bool - lock sync.RWMutex + lock storage.Locker } // ID retrieves the pod's ID @@ -28,14 +32,43 @@ func (p *Pod) Name() string { return p.name } -// Creates a new pod -func newPod() (*Pod, error) { +// Labels returns the pod's labels +func (p *Pod) Labels() map[string]string { + labels := make(map[string]string) + for key, value := range p.labels { + labels[key] = value + } + + return labels +} + +// Creates a new, empty pod +func newPod(lockDir string) (*Pod, error) { pod := new(Pod) pod.id = stringid.GenerateNonCryptoID() - pod.name = pod.id // TODO generate human-readable name here + pod.name = namesgenerator.GetRandomName(0) pod.containers = make(map[string]*Container) + // TODO: containers and pods share a locks folder, but not tables in the + // database + // As the locks are 256-bit pseudorandom integers, collision is unlikely + // But it's something worth looking into + + // Path our lock file will reside at + lockPath := filepath.Join(lockDir, pod.id) + // Ensure there is no conflict - file does not exist + _, err := os.Stat(lockPath) + if err == nil || !os.IsNotExist(err) { + return nil, errors.Wrapf(ErrCtrExists, "lockfile for pod ID %s already exists", pod.id) + } + // Grab a lockfile at the given path + lock, err := storage.GetLockfile(lockPath) + if err != nil { + return nil, errors.Wrapf(err, "error creating lockfile for new pod") + } + pod.lock = lock + return pod, nil } @@ -45,8 +78,6 @@ func newPod() (*Pod, error) { func (p *Pod) addContainer(ctr *Container) error { p.lock.Lock() defer p.lock.Unlock() - ctr.lock.Lock() - defer ctr.lock.Unlock() if !p.valid { return ErrPodRemoved @@ -101,8 +132,8 @@ func (p *Pod) Kill(signal uint) error { // HasContainer checks if a container is present in the pod func (p *Pod) HasContainer(id string) (bool, error) { - p.lock.RLock() - defer p.lock.RUnlock() + p.lock.Lock() + defer p.lock.Unlock() if !p.valid { return false, ErrPodRemoved @@ -115,8 +146,8 @@ func (p *Pod) HasContainer(id string) (bool, error) { // GetContainers retrieves the containers in the pod func (p *Pod) GetContainers() ([]*Container, error) { - p.lock.RLock() - defer p.lock.RUnlock() + p.lock.Lock() + defer p.lock.Unlock() if !p.valid { return nil, ErrPodRemoved |