2016-11-22 22:23:01 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2017-06-01 09:20:22 +00:00
|
|
|
"github.com/containers/storage"
|
2017-07-19 19:03:22 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/libkpod/sandbox"
|
2016-11-22 22:23:01 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
2017-06-01 10:07:03 +00:00
|
|
|
pkgstorage "github.com/kubernetes-incubator/cri-o/pkg/storage"
|
2016-11-22 22:23:01 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RemovePodSandbox deletes the sandbox. If there are any running containers in the
|
|
|
|
// sandbox, they should be force deleted.
|
|
|
|
func (s *Server) RemovePodSandbox(ctx context.Context, req *pb.RemovePodSandboxRequest) (*pb.RemovePodSandboxResponse, error) {
|
|
|
|
logrus.Debugf("RemovePodSandboxRequest %+v", req)
|
2017-02-03 14:41:28 +00:00
|
|
|
sb, err := s.getPodSandboxFromRequest(req.PodSandboxId)
|
2016-11-22 22:23:01 +00:00
|
|
|
if err != nil {
|
2017-07-19 19:03:22 +00:00
|
|
|
if err == sandbox.ErrIDEmpty {
|
2016-12-07 16:38:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-22 14:38:10 +00:00
|
|
|
// If the sandbox isn't found we just return an empty response to adhere
|
|
|
|
// the the CRI interface which expects to not error out in not found
|
|
|
|
// cases.
|
|
|
|
|
2016-12-07 16:38:36 +00:00
|
|
|
resp := &pb.RemovePodSandboxResponse{}
|
2017-02-03 14:41:28 +00:00
|
|
|
logrus.Warnf("could not get sandbox %s, it's probably been removed already: %v", req.PodSandboxId, err)
|
2016-12-07 16:38:36 +00:00
|
|
|
return resp, nil
|
2016-11-22 22:23:01 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 19:03:22 +00:00
|
|
|
podInfraContainer := sb.InfraContainer()
|
|
|
|
containers := sb.Containers().List()
|
2016-11-22 22:23:01 +00:00
|
|
|
containers = append(containers, podInfraContainer)
|
|
|
|
|
|
|
|
// Delete all the containers in the sandbox
|
|
|
|
for _, c := range containers {
|
2017-07-17 12:25:32 +00:00
|
|
|
if err := s.Runtime().UpdateStatus(c); err != nil {
|
2016-11-22 22:23:01 +00:00
|
|
|
return nil, fmt.Errorf("failed to update container state: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-07-17 12:25:32 +00:00
|
|
|
cState := s.Runtime().ContainerStatus(c)
|
2016-11-22 22:23:01 +00:00
|
|
|
if cState.Status == oci.ContainerStateCreated || cState.Status == oci.ContainerStateRunning {
|
2017-07-17 12:25:32 +00:00
|
|
|
if err := s.Runtime().StopContainer(c, -1); err != nil {
|
2017-03-14 18:19:18 +00:00
|
|
|
// Assume container is already stopped
|
|
|
|
logrus.Warnf("failed to stop container %s: %v", c.Name(), err)
|
2016-11-22 22:23:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 12:25:32 +00:00
|
|
|
if err := s.Runtime().DeleteContainer(c); err != nil {
|
2017-07-19 19:03:22 +00:00
|
|
|
return nil, fmt.Errorf("failed to delete container %s in pod sandbox %s: %v", c.Name(), sb.ID(), err)
|
2016-11-22 22:23:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 10:55:49 +00:00
|
|
|
if c.ID() == podInfraContainer.ID() {
|
2016-11-22 22:23:01 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-06-01 09:20:22 +00:00
|
|
|
if err := s.storageRuntimeServer.StopContainer(c.ID()); err != nil && err != storage.ErrContainerUnknown {
|
|
|
|
// assume container already umounted
|
2017-07-19 19:03:22 +00:00
|
|
|
logrus.Warnf("failed to stop container %s in pod sandbox %s: %v", c.Name(), sb.ID(), err)
|
2016-10-18 14:48:33 +00:00
|
|
|
}
|
2017-06-01 09:20:22 +00:00
|
|
|
if err := s.storageRuntimeServer.DeleteContainer(c.ID()); err != nil && err != storage.ErrContainerUnknown {
|
2017-07-19 19:03:22 +00:00
|
|
|
return nil, fmt.Errorf("failed to delete container %s in pod sandbox %s: %v", c.Name(), sb.ID(), err)
|
2016-11-22 22:23:01 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 17:10:16 +00:00
|
|
|
s.ReleaseContainerName(c.Name())
|
2016-11-22 22:23:01 +00:00
|
|
|
s.removeContainer(c)
|
2017-07-17 12:25:32 +00:00
|
|
|
if err := s.CtrIDIndex().Delete(c.ID()); err != nil {
|
2017-07-19 19:03:22 +00:00
|
|
|
return nil, fmt.Errorf("failed to delete container %s in pod sandbox %s from index: %v", c.Name(), sb.ID(), err)
|
2016-12-14 21:54:17 +00:00
|
|
|
}
|
2016-11-22 22:23:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 18:19:19 +00:00
|
|
|
s.removeContainer(podInfraContainer)
|
|
|
|
|
2016-11-22 22:23:01 +00:00
|
|
|
// Remove the files related to the sandbox
|
2017-07-19 19:03:22 +00:00
|
|
|
if err := s.storageRuntimeServer.StopContainer(sb.ID()); err != nil && err != storage.ErrContainerUnknown {
|
|
|
|
logrus.Warnf("failed to stop sandbox container in pod sandbox %s: %v", sb.ID(), err)
|
2016-10-18 14:48:33 +00:00
|
|
|
}
|
2017-07-19 19:03:22 +00:00
|
|
|
if err := s.storageRuntimeServer.RemovePodSandbox(sb.ID()); err != nil && err != pkgstorage.ErrInvalidSandboxID {
|
|
|
|
return nil, fmt.Errorf("failed to remove pod sandbox %s: %v", sb.ID(), err)
|
2016-11-22 22:23:01 +00:00
|
|
|
}
|
2016-10-18 14:48:33 +00:00
|
|
|
|
2017-07-20 17:10:16 +00:00
|
|
|
s.ReleaseContainerName(podInfraContainer.Name())
|
2017-07-17 12:25:32 +00:00
|
|
|
if err := s.CtrIDIndex().Delete(podInfraContainer.ID()); err != nil {
|
2017-07-19 19:03:22 +00:00
|
|
|
return nil, fmt.Errorf("failed to delete infra container %s in pod sandbox %s from index: %v", podInfraContainer.ID(), sb.ID(), err)
|
2016-12-14 21:54:17 +00:00
|
|
|
}
|
2016-11-22 22:23:01 +00:00
|
|
|
|
2017-07-19 19:03:22 +00:00
|
|
|
s.releasePodName(sb.Name())
|
|
|
|
s.removeSandbox(sb.ID())
|
|
|
|
if err := s.podIDIndex.Delete(sb.ID()); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to delete pod sandbox %s from index: %v", sb.ID(), err)
|
2016-12-14 21:54:17 +00:00
|
|
|
}
|
2016-11-22 22:23:01 +00:00
|
|
|
|
|
|
|
resp := &pb.RemovePodSandboxResponse{}
|
|
|
|
logrus.Debugf("RemovePodSandboxResponse %+v", resp)
|
|
|
|
return resp, nil
|
|
|
|
}
|