summaryrefslogtreecommitdiff
path: root/libpod/in_memory_state.go
diff options
context:
space:
mode:
authorMatthew Heon <matthew.heon@gmail.com>2017-12-13 23:12:32 -0500
committerAtomic Bot <atomic-devel@projectatomic.io>2018-01-17 15:26:43 +0000
commit65d643caeb31364b59612f0f91be90894c65d703 (patch)
tree5899bac7ed634faa4f238332b9de896e4606aa74 /libpod/in_memory_state.go
parent13f004aec5c70ca5be9f6c134566abd1e62ea660 (diff)
downloadpodman-65d643caeb31364b59612f0f91be90894c65d703.tar.gz
podman-65d643caeb31364b59612f0f91be90894c65d703.tar.bz2
podman-65d643caeb31364b59612f0f91be90894c65d703.zip
Change handling for pods in libpod state
Add new functions to update pods and add/remove containers from them Use these new functions in place of manually modifying pods Signed-off-by: Matthew Heon <matthew.heon@gmail.com> Closes: #229 Approved by: rhatdan
Diffstat (limited to 'libpod/in_memory_state.go')
-rw-r--r--libpod/in_memory_state.go96
1 files changed, 83 insertions, 13 deletions
diff --git a/libpod/in_memory_state.go b/libpod/in_memory_state.go
index 93c61537f..244a1ab25 100644
--- a/libpod/in_memory_state.go
+++ b/libpod/in_memory_state.go
@@ -104,8 +104,7 @@ func (s *InMemoryState) HasContainer(id string) (bool, error) {
}
// AddContainer adds a container to the state
-// If the container belongs to a pod, the pod must already be present when the
-// container is added, and the container must be present in the pod
+// Containers in a pod cannot be added to the state
func (s *InMemoryState) AddContainer(ctr *Container) error {
if !ctr.valid {
return errors.Wrapf(ErrCtrRemoved, "container with ID %s is not valid", ctr.ID())
@@ -116,17 +115,8 @@ func (s *InMemoryState) AddContainer(ctr *Container) error {
return errors.Wrapf(ErrCtrExists, "container with ID %s already exists in state", ctr.ID())
}
- if ctr.pod != nil {
- if _, ok := s.pods[ctr.pod.ID()]; !ok {
- return errors.Wrapf(ErrNoSuchPod, "pod %s does not exist, cannot add container %s", ctr.pod.ID(), ctr.ID())
- }
-
- hasCtr, err := ctr.pod.HasContainer(ctr.ID())
- if err != nil {
- return errors.Wrapf(err, "error checking if container %s is present in pod %s", ctr.ID(), ctr.pod.ID())
- } else if !hasCtr {
- return errors.Wrapf(ErrNoSuchCtr, "container %s is not present in pod %s", ctr.ID(), ctr.pod.ID())
- }
+ if ctr.config.Pod != "" {
+ return errors.Wrapf(ErrInvalidArg, "cannot add a container that is in a pod with AddContainer, use AddContainerToPod")
}
if err := s.ctrNameIndex.Reserve(ctr.Name(), ctr.ID()); err != nil {
@@ -346,6 +336,86 @@ func (s *InMemoryState) RemovePod(pod *Pod) error {
return nil
}
+// UpdatePod updates a pod's state from the backing database
+// As in-memory states have no database this is a no-op
+func (s *InMemoryState) UpdatePod(pod *Pod) error {
+ return nil
+}
+
+// AddContainerToPod adds a container to the given pod, also adding it to the
+// state
+func (s *InMemoryState) AddContainerToPod(pod *Pod, ctr *Container) error {
+ if !pod.valid {
+ return errors.Wrapf(ErrPodRemoved, "pod %s is not valid and cannot be added to", pod.ID())
+ } else if !ctr.valid {
+ return errors.Wrapf(ErrCtrRemoved, "container %s is not valid and cannot be added to the pod", ctr.ID())
+ }
+
+ if ctr.config.Pod != pod.ID() {
+ return errors.Wrapf(ErrInvalidArg, "container %s is not in pod %s", ctr.ID(), pod.ID())
+ }
+
+ // Add container to pod
+ if err := pod.addContainer(ctr); err != nil {
+ return err
+ }
+
+ // Add container to state
+ _, ok := s.containers[ctr.ID()]
+ if ok {
+ return errors.Wrapf(ErrCtrExists, "container with ID %s already exists in state", ctr.ID())
+ }
+
+ if err := s.ctrNameIndex.Reserve(ctr.Name(), ctr.ID()); err != nil {
+ return errors.Wrapf(err, "error registering container name %s", ctr.Name())
+ }
+
+ if err := s.ctrIDIndex.Add(ctr.ID()); err != nil {
+ s.ctrNameIndex.Release(ctr.Name())
+ return errors.Wrapf(err, "error registering container ID %s", ctr.ID())
+ }
+
+ s.containers[ctr.ID()] = ctr
+
+ return nil
+}
+
+// RemoveContainerFromPod removes the given container from the given pod
+// The container is also removed from the state
+func (s *InMemoryState) RemoveContainerFromPod(pod *Pod, ctr *Container) error {
+ if !pod.valid {
+ return errors.Wrapf(ErrPodRemoved, "pod %s is not valid and containers cannot be removed", pod.ID())
+ } else if !ctr.valid {
+ return errors.Wrapf(ErrCtrRemoved, "container %s is not valid and cannot be removed from the pod", ctr.ID())
+ }
+
+ // Is the container in the pod?
+ exists, err := pod.HasContainer(ctr.ID())
+ if err != nil {
+ return errors.Wrapf(err, "error checking for container %s in pod %s", ctr.ID(), pod.ID())
+ } else if !exists {
+ return errors.Wrapf(ErrNoSuchCtr, "no container %s in pod %s", ctr.ID(), pod.ID())
+ }
+
+ // Remove container from pod
+ if err := pod.removeContainer(ctr); err != nil {
+ return err
+ }
+
+ // Remove container from state
+ if _, ok := s.containers[ctr.ID()]; !ok {
+ return errors.Wrapf(ErrNoSuchCtr, "no container exists in state with ID %s", ctr.ID())
+ }
+
+ if err := s.ctrIDIndex.Delete(ctr.ID()); err != nil {
+ return errors.Wrapf(err, "error removing container ID from index")
+ }
+ delete(s.containers, ctr.ID())
+ s.ctrNameIndex.Release(ctr.Name())
+
+ return nil
+}
+
// AllPods retrieves all pods currently in the state
func (s *InMemoryState) AllPods() ([]*Pod, error) {
pods := make([]*Pod, 0, len(s.pods))