From a59ecc50e3300e5fc21de5f8f2674c20b640dde4 Mon Sep 17 00:00:00 2001 From: wanglei01 Date: Fri, 30 Sep 2022 16:20:05 +0800 Subject: [PATCH] CRI: implement Controller.Delete for SandboxAPI Signed-off-by: WangLei --- pkg/cri/sbserver/helpers.go | 12 ---- pkg/cri/sbserver/helpers_linux.go | 12 ++++ pkg/cri/sbserver/podsandbox/controller.go | 5 -- pkg/cri/sbserver/podsandbox/sandbox_delete.go | 60 +++++++++++++++++++ pkg/cri/sbserver/sandbox_remove.go | 21 +------ 5 files changed, 74 insertions(+), 36 deletions(-) create mode 100644 pkg/cri/sbserver/podsandbox/sandbox_delete.go diff --git a/pkg/cri/sbserver/helpers.go b/pkg/cri/sbserver/helpers.go index 2eecfc0e3..d46d201e0 100644 --- a/pkg/cri/sbserver/helpers.go +++ b/pkg/cri/sbserver/helpers.go @@ -121,18 +121,6 @@ func makeContainerName(c *runtime.ContainerMetadata, s *runtime.PodSandboxMetada }, nameDelimiter) } -// getSandboxRootDir returns the root directory for managing sandbox files, -// e.g. hosts files. -func (c *criService) getSandboxRootDir(id string) string { - return filepath.Join(c.config.RootDir, sandboxesDir, id) -} - -// getVolatileSandboxRootDir returns the root directory for managing volatile sandbox files, -// e.g. named pipes. -func (c *criService) getVolatileSandboxRootDir(id string) string { - return filepath.Join(c.config.StateDir, sandboxesDir, id) -} - // getContainerRootDir returns the root directory for managing container files, // e.g. state checkpoint. func (c *criService) getContainerRootDir(id string) string { diff --git a/pkg/cri/sbserver/helpers_linux.go b/pkg/cri/sbserver/helpers_linux.go index 655b96f0e..d3abc3fb6 100644 --- a/pkg/cri/sbserver/helpers_linux.go +++ b/pkg/cri/sbserver/helpers_linux.go @@ -65,6 +65,18 @@ func getCgroupsPath(cgroupsParent, id string) string { return filepath.Join(cgroupsParent, id) } +// getSandboxRootDir returns the root directory for managing sandbox files, +// e.g. hosts files. +func (c *criService) getSandboxRootDir(id string) string { + return filepath.Join(c.config.RootDir, sandboxesDir, id) +} + +// getVolatileSandboxRootDir returns the root directory for managing volatile sandbox files, +// e.g. named pipes. +func (c *criService) getVolatileSandboxRootDir(id string) string { + return filepath.Join(c.config.StateDir, sandboxesDir, id) +} + // getSandboxHostname returns the hostname file path inside the sandbox root directory. func (c *criService) getSandboxHostname(id string) string { return filepath.Join(c.getSandboxRootDir(id), "hostname") diff --git a/pkg/cri/sbserver/podsandbox/controller.go b/pkg/cri/sbserver/podsandbox/controller.go index 6abd73b69..decc59f7f 100644 --- a/pkg/cri/sbserver/podsandbox/controller.go +++ b/pkg/cri/sbserver/podsandbox/controller.go @@ -83,11 +83,6 @@ func New( var _ sandbox.Controller = (*Controller)(nil) -func (c *Controller) Delete(ctx context.Context, sandboxID string) (*api.ControllerDeleteResponse, error) { - //TODO implement me - panic("implement me") -} - func (c *Controller) Status(ctx context.Context, sandboxID string) (*api.ControllerStatusResponse, error) { //TODO implement me panic("implement me") diff --git a/pkg/cri/sbserver/podsandbox/sandbox_delete.go b/pkg/cri/sbserver/podsandbox/sandbox_delete.go new file mode 100644 index 000000000..b40c768ca --- /dev/null +++ b/pkg/cri/sbserver/podsandbox/sandbox_delete.go @@ -0,0 +1,60 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package podsandbox + +import ( + "context" + "fmt" + + "github.com/containerd/containerd" + api "github.com/containerd/containerd/api/services/sandbox/v1" + "github.com/containerd/containerd/errdefs" + "github.com/containerd/containerd/log" +) + +func (c *Controller) Delete(ctx context.Context, sandboxID string) (*api.ControllerDeleteResponse, error) { + sandbox, err := c.sandboxStore.Get(sandboxID) + if err != nil { + if !errdefs.IsNotFound(err) { + return nil, fmt.Errorf("an error occurred when try to find sandbox %q: %w", sandboxID, err) + } + // Do not return error if the id doesn't exist. + log.G(ctx).Tracef("Sandbox controller Delete called for sandbox %q that does not exist", sandboxID) + return &api.ControllerDeleteResponse{}, nil + } + + // Cleanup the sandbox root directories. + sandboxRootDir := c.getSandboxRootDir(sandboxID) + if err := ensureRemoveAll(ctx, sandboxRootDir); err != nil { + return nil, fmt.Errorf("failed to remove sandbox root directory %q: %w", sandboxRootDir, err) + } + volatileSandboxRootDir := c.getVolatileSandboxRootDir(sandboxID) + if err := ensureRemoveAll(ctx, volatileSandboxRootDir); err != nil { + return nil, fmt.Errorf("failed to remove volatile sandbox root directory %q: %w", + volatileSandboxRootDir, err) + } + + // Delete sandbox container. + if err := sandbox.Container.Delete(ctx, containerd.WithSnapshotCleanup); err != nil { + if !errdefs.IsNotFound(err) { + return nil, fmt.Errorf("failed to delete sandbox container %q: %w", sandboxID, err) + } + log.G(ctx).Tracef("Sandbox controller Delete called for sandbox container %q that does not exist", sandboxID) + } + + return &api.ControllerDeleteResponse{}, nil +} diff --git a/pkg/cri/sbserver/sandbox_remove.go b/pkg/cri/sbserver/sandbox_remove.go index 74cdb00e5..f49fdbea0 100644 --- a/pkg/cri/sbserver/sandbox_remove.go +++ b/pkg/cri/sbserver/sandbox_remove.go @@ -21,7 +21,6 @@ import ( "fmt" "time" - "github.com/containerd/containerd" "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/log" @@ -81,24 +80,8 @@ func (c *criService) RemovePodSandbox(ctx context.Context, r *runtime.RemovePodS } } - // Cleanup the sandbox root directories. - sandboxRootDir := c.getSandboxRootDir(id) - if err := ensureRemoveAll(ctx, sandboxRootDir); err != nil { - return nil, fmt.Errorf("failed to remove sandbox root directory %q: %w", - sandboxRootDir, err) - } - volatileSandboxRootDir := c.getVolatileSandboxRootDir(id) - if err := ensureRemoveAll(ctx, volatileSandboxRootDir); err != nil { - return nil, fmt.Errorf("failed to remove volatile sandbox root directory %q: %w", - volatileSandboxRootDir, err) - } - - // Delete sandbox container. - if err := sandbox.Container.Delete(ctx, containerd.WithSnapshotCleanup); err != nil { - if !errdefs.IsNotFound(err) { - return nil, fmt.Errorf("failed to delete sandbox container %q: %w", id, err) - } - log.G(ctx).Tracef("Remove called for sandbox container %q that does not exist", id) + if _, err := c.sandboxController.Delete(ctx, id); err != nil { + return nil, fmt.Errorf("failed to delete sandbox %q: %w", id, err) } // Remove sandbox from sandbox store. Note that once the sandbox is successfully