-
Notifications
You must be signed in to change notification settings - Fork 423
APIServer: Make container deletes a little more robust #567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dcantah
wants to merge
1
commit into
apple:main
Choose a base branch
from
dcantah:get-delete-in-tip-top-shape
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,7 +122,11 @@ actor ContainersService { | |
throw ContainerizationError(.exists, message: "hostname(s) already exist: \(conflictingHostnames)") | ||
} | ||
|
||
self.containers[configuration.id] = ContainerSnapshot(configuration: configuration, status: .stopped, networks: []) | ||
self.containers[configuration.id] = ContainerSnapshot( | ||
configuration: configuration, | ||
status: .stopped, | ||
networks: [] | ||
) | ||
|
||
let runtimePlugin = self.runtimePlugins.filter { | ||
$0.name == configuration.runtimeHandler | ||
|
@@ -207,36 +211,63 @@ actor ContainersService { | |
/// Delete a container and its resources. | ||
public func delete(id: String, force: Bool) async throws { | ||
self.log.debug("\(#function)") | ||
let item = try self._get(id: id) | ||
switch item.status { | ||
case .running: | ||
if !force { | ||
|
||
try await lock.withLock { context in | ||
let item = try await self.get(id: id, context: context) | ||
switch item.status { | ||
case .running: | ||
let autoRemove = try await self.getContainerCreationOptions(id: id).autoRemove | ||
// Check the state from the sb services point of view. | ||
let client = SandboxClient( | ||
id: item.configuration.id, | ||
runtime: item.configuration.runtimeHandler | ||
) | ||
let ctrState = try await client.state() | ||
// Someone asked to delete a container before the process exited XPC event | ||
// came through. The sandbox process says the container is dead, but our | ||
// state here isn't updated yet. We are perfectly fine to delete the container, | ||
// so let's update our state and do just that. | ||
if ctrState.status == .stopped { | ||
let snapshot = ContainerSnapshot( | ||
configuration: item.configuration, | ||
status: .stopped, | ||
networks: [] | ||
) | ||
await self.setContainer(id, snapshot, context: context) | ||
if !autoRemove { | ||
try await self.cleanup(id: id, item: item, context: context) | ||
} | ||
return | ||
} | ||
|
||
if !force { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: perhaps L243-L258 in an else, and then 237-240 can go away in favor of 259-262? |
||
throw ContainerizationError( | ||
.invalidState, | ||
message: "container \(id) is running and can not be deleted" | ||
) | ||
} | ||
|
||
let opts = ContainerStopOptions( | ||
timeoutInSeconds: 5, | ||
signal: SIGKILL | ||
) | ||
try await self._stop( | ||
id: id, | ||
runtimeHandler: item.configuration.runtimeHandler, | ||
options: opts | ||
) | ||
if autoRemove { | ||
return | ||
} | ||
try await self.cleanup(id: id, item: item, context: context) | ||
case .stopped: | ||
try await self.cleanup(id: id, item: item, context: context) | ||
default: | ||
throw ContainerizationError( | ||
.invalidState, | ||
message: "container \(id) is \(item.status) and can not be deleted" | ||
message: "container \(id) is in state \(item.status) and can not be deleted" | ||
) | ||
} | ||
let autoRemove = try getContainerCreationOptions(id: id).autoRemove | ||
let opts = ContainerStopOptions( | ||
timeoutInSeconds: 5, | ||
signal: SIGKILL | ||
) | ||
try await self._stop( | ||
id: id, | ||
runtimeHandler: item.configuration.runtimeHandler, | ||
options: opts | ||
) | ||
if autoRemove { | ||
return | ||
} | ||
try self._cleanup(id: id, item: item) | ||
case .stopping: | ||
throw ContainerizationError( | ||
.invalidState, | ||
message: "container \(id) is \(item.status) and can not be deleted" | ||
) | ||
default: | ||
try self._cleanup(id: id, item: item) | ||
} | ||
} | ||
|
||
|
@@ -284,6 +315,21 @@ actor ContainersService { | |
let snapshot = ContainerSnapshot(configuration: item.configuration, status: .stopped, networks: []) | ||
await self.setContainer(id, snapshot, context: context) | ||
|
||
do { | ||
let client = SandboxClient( | ||
id: item.configuration.id, | ||
runtime: item.configuration.runtimeHandler | ||
) | ||
try await client.shutdown() | ||
} catch { | ||
self.log.error( | ||
"Failed to shut down SandboxService after process exit", | ||
metadata: [ | ||
"id": .string(id), | ||
"error": .string(String(describing: error)), | ||
]) | ||
} | ||
|
||
let options = try getContainerCreationOptions(id: id) | ||
if options.autoRemove { | ||
try self.cleanup(id: id, item: item, context: context) | ||
|
@@ -303,7 +349,10 @@ actor ContainersService { | |
self.log.info("Handling container \(id) Start.") | ||
do { | ||
let currentSnapshot = try self.get(id: id, context: context) | ||
let client = SandboxClient(id: currentSnapshot.configuration.id, runtime: currentSnapshot.configuration.runtimeHandler) | ||
let client = SandboxClient( | ||
id: currentSnapshot.configuration.id, | ||
runtime: currentSnapshot.configuration.runtimeHandler | ||
) | ||
let sandboxSnapshot = try await client.state() | ||
let snapshot = ContainerSnapshot(configuration: currentSnapshot.configuration, status: .running, networks: sandboxSnapshot.networks) | ||
await self.setContainer(id, snapshot, context: context) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps a one-line explanation of why we don't invoke this for the auto-remove case? who's responsible for cleanup?