From b494574b6afd9ce1a92332e786700cac5fd11042 Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Tue, 16 Aug 2016 10:27:14 -0700 Subject: [PATCH] Add implementation for removing pod sandbox Signed-off-by: Mrunal Patel --- server/runtime.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/server/runtime.go b/server/runtime.go index 6958f193..8772bb33 100644 --- a/server/runtime.go +++ b/server/runtime.go @@ -161,7 +161,7 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxRequest) (*pb.StopPodSandboxResponse, error) { sbName := req.PodSandboxId if *sbName == "" { - return nil, fmt.Errorf("PodSandboxConfig.Name should not be empty") + return nil, fmt.Errorf("PodSandboxId should not be empty") } sb := s.state.sandboxes[*sbName] if sb == nil { @@ -177,10 +177,32 @@ func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxReque return &pb.StopPodSandboxResponse{}, nil } -// DeletePodSandbox deletes the sandbox. If there are any running containers in the +// RemovePodSandbox deletes the sandbox. If there are any running containers in the // sandbox, they should be force deleted. -func (s *Server) RemovePodSandbox(context.Context, *pb.RemovePodSandboxRequest) (*pb.RemovePodSandboxResponse, error) { - return nil, nil +func (s *Server) RemovePodSandbox(ctx context.Context, req *pb.RemovePodSandboxRequest) (*pb.RemovePodSandboxResponse, error) { + sbName := req.PodSandboxId + if *sbName == "" { + return nil, fmt.Errorf("PodSandboxId should not be empty") + } + sb := s.state.sandboxes[*sbName] + if sb == nil { + return nil, fmt.Errorf("specified sandbox not found: %s", *sbName) + } + + // Delete all the containers in the sandbox + for _, c := range sb.containers { + if err := s.runtime.DeleteContainer(c); err != nil { + return nil, fmt.Errorf("failed to delete container %s in sandbox %s: %v", c.Name(), *sbName, err) + } + } + + // Remove the files related to the sandbox + podSandboxDir := filepath.Join(s.sandboxDir, *sbName) + if err := os.RemoveAll(podSandboxDir); err != nil { + return nil, fmt.Errorf("failed to remove sandbox %s directory: %v", *sbName, err) + } + + return &pb.RemovePodSandboxResponse{}, nil } // PodSandboxStatus returns the Status of the PodSandbox.