From 9d06475aafefc9dfbb0b6dc344d2bbe036842011 Mon Sep 17 00:00:00 2001 From: Saehej Kang Date: Wed, 7 Jan 2026 16:53:33 -0800 Subject: [PATCH] [container]: add startedDate field (#1018) - Closes #302. - Closes #336 (obsoletes this PR). --- Sources/ContainerCommands/Container/ContainerList.swift | 5 ++++- .../ContainerResource/Container/ContainerSnapshot.swift | 8 +++++++- .../ContainerAPIService/Client/ClientContainer.swift | 5 +++++ .../Server/Containers/ContainersService.swift | 7 +++++-- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Sources/ContainerCommands/Container/ContainerList.swift b/Sources/ContainerCommands/Container/ContainerList.swift index 6f72230f..ba3a0d6f 100644 --- a/Sources/ContainerCommands/Container/ContainerList.swift +++ b/Sources/ContainerCommands/Container/ContainerList.swift @@ -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 } } diff --git a/Sources/ContainerResource/Container/ContainerSnapshot.swift b/Sources/ContainerResource/Container/ContainerSnapshot.swift index 3b5628bb..fc9dbcbf 100644 --- a/Sources/ContainerResource/Container/ContainerSnapshot.swift +++ b/Sources/ContainerResource/Container/ContainerSnapshot.swift @@ -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 } } diff --git a/Sources/Services/ContainerAPIService/Client/ClientContainer.swift b/Sources/Services/ContainerAPIService/Client/ClientContainer.swift index 6231506c..0eb3dc6e 100644 --- a/Sources/Services/ContainerAPIService/Client/ClientContainer.swift +++ b/Sources/Services/ContainerAPIService/Client/ClientContainer.swift @@ -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 } } diff --git a/Sources/Services/ContainerAPIService/Server/Containers/ContainersService.swift b/Sources/Services/ContainerAPIService/Server/Containers/ContainersService.swift index 1a1e4f13..033166d9 100644 --- a/Sources/Services/ContainerAPIService/Server/Containers/ContainersService.swift +++ b/Sources/Services/ContainerAPIService/Server/Containers/ContainersService.swift @@ -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) } }