chore: import upstream snapshot with attribution
container project - merge build / Invoke build (push) Successful in 1s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:06:18 +08:00
commit e84fb4a79e
474 changed files with 68321 additions and 0 deletions
@@ -0,0 +1,40 @@
//===----------------------------------------------------------------------===//
// Copyright © 2025-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
extension Application {
public struct VolumeCommand: AsyncLoggableCommand {
public static let configuration = CommandConfiguration(
commandName: "volume",
abstract: "Manage container volumes",
subcommands: [
VolumeCreate.self,
VolumeDelete.self,
VolumeList.self,
VolumeInspect.self,
VolumePrune.self,
],
aliases: ["v"]
)
public init() {}
@OptionGroup
public var logOptions: Flags.Logging
}
}
@@ -0,0 +1,63 @@
//===----------------------------------------------------------------------===//
// Copyright © 2025-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 Foundation
extension Application.VolumeCommand {
public struct VolumeCreate: AsyncLoggableCommand {
public static let configuration = CommandConfiguration(
commandName: "create",
abstract: "Create a new volume"
)
@Option(name: .customLong("label"), help: "Set metadata for a volume")
var labels: [String] = []
@Option(name: .customLong("opt"), help: "Set driver specific options")
var driverOpts: [String] = []
@Option(name: .short, help: "Size of the volume in bytes, with optional K, M, G, T, or P suffix")
var size: String?
@OptionGroup
public var logOptions: Flags.Logging
@Argument(help: "Volume name")
var name: String
public init() {}
public func run() async throws {
var parsedDriverOpts = Utility.parseKeyValuePairs(driverOpts)
let parsedLabels = Utility.parseKeyValuePairs(labels)
// If --size is specified, add it to driver options
if let size = size {
parsedDriverOpts["size"] = size
}
let volume = try await ClientVolume.create(
name: name,
driver: "local",
driverOpts: parsedDriverOpts,
labels: parsedLabels
)
print(volume.name)
}
}
}
@@ -0,0 +1,116 @@
//===----------------------------------------------------------------------===//
// Copyright © 2025-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 ContainerResource
import ContainerizationError
import Foundation
extension Application.VolumeCommand {
public struct VolumeDelete: AsyncLoggableCommand {
public static let configuration = CommandConfiguration(
commandName: "delete",
abstract: "Delete one or more volumes",
aliases: ["rm"]
)
@Flag(name: .shortAndLong, help: "Delete all volumes")
var all = false
@OptionGroup
public var logOptions: Flags.Logging
@Argument(help: "Volume names")
var names: [String] = []
public init() {}
public func validate() throws {
if names.count == 0 && !all {
throw ContainerizationError(.invalidArgument, message: "no volumes specified and --all not supplied")
}
if names.count > 0 && all {
throw ContainerizationError(
.invalidArgument,
message: "explicitly supplied volume name(s) conflict with the --all flag"
)
}
}
public func run() async throws {
let uniqueVolumeNames = Set<String>(names)
let volumes: [VolumeConfiguration]
if all {
volumes = try await ClientVolume.list()
} else {
volumes = try await ClientVolume.list()
.filter { v in
uniqueVolumeNames.contains(v.id)
}
// If one of the volumes requested isn't present lets throw. We don't need to do
// this for --all as --all should be perfectly usable with no volumes to remove,
// otherwise it'd be quite clunky.
if volumes.count != uniqueVolumeNames.count {
let missing = uniqueVolumeNames.filter { id in
!volumes.contains { v in
v.id == id
}
}
throw ContainerizationError(
.notFound,
message: "failed to delete one or more volumes: \(missing)"
)
}
}
var failed = [String]()
let _log = log
try await withThrowingTaskGroup(of: VolumeConfiguration?.self) { group in
for volume in volumes {
group.addTask {
do {
try await ClientVolume.delete(name: volume.id)
print(volume.id)
return nil
} catch {
_log.error(
"failed to delete volume",
metadata: [
"id": "\(volume.id)",
"error": "\(error)",
])
return volume
}
}
}
for try await volume in group {
guard let volume else {
continue
}
failed.append(volume.id)
}
}
if failed.count > 0 {
throw ContainerizationError(.internalError, message: "delete failed for one or more volumes: \(failed)")
}
}
}
}
@@ -0,0 +1,55 @@
//===----------------------------------------------------------------------===//
// Copyright © 2025-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 ContainerResource
import ContainerizationError
import Foundation
extension Application.VolumeCommand {
public struct VolumeInspect: AsyncLoggableCommand {
public static let configuration = CommandConfiguration(
commandName: "inspect",
abstract: "Display information about one or more volumes"
)
@OptionGroup
public var logOptions: Flags.Logging
@Argument(help: "Volumes to inspect")
var names: [String]
public init() {}
public func run() async throws {
let uniqueNames = Set(names)
let volumes = try await ClientVolume.list().filter { uniqueNames.contains($0.id) }
let volumeResources = volumes.map { VolumeResource(configuration: $0) }
if volumes.count != uniqueNames.count {
let found = Set(volumes.map { $0.id })
let missing = uniqueNames.subtracting(found).sorted()
throw ContainerizationError(
.notFound,
message: "volume not found: \(missing.joined(separator: ", "))"
)
}
try Output.emit(Output.renderJSON(volumeResources, options: .pretty))
}
}
}
@@ -0,0 +1,48 @@
//===----------------------------------------------------------------------===//
// Copyright © 2025-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 ContainerResource
import ContainerizationExtras
import Foundation
extension Application.VolumeCommand {
public struct VolumeList: AsyncLoggableCommand {
public static let configuration = CommandConfiguration(
commandName: "list",
abstract: "List volumes",
aliases: ["ls"]
)
@Option(name: .long, help: "Format of the output")
var format: ListFormat = .table
@Flag(name: .shortAndLong, help: "Only output the volume name")
var quiet: Bool = false
@OptionGroup
public var logOptions: Flags.Logging
public init() {}
public func run() async throws {
let volumes = try await ClientVolume.list()
let volumeResources = volumes.map { VolumeResource(configuration: $0) }
try Output.render(payload: volumeResources, display: volumeResources, format: format, quiet: quiet)
}
}
}
@@ -0,0 +1,78 @@
//===----------------------------------------------------------------------===//
// Copyright © 2025-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 Foundation
extension Application.VolumeCommand {
public struct VolumePrune: AsyncLoggableCommand {
public init() {}
public static let configuration = CommandConfiguration(
commandName: "prune",
abstract: "Remove volumes with no container references")
@OptionGroup
public var logOptions: Flags.Logging
public func run() async throws {
let allVolumes = try await ClientVolume.list()
// Find all volumes not used by any container
let client = ContainerClient()
let containers = try await client.list()
var volumesInUse = Set<String>()
for container in containers {
for mount in container.configuration.mounts {
if mount.isVolume, let volumeName = mount.volumeName {
volumesInUse.insert(volumeName)
}
}
}
let volumesToPrune = allVolumes.filter { volume in
!volumesInUse.contains(volume.name)
}
var prunedVolumes = [String]()
var totalSize: UInt64 = 0
for volume in volumesToPrune {
do {
let actualSize = try await ClientVolume.volumeDiskUsage(name: volume.name)
totalSize += actualSize
try await ClientVolume.delete(name: volume.name)
prunedVolumes.append(volume.name)
} catch {
log.error(
"failed to prune volume",
metadata: [
"id": "\(volume.name)",
"error": "\(error)",
])
}
}
for name in prunedVolumes {
print(name)
}
let formatter = ByteCountFormatter()
let freed = formatter.string(fromByteCount: Int64(totalSize))
log.info("Reclaimed \(freed) in disk space")
}
}
}
@@ -0,0 +1,36 @@
//===----------------------------------------------------------------------===//
// 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 ContainerResource
extension VolumeResource: ListDisplayable {
public static var tableHeader: [String] {
["NAME", "TYPE", "DRIVER", "OPTIONS"]
}
public var tableRow: [String] {
[
name,
isAnonymous ? "anonymous" : "named",
configuration.driver,
configuration.options.isEmpty ? "" : configuration.options.sorted(by: { $0.key < $1.key }).map { "\($0.key)=\($0.value)" }.joined(separator: ","),
]
}
public var quietValue: String {
name
}
}