CRI: implement Controller.Delete for SandboxAPI
Signed-off-by: WangLei <wllenyj@linux.alibaba.com>
This commit is contained in:
		| @@ -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 { | ||||
|   | ||||
| @@ -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") | ||||
|   | ||||
| @@ -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") | ||||
|   | ||||
							
								
								
									
										60
									
								
								pkg/cri/sbserver/podsandbox/sandbox_delete.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								pkg/cri/sbserver/podsandbox/sandbox_delete.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||
| } | ||||
| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 wanglei01
					wanglei01