From 6b7b4b03a888b2d87dd58d1e8048d2a5c7e6a36b Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Sun, 28 Jan 2018 05:31:01 -0500 Subject: Add pod removal code Signed-off-by: Matthew Heon Closes: #268 Approved by: rhatdan --- libpod/in_memory_state.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'libpod/in_memory_state.go') diff --git a/libpod/in_memory_state.go b/libpod/in_memory_state.go index 2f0d2ec70..00e8ad4c3 100644 --- a/libpod/in_memory_state.go +++ b/libpod/in_memory_state.go @@ -423,6 +423,52 @@ func (s *InMemoryState) RemovePod(pod *Pod) error { return nil } +// RemovePodContainers removes all containers from a pod +// This is used to simultaneously remove a number of containers with +// many interdependencies +// Will only remove containers if no dependencies outside of the pod are present +func (s *InMemoryState) RemovePodContainers(pod *Pod) error { + if !pod.valid { + return errors.Wrapf(ErrPodRemoved, "pod %s is not valid", pod.ID()) + } + + // Get pod containers + podCtrs, ok := s.podContainers[pod.ID()] + if !ok { + return errors.Wrapf(ErrNoSuchPod, "no pod exists in state with ID %s", pod.ID()) + } + + // Go through container dependencies. Check to see if any are outside the pod. + for ctr := range podCtrs { + ctrDeps, ok := s.ctrDepends[ctr] + if !ok { + return errors.Wrapf(ErrInternal, "cannot find dependencies for container %s", ctr) + } + + for _, dep := range ctrDeps { + _, ok := podCtrs[dep] + if !ok { + return errors.Wrapf(ErrCtrExists, "container %s has dependency %s outside of pod %s", ctr, dep, pod.ID()) + } + } + } + + // All dependencies are OK to remove + // Remove all containers + s.podContainers[pod.ID()] = make(map[string]*Container) + for _, ctr := range podCtrs { + if err := s.ctrIDIndex.Delete(ctr.ID()); err != nil { + return errors.Wrapf(err, "error removing container ID from index") + } + s.ctrNameIndex.Release(ctr.Name()) + + delete(s.containers, ctr.ID()) + delete(s.ctrDepends, ctr.ID()) + } + + 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 { -- cgit v1.2.3-54-g00ecf