[container]: add startedDate field (#1018)

- Closes #302.
- Closes #336 (obsoletes this PR).
This commit is contained in:
Saehej Kang
2026-01-07 16:53:33 -08:00
committed by GitHub
parent db8932ab0f
commit 9d06475aaf
4 changed files with 21 additions and 4 deletions
@@ -48,7 +48,7 @@ extension Application {
}
private func createHeader() -> [[String]] {
[["ID", "IMAGE", "OS", "ARCH", "STATE", "ADDR", "CPUS", "MEMORY"]]
[["ID", "IMAGE", "OS", "ARCH", "STATE", "ADDR", "CPUS", "MEMORY", "STARTED"]]
}
private func printContainers(containers: [ClientContainer], format: ListFormat) throws {
@@ -97,6 +97,7 @@ extension ClientContainer {
self.networks.compactMap { $0.ipv4Address.description }.joined(separator: ","),
"\(self.configuration.resources.cpus)",
"\(self.configuration.resources.memoryInBytes / (1024 * 1024)) MB",
self.startedDate.map { ISO8601DateFormatter().string(from: $0) } ?? "",
]
}
}
@@ -105,10 +106,12 @@ struct PrintableContainer: Codable {
let status: RuntimeStatus
let configuration: ContainerConfiguration
let networks: [Attachment]
let startedDate: Date?
init(_ container: ClientContainer) {
self.status = container.status
self.configuration = container.configuration
self.networks = container.networks
self.startedDate = container.startedDate
}
}
@@ -14,6 +14,8 @@
// limitations under the License.
//===----------------------------------------------------------------------===//
import Foundation
/// A snapshot of a container along with its configuration
/// and any runtime state information.
public struct ContainerSnapshot: Codable, Sendable {
@@ -23,14 +25,18 @@ public struct ContainerSnapshot: Codable, Sendable {
public var status: RuntimeStatus
/// Network interfaces attached to the sandbox that are provided to the container.
public var networks: [Attachment]
/// When the container was started.
public var startedDate: Date?
public init(
configuration: ContainerConfiguration,
status: RuntimeStatus,
networks: [Attachment]
networks: [Attachment],
startedDate: Date? = nil
) {
self.configuration = configuration
self.status = status
self.networks = networks
self.startedDate = startedDate
}
}
@@ -43,16 +43,21 @@ public struct ClientContainer: Sendable, Codable {
/// Network allocated to the container.
public let networks: [Attachment]
/// When the container was started.
public let startedDate: Date?
package init(configuration: ContainerConfiguration) {
self.configuration = configuration
self.status = .stopped
self.networks = []
self.startedDate = nil
}
init(snapshot: ContainerSnapshot) {
self.configuration = snapshot.configuration
self.status = snapshot.status
self.networks = snapshot.networks
self.startedDate = snapshot.startedDate
}
}
@@ -87,7 +87,8 @@ public actor ContainersService {
snapshot: .init(
configuration: config,
status: .stopped,
networks: []
networks: [],
startedDate: nil
)
)
results[config.id] = state
@@ -249,7 +250,8 @@ public actor ContainersService {
let snapshot = ContainerSnapshot(
configuration: configuration,
status: .stopped,
networks: []
networks: [],
startedDate: nil
)
await self.setContainerState(configuration.id, ContainerState(snapshot: snapshot), context: context)
} catch {
@@ -371,6 +373,7 @@ public actor ContainersService {
let sandboxSnapshot = try await client.state()
state.snapshot.status = .running
state.snapshot.networks = sandboxSnapshot.networks
state.snapshot.startedDate = Date()
await self.setContainerState(id, state, context: context)
}
}