-
Notifications
You must be signed in to change notification settings - Fork 742
Implement container prune #904
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a8e259a
Implement container prune
tico88612 c44507e
Add container prune docs
tico88612 5b9b147
Test: add getContainerId in CLITest.swift
tico88612 d2251e7
Add container prune testcases
tico88612 faac62d
Refactor: client side prune container
tico88612 06ec76c
Update ContainerPrune to AsyncLoggableCommand and change import name
tico88612 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,60 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // 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 ContainerPrune: AsyncLoggableCommand { | ||
| public init() {} | ||
|
|
||
| public static let configuration = CommandConfiguration( | ||
| commandName: "prune", | ||
| abstract: "Remove all stopped containers" | ||
| ) | ||
|
|
||
| @OptionGroup | ||
| public var logOptions: Flags.Logging | ||
|
|
||
| public func run() async throws { | ||
| let containersToPrune = try await ClientContainer.list().filter { $0.status == .stopped } | ||
|
|
||
| var prunedContainerIds = [String]() | ||
| var totalSize: UInt64 = 0 | ||
|
|
||
| for container in containersToPrune { | ||
| do { | ||
| let actualSize = try await ClientContainer.containerDiskUsage(id: container.id) | ||
| totalSize += actualSize | ||
| try await container.delete() | ||
| prunedContainerIds.append(container.id) | ||
| } catch { | ||
| log.error("Failed to prune container \(container.id): \(error)") | ||
| } | ||
| } | ||
|
|
||
| let formatter = ByteCountFormatter() | ||
| let freed = formatter.string(fromByteCount: Int64(totalSize)) | ||
|
|
||
| for name in prunedContainerIds { | ||
| print(name) | ||
| } | ||
| print("Reclaimed \(freed) in disk space") | ||
| } | ||
| } | ||
| } |
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,87 @@ | ||
| //===----------------------------------------------------------------------===// | ||
|
tico88612 marked this conversation as resolved.
|
||
| // 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(.serialized) | ||
| class TestCLIPruneCommand: CLITest { | ||
| private func getTestName() -> String { | ||
| Test.current!.name.trimmingCharacters(in: ["(", ")"]).lowercased() | ||
| } | ||
|
|
||
| @Test func testContainerPruneNoContainers() throws { | ||
| let (_, output, error, status) = try run(arguments: ["prune"]) | ||
| if status != 0 { | ||
| throw CLIError.executionFailed("container prune failed: \(error)") | ||
| } | ||
|
|
||
| #expect(output.contains("Reclaimed Zero KB in disk space"), "should show no containers message") | ||
| } | ||
|
|
||
| @Test func testContainerPruneStoppedContainers() throws { | ||
| let testName = getTestName() | ||
| let npcName = "\(testName)_wont_be_pruned" | ||
| let pc0Name = "\(testName)_pruned_0" | ||
| let pc1Name = "\(testName)_pruned_1" | ||
|
|
||
| try doLongRun(name: npcName, containerArgs: ["sleep", "3600"], autoRemove: true) | ||
| try doLongRun(name: pc0Name, containerArgs: ["sleep", "3600"], autoRemove: false) | ||
| try doLongRun(name: pc1Name, containerArgs: ["sleep", "3600"], autoRemove: false) | ||
| defer { | ||
| try? doStop(name: npcName) | ||
| try? doStop(name: pc0Name) | ||
| try? doStop(name: pc1Name) | ||
| try? doRemove(name: npcName) | ||
| try? doRemove(name: pc0Name) | ||
| try? doRemove(name: pc1Name) | ||
| } | ||
| try waitForContainerRunning(npcName) | ||
| try waitForContainerRunning(pc0Name) | ||
| try waitForContainerRunning(pc1Name) | ||
|
|
||
| try doStop(name: pc0Name) | ||
| try doStop(name: pc1Name) | ||
|
|
||
| let pc0Id = try getContainerId(pc0Name) | ||
| let pc1Id = try getContainerId(pc1Name) | ||
|
|
||
| // Poll status until both containers are stopped, with interval checks and a timeout to avoid infinite loop | ||
| let start = Date() | ||
| let timeout: TimeInterval = 30 // seconds | ||
| while true { | ||
| let s0 = try getContainerStatus(pc0Name) | ||
| let s1 = try getContainerStatus(pc1Name) | ||
| if s0 == "stopped" && s1 == "stopped" { break } | ||
| if Date().timeIntervalSince(start) > timeout { | ||
| throw CLIError.executionFailed("Timeout waiting for containers to stop: pc0=\(s0), pc1=\(s1)") | ||
| } | ||
| Thread.sleep(forTimeInterval: 0.2) | ||
| } | ||
|
|
||
| let (_, output, error, status) = try run(arguments: ["prune"]) | ||
|
|
||
| if status != 0 { | ||
| throw CLIError.executionFailed("container prune failed: \(error)") | ||
| } | ||
|
|
||
| #expect(output.contains(pc0Id) && output.contains(pc1Id), "should show the stopped containers id") | ||
| #expect(!output.contains("Reclaimed Zero KB in disk space"), "reclaimed spaces should not Zero KB") | ||
|
|
||
| let checkStatus = try getContainerStatus(npcName) | ||
| #expect(checkStatus == "running", "not pruned container should still be 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
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
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.