-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[container]: add clean command #1949
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
Open
saehejkang
wants to merge
5
commits into
apple:main
Choose a base branch
from
saehejkang:container-clean-command
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.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
083b3ec
container clean command
saehejkang b0560a2
add missing route + update tests
saehejkang 0f08f7d
Merge branch 'main' into container-clean-command
saehejkang a0bdfff
Merge remote-tracking branch 'upstream/main' into container-clean-com…
saehejkang 54a0843
fix how volumes are trimmed
saehejkang 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // Copyright © 2026 Apple Inc. and the container project 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 | ||
| // | ||
| // https://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. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import ArgumentParser | ||
| import ContainerAPIClient | ||
| import ContainerizationError | ||
| import Foundation | ||
|
|
||
| extension Application { | ||
| public struct ContainerClean: AsyncLoggableCommand { | ||
| public init() {} | ||
| public static let configuration = CommandConfiguration( | ||
| commandName: "clean", | ||
| abstract: "Clean one or more running containers" | ||
| ) | ||
|
|
||
| @OptionGroup | ||
| public var logOptions: Flags.Logging | ||
|
|
||
| @Argument(help: "Container IDs") | ||
| var containerIds: [String] = [] | ||
|
|
||
| public func validate() throws { | ||
| if containerIds.count == 0 { | ||
| throw ContainerizationError(.invalidArgument, message: "no containers specified") | ||
| } | ||
| } | ||
|
|
||
| public mutating func run() async throws { | ||
| let client = ContainerClient() | ||
| let containers = Array(Set(containerIds)) | ||
|
|
||
| var errors: [any Error] = [] | ||
| try await withThrowingTaskGroup(of: (any Error)?.self) { group in | ||
| for container in containers { | ||
| group.addTask { | ||
| do { | ||
| try await client.clean(id: container) | ||
| print(container) | ||
| return nil | ||
| } catch { | ||
| return error | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for try await error in group { | ||
| if let error { | ||
| errors.append(error) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if !errors.isEmpty { | ||
| throw AggregateError(errors) | ||
| } | ||
| } | ||
| } | ||
| } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // Copyright © 2026 Apple Inc. and the container project 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 | ||
| // | ||
| // https://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. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import Foundation | ||
| import Testing | ||
|
|
||
| @Suite(.serialSuites) | ||
| class TestCLIClean: CLITest { | ||
| private func getTestName() -> String { | ||
| Test.current!.name.trimmingCharacters(in: ["(", ")"]).lowercased() | ||
| } | ||
|
|
||
| @Test func testCleanRunningContainer() throws { | ||
|
saehejkang marked this conversation as resolved.
Outdated
|
||
| let name = getTestName() | ||
| try doLongRun(name: name, autoRemove: false) | ||
| defer { | ||
| try? doStop(name: name) | ||
| try? doRemove(name: name) | ||
| } | ||
|
|
||
| try waitForContainerRunning(name) | ||
|
|
||
| // Clean should succeed on running container | ||
| try doClean(name: name) | ||
|
|
||
| // Container should still be running after clean | ||
| let status = try getContainerStatus(name) | ||
| #expect(status == "running") | ||
| } | ||
|
|
||
| @Test func testCleanStoppedContainerFails() throws { | ||
| let name = getTestName() | ||
| try doLongRun(name: name, autoRemove: false) | ||
| defer { try? doRemove(name: name) } | ||
|
|
||
| try waitForContainerRunning(name) | ||
| try doStop(name: name) | ||
|
|
||
| let status = try getContainerStatus(name) | ||
| #expect(status == "stopped") | ||
|
|
||
| // Clean should fail on stopped container | ||
| let (_, _, _, exitStatus) = try run(arguments: ["clean", name]) | ||
| #expect(exitStatus != 0) | ||
| } | ||
|
|
||
| @Test func testCleanMultipleContainers() throws { | ||
| let name1 = getTestName() + "1" | ||
| let name2 = getTestName() + "2" | ||
|
|
||
| try doLongRun(name: name1, autoRemove: false) | ||
| try doLongRun(name: name2, autoRemove: false) | ||
|
|
||
| defer { | ||
| try? doStop(name: name1) | ||
| try? doStop(name: name2) | ||
| try? doRemove(name: name1) | ||
| try? doRemove(name: name2) | ||
| } | ||
|
|
||
| try waitForContainerRunning(name1) | ||
| try waitForContainerRunning(name2) | ||
|
|
||
| // Clean both containers | ||
| let (_, _, _, exitStatus1) = try run(arguments: ["clean", name1, name2]) | ||
| #expect(exitStatus1 == 0) | ||
|
|
||
| // Both containers should still be running | ||
| let status1 = try getContainerStatus(name1) | ||
| let status2 = try getContainerStatus(name2) | ||
| #expect(status1 == "running") | ||
| #expect(status2 == "running") | ||
| } | ||
|
|
||
| @Test func testCleanAfterFileCreation() throws { | ||
| let name = getTestName() | ||
| try doLongRun(name: name, autoRemove: false) | ||
| defer { | ||
| try? doStop(name: name) | ||
| try? doRemove(name: name) | ||
| } | ||
|
|
||
| try waitForContainerRunning(name) | ||
|
|
||
| // Create some files to exercise the filesystem trim path | ||
| _ = try doExec(name: name, cmd: ["sh", "-c", "dd if=/dev/zero of=/test-file bs=1M count=10"]) | ||
|
saehejkang marked this conversation as resolved.
Outdated
|
||
| _ = try doExec(name: name, cmd: ["rm", "/test-file"]) | ||
|
|
||
| // Clean should succeed | ||
| try doClean(name: name) | ||
|
|
||
| // Container should still be running | ||
| let status = try getContainerStatus(name) | ||
| #expect(status == "running") | ||
| } | ||
|
|
||
| @Test func testCleanWithVolume() throws { | ||
| let name = getTestName() | ||
| let volumeName = name + "-vol" | ||
|
|
||
| // Create a volume | ||
| let (_, _, volError, volStatus) = try run(arguments: ["volume", "create", volumeName]) | ||
| guard volStatus == 0 else { | ||
| throw CLIError.executionFailed("volume create failed: \(volError)") | ||
| } | ||
|
|
||
| defer { | ||
| try? run(arguments: ["volume", "rm", volumeName]) | ||
| } | ||
|
|
||
| // Create container with volume mount | ||
| try doCreate( | ||
| name: name, | ||
| image: nil, | ||
| args: nil, | ||
| volumes: ["\(volumeName):/mnt/vol"], | ||
| networks: [], | ||
| ports: [] | ||
| ) | ||
|
|
||
| try doStart(name: name) | ||
|
|
||
| defer { | ||
| try? doStop(name: name) | ||
| try? doRemove(name: name) | ||
| } | ||
|
|
||
| try waitForContainerRunning(name) | ||
|
|
||
| // Write to volume | ||
| _ = try doExec(name: name, cmd: ["sh", "-c", "dd if=/dev/zero of=/mnt/vol/test bs=1M count=5"]) | ||
| _ = try doExec(name: name, cmd: ["rm", "/mnt/vol/test"]) | ||
|
|
||
| // Clean should succeed and also trim the volume | ||
| try doClean(name: name) | ||
|
|
||
| // Container should still be running | ||
| let status = try getContainerStatus(name) | ||
| #expect(status == "running") | ||
| } | ||
| } | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.