Skip to content

Commit 70da5c7

Browse files
committed
Sandbox: Cleanup shim on Start failure
Made a change a bit ago to cleanup the shim on CreateSandbox failures and noted that we should probably do the same on Start as well as nothing gets cleaned up otherwise, and nothing states that a sandbox server/shim should exit itself if Create/Start fail. Ideally this could be hooked up to some subsystem in containerd that'd do it for us, but for now to allow developing prototyping sandbox shims this makes things much friendlier. Signed-off-by: Danny Canter <[email protected]>
1 parent 1fbd703 commit 70da5c7

1 file changed

Lines changed: 21 additions & 16 deletions

File tree

plugins/sandbox/controller.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,25 @@ type controllerLocal struct {
8383

8484
var _ sandbox.Controller = (*controllerLocal)(nil)
8585

86+
func (c *controllerLocal) cleanupShim(ctx context.Context, sandboxID string, svc runtimeAPI.TTRPCSandboxService) {
87+
// Let the shim exit, then we can clean up the bundle after.
88+
if _, sErr := svc.ShutdownSandbox(ctx, &runtimeAPI.ShutdownSandboxRequest{
89+
SandboxID: sandboxID,
90+
}); sErr != nil {
91+
log.G(ctx).WithError(sErr).WithField("sandboxID", sandboxID).
92+
Error("failed to shutdown sandbox")
93+
}
94+
95+
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
96+
defer cancel()
97+
98+
dErr := c.shims.Delete(ctx, sandboxID)
99+
if dErr != nil {
100+
log.G(ctx).WithError(dErr).WithField("sandboxID", sandboxID).
101+
Error("failed to delete shim")
102+
}
103+
}
104+
86105
func (c *controllerLocal) Create(ctx context.Context, sandboxID string, opts ...sandbox.CreateOpt) error {
87106
var coptions sandbox.CreateOptions
88107
for _, opt := range opts {
@@ -128,22 +147,7 @@ func (c *controllerLocal) Create(ctx context.Context, sandboxID string, opts ...
128147
Options: options,
129148
NetnsPath: coptions.NetNSPath,
130149
}); err != nil {
131-
// Let the shim exit, then we can clean up the bundle after.
132-
if _, sErr := svc.ShutdownSandbox(ctx, &runtimeAPI.ShutdownSandboxRequest{
133-
SandboxID: sandboxID,
134-
}); sErr != nil {
135-
log.G(ctx).WithError(sErr).WithField("sandboxID", sandboxID).
136-
Error("failed to shutdown sandbox after failed create")
137-
}
138-
139-
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
140-
defer cancel()
141-
142-
dErr := c.shims.Delete(ctx, sandboxID)
143-
if dErr != nil {
144-
log.G(ctx).WithError(dErr).WithField("sandboxID", sandboxID).
145-
Error("failed to delete shim after failed sandbox create")
146-
}
150+
c.cleanupShim(ctx, sandboxID, svc)
147151
return fmt.Errorf("failed to create sandbox %s: %w", sandboxID, errdefs.FromGRPC(err))
148152
}
149153

@@ -163,6 +167,7 @@ func (c *controllerLocal) Start(ctx context.Context, sandboxID string) (sandbox.
163167

164168
resp, err := svc.StartSandbox(ctx, &runtimeAPI.StartSandboxRequest{SandboxID: sandboxID})
165169
if err != nil {
170+
c.cleanupShim(ctx, sandboxID, svc)
166171
return sandbox.ControllerInstance{}, fmt.Errorf("failed to start sandbox %s: %w", sandboxID, errdefs.FromGRPC(err))
167172
}
168173

0 commit comments

Comments
 (0)