mirror of
https://github.com/apple/container.git
synced 2026-09-28 01:46:25 -04:00
Conform containers to ManagedResource (#1656)
- Closes #1651 - Adds ManagedContainer that conforms to Managed Resource
This commit is contained in:
@@ -51,13 +51,13 @@ extension Application {
|
||||
}
|
||||
|
||||
try Output.render(
|
||||
payload: [PrintableContainer(container)],
|
||||
payload: [ManagedContainer(container)],
|
||||
display: [PrintableBuilder(container)],
|
||||
format: format,
|
||||
quiet: quiet
|
||||
)
|
||||
} catch let error as ContainerizationError where error.code == .notFound {
|
||||
try Output.render(payload: [PrintableContainer](), format: format) {
|
||||
try Output.render(payload: [ManagedContainer](), format: format) {
|
||||
quiet ? "" : "builder is not running"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ extension Application {
|
||||
)
|
||||
}
|
||||
|
||||
try Output.emit(Output.renderJSON(containers.map { PrintableContainer($0) }, options: .pretty))
|
||||
try Output.emit(Output.renderJSON(containers.map { ManagedContainer($0) }, options: .pretty))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,46 +46,8 @@ extension Application {
|
||||
let client = ContainerClient()
|
||||
let filters = self.all ? ContainerListFilters.all : ContainerListFilters(status: .running)
|
||||
let containers = try await client.list(filters: filters)
|
||||
let items = containers.map { PrintableContainer($0) }
|
||||
let items = containers.map { ManagedContainer($0) }
|
||||
try Output.render(payload: items, display: items, format: format, quiet: quiet)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension PrintableContainer: ListDisplayable {
|
||||
static var tableHeader: [String] {
|
||||
["ID", "IMAGE", "OS", "ARCH", "STATE", "IP", "CPUS", "MEMORY", "STARTED"]
|
||||
}
|
||||
|
||||
var tableRow: [String] {
|
||||
[
|
||||
self.configuration.id,
|
||||
self.configuration.image.reference,
|
||||
self.configuration.platform.os,
|
||||
self.configuration.platform.architecture,
|
||||
self.status.rawValue,
|
||||
self.networks.map { $0.ipv4Address.description }.joined(separator: ","),
|
||||
"\(self.configuration.resources.cpus)",
|
||||
"\(self.configuration.resources.memoryInBytes / (1024 * 1024)) MB",
|
||||
self.startedDate?.ISO8601Format() ?? "",
|
||||
]
|
||||
}
|
||||
|
||||
var quietValue: String {
|
||||
self.configuration.id
|
||||
}
|
||||
}
|
||||
|
||||
struct PrintableContainer: Codable, Sendable {
|
||||
let status: RuntimeStatus
|
||||
let configuration: ContainerConfiguration
|
||||
let networks: [Attachment]
|
||||
let startedDate: Date?
|
||||
|
||||
init(_ container: ContainerSnapshot) {
|
||||
self.status = container.status
|
||||
self.configuration = container.configuration
|
||||
self.networks = container.networks
|
||||
self.startedDate = container.startedDate
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
// 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
|
||||
import Foundation
|
||||
|
||||
extension ManagedContainer: ListDisplayable {
|
||||
public static var tableHeader: [String] {
|
||||
["ID", "IMAGE", "OS", "ARCH", "STATE", "IP", "CPUS", "MEMORY", "STARTED"]
|
||||
}
|
||||
|
||||
public var tableRow: [String] {
|
||||
[
|
||||
configuration.id,
|
||||
configuration.image.reference,
|
||||
configuration.platform.os,
|
||||
configuration.platform.architecture,
|
||||
status.state.rawValue,
|
||||
status.networks.map { $0.ipv4Address.description }.joined(separator: ","),
|
||||
"\(configuration.resources.cpus)",
|
||||
"\(configuration.resources.memoryInBytes / (1024 * 1024)) MB",
|
||||
status.startedDate?.ISO8601Format() ?? "",
|
||||
]
|
||||
}
|
||||
|
||||
public var quietValue: String { configuration.id }
|
||||
}
|
||||
@@ -107,7 +107,7 @@ public enum Output {
|
||||
/// Renders list output in the requested format.
|
||||
///
|
||||
/// The machine-readable payload and the display model may be the same type
|
||||
/// (e.g., `PrintableContainer`) or different types.
|
||||
/// (e.g., `ManagedContainer`) or different types.
|
||||
public static func render<J: Encodable, D: ListDisplayable>(
|
||||
payload: J, display: [D], format: ListFormat, quiet: Bool, jsonOptions: JSONOptions = .compact
|
||||
) throws {
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import ContainerizationOCI
|
||||
import Foundation
|
||||
|
||||
public struct ContainerConfiguration: Sendable, Codable {
|
||||
/// Identifier for the container.
|
||||
@@ -61,6 +62,8 @@ public struct ContainerConfiguration: Sendable, Codable {
|
||||
public var shmSize: UInt64?
|
||||
/// Signal to send to the container process on stop (from image config).
|
||||
public var stopSignal: String?
|
||||
/// The time at which the container was created.
|
||||
public var creationDate: Date = Date()
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case id
|
||||
@@ -85,6 +88,7 @@ public struct ContainerConfiguration: Sendable, Codable {
|
||||
case capDrop
|
||||
case shmSize
|
||||
case stopSignal
|
||||
case creationDate
|
||||
}
|
||||
|
||||
/// Create a configuration from the supplied Decoder, initializing missing
|
||||
@@ -120,6 +124,7 @@ public struct ContainerConfiguration: Sendable, Codable {
|
||||
capDrop = try container.decodeIfPresent([String].self, forKey: .capDrop) ?? []
|
||||
shmSize = try container.decodeIfPresent(UInt64.self, forKey: .shmSize)
|
||||
stopSignal = try container.decodeIfPresent(String.self, forKey: .stopSignal)
|
||||
creationDate = try container.decodeIfPresent(Date.self, forKey: .creationDate) ?? Date(timeIntervalSince1970: 0)
|
||||
}
|
||||
|
||||
public struct DNSConfiguration: Sendable, Codable {
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
// 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
|
||||
|
||||
/// The runtime status of a container. Identity-free: the container's id lives
|
||||
/// on its ``ContainerConfiguration``.
|
||||
public struct ContainerStatus: Codable, Sendable {
|
||||
/// The state-machine value for the container (running, stopped, …).
|
||||
public let state: RuntimeStatus
|
||||
/// Network attachments provided to the container.
|
||||
public let networks: [Attachment]
|
||||
/// When the container was started, if it has been.
|
||||
public let startedDate: Date?
|
||||
|
||||
public init(state: RuntimeStatus, networks: [Attachment], startedDate: Date? = nil) {
|
||||
self.state = state
|
||||
self.networks = networks
|
||||
self.startedDate = startedDate
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
// 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 ContainerizationOCI
|
||||
import Foundation
|
||||
|
||||
/// A container as a managed resource. Wraps the persistent
|
||||
/// ``ContainerConfiguration`` and a runtime ``ContainerStatus``.
|
||||
public struct ManagedContainer: ManagedResource {
|
||||
public let configuration: ContainerConfiguration
|
||||
public let status: ContainerStatus
|
||||
|
||||
// MARK: ManagedResource
|
||||
public var id: String { configuration.id }
|
||||
public var name: String { configuration.id } // containers have no separate name field today
|
||||
|
||||
public var creationDate: Date { configuration.creationDate }
|
||||
|
||||
/// Typed labels for conformance / filtering. Drops labels that fail
|
||||
/// validation; the raw dictionary is preserved on `configuration.labels`
|
||||
/// and is what gets serialized.
|
||||
public var labels: ResourceLabels { (try? ResourceLabels(configuration.labels)) ?? .init() }
|
||||
|
||||
/// Platform passthrough (parity with the former ContainerSnapshot.platform).
|
||||
public var platform: ContainerizationOCI.Platform { configuration.platform }
|
||||
|
||||
/// Mint ids the way containers actually mint them (lowercased UUID),
|
||||
/// not the protocol's 64-hex default.
|
||||
public static func generateId() -> String { UUID().uuidString.lowercased() }
|
||||
|
||||
/// Container name rule (mixed case, dots, hyphens, underscores). Duplicated from
|
||||
/// Utility.validEntityName
|
||||
public static func nameValid(_ name: String) -> Bool {
|
||||
let pattern = #"^[a-zA-Z0-9][a-zA-Z0-9_.-]+$"#
|
||||
return name.range(of: pattern, options: .regularExpression) != nil
|
||||
}
|
||||
|
||||
public init(configuration: ContainerConfiguration, status: ContainerStatus) {
|
||||
self.configuration = configuration
|
||||
self.status = status
|
||||
}
|
||||
|
||||
/// CLI-boundary factory: build from the snapshot the client returns today.
|
||||
public init(_ snapshot: ContainerSnapshot) {
|
||||
self.configuration = snapshot.configuration
|
||||
self.status = ContainerStatus(
|
||||
state: snapshot.status,
|
||||
networks: snapshot.networks,
|
||||
startedDate: snapshot.startedDate
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension ManagedContainer {
|
||||
enum CodingKeys: String, CodingKey { case id, configuration, status }
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var c = encoder.container(keyedBy: CodingKeys.self)
|
||||
try c.encode(id, forKey: .id)
|
||||
try c.encode(configuration, forKey: .configuration)
|
||||
try c.encode(status, forKey: .status)
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let c = try decoder.container(keyedBy: CodingKeys.self)
|
||||
self.configuration = try c.decode(ContainerConfiguration.self, forKey: .configuration)
|
||||
self.status = try c.decode(ContainerStatus.self, forKey: .status)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user