2016-11-22 22:23:01 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
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"
|
2017-08-02 15:17:45 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-08-05 11:40:46 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-11-22 22:23:01 +00:00
|
|
|
"golang.org/x/net/context"
|
2017-08-04 11:13:19 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
2016-11-22 22:23:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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-08-17 18:28:18 +00:00
|
|
|
if !sb.Stopped() {
|
|
|
|
cState := s.Runtime().ContainerStatus(c)
|
|
|
|
if cState.Status == oci.ContainerStateCreated || cState.Status == oci.ContainerStateRunning {
|
|
|
|
if err := s.Runtime().StopContainer(c, -1); err != nil {
|
|
|
|
// 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-07-31 18:38:45 +00:00
|
|
|
if err := s.StorageRuntimeServer().StopContainer(c.ID()); err != nil && err != storage.ErrContainerUnknown {
|
2017-06-01 09:20:22 +00:00
|
|
|
// 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-07-31 18:38:45 +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-08-02 15:17:45 +00:00
|
|
|
if err := s.StorageRuntimeServer().StopContainer(sb.ID()); err != nil && errors.Cause(err) != storage.ErrContainerUnknown {
|
2017-07-19 19:03:22 +00:00
|
|
|
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-31 18:38:45 +00:00
|
|
|
if err := s.StorageRuntimeServer().RemovePodSandbox(sb.ID()); err != nil && err != pkgstorage.ErrInvalidSandboxID {
|
2017-07-19 19:03:22 +00:00
|
|
|
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-25 15:36:33 +00:00
|
|
|
s.ReleasePodName(sb.Name())
|
2017-07-19 19:03:22 +00:00
|
|
|
s.removeSandbox(sb.ID())
|
2017-07-25 15:36:33 +00:00
|
|
|
if err := s.PodIDIndex().Delete(sb.ID()); err != nil {
|
2017-07-19 19:03:22 +00:00
|
|
|
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
|
|
|
|
}
|