Add initial sandbox management implementation
Signed-off-by: Random-Liu <lantaol@google.com>
This commit is contained in:
@@ -17,15 +17,75 @@ limitations under the License.
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/containerd/containerd/api/services/execution"
|
||||
|
||||
"k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
||||
)
|
||||
|
||||
// RemovePodSandbox removes the sandbox. If there are running containers in the
|
||||
// sandbox, they should be forcibly removed.
|
||||
func (c *criContainerdService) RemovePodSandbox(ctx context.Context, r *runtime.RemovePodSandboxRequest) (*runtime.RemovePodSandboxResponse, error) {
|
||||
return nil, errors.New("not implemented")
|
||||
func (c *criContainerdService) RemovePodSandbox(ctx context.Context, r *runtime.RemovePodSandboxRequest) (retRes *runtime.RemovePodSandboxResponse, retErr error) {
|
||||
glog.V(2).Infof("RemovePodSandbox for sandbox %q", r.GetPodSandboxId())
|
||||
defer func() {
|
||||
if retErr == nil {
|
||||
glog.V(2).Info("RemovePodSandbox returns successfully")
|
||||
}
|
||||
}()
|
||||
|
||||
sandbox, err := c.getSandbox(r.GetPodSandboxId())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find sandbox %q: %v", r.GetPodSandboxId(), err)
|
||||
}
|
||||
if sandbox == nil {
|
||||
// Do not return error if the id doesn't exist.
|
||||
glog.V(5).Infof("RemovePodSandbox called for sandbox %q that does not exist",
|
||||
r.GetPodSandboxId())
|
||||
return &runtime.RemovePodSandboxResponse{}, nil
|
||||
}
|
||||
// Use the full sandbox id.
|
||||
id := sandbox.ID
|
||||
|
||||
// TODO(random-liu): [P2] Remove all containers in the sandbox.
|
||||
|
||||
// Return error if sandbox container is not fully stopped.
|
||||
_, err = c.containerService.Info(ctx, &execution.InfoRequest{ID: id})
|
||||
if err != nil && !isContainerdContainerNotExistError(err) {
|
||||
return nil, fmt.Errorf("failed to get sandbox container info for %q: %v", id, err)
|
||||
}
|
||||
if err == nil {
|
||||
return nil, fmt.Errorf("sandbox container %q is not fully stopped", id)
|
||||
}
|
||||
|
||||
// TODO(random-liu): [P0] Cleanup shm created in RunPodSandbox.
|
||||
// TODO(random-liu): [P1] Remove permanent namespace once used.
|
||||
|
||||
// Cleanup the sandbox root directory.
|
||||
sandboxRootDir := getSandboxRootDir(c.rootDir, id)
|
||||
if err := c.os.RemoveAll(sandboxRootDir); err != nil {
|
||||
return nil, fmt.Errorf("failed to remove sandbox root directory %q: %v",
|
||||
sandboxRootDir, err)
|
||||
}
|
||||
|
||||
// Remove sandbox metadata from metadata store. Note that once the sandbox
|
||||
// metadata is successfully deleted:
|
||||
// 1) ListPodSandbox will not include this sandbox.
|
||||
// 2) PodSandboxStatus and StopPodSandbox will return error.
|
||||
// 3) On-going operations which have held the metadata reference will not be
|
||||
// affected.
|
||||
if err := c.sandboxStore.Delete(id); err != nil {
|
||||
return nil, fmt.Errorf("failed to delete sandbox metadata for %q: %v", id, err)
|
||||
}
|
||||
|
||||
// Release the sandbox id from id index.
|
||||
c.sandboxIDIndex.Delete(id) // nolint: errcheck
|
||||
|
||||
// Release the sandbox name reserved for the sandbox.
|
||||
c.sandboxNameIndex.ReleaseByKey(id)
|
||||
|
||||
return &runtime.RemovePodSandboxResponse{}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user