Skip to content

Commit 9d06475

Browse files
authored
[container]: add startedDate field (#1018)
- Closes #302. - Closes #336 (obsoletes this PR).
1 parent db8932a commit 9d06475

4 files changed

Lines changed: 21 additions & 4 deletions

File tree

Sources/ContainerCommands/Container/ContainerList.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extension Application {
4848
}
4949

5050
private func createHeader() -> [[String]] {
51-
[["ID", "IMAGE", "OS", "ARCH", "STATE", "ADDR", "CPUS", "MEMORY"]]
51+
[["ID", "IMAGE", "OS", "ARCH", "STATE", "ADDR", "CPUS", "MEMORY", "STARTED"]]
5252
}
5353

5454
private func printContainers(containers: [ClientContainer], format: ListFormat) throws {
@@ -97,6 +97,7 @@ extension ClientContainer {
9797
self.networks.compactMap { $0.ipv4Address.description }.joined(separator: ","),
9898
"\(self.configuration.resources.cpus)",
9999
"\(self.configuration.resources.memoryInBytes / (1024 * 1024)) MB",
100+
self.startedDate.map { ISO8601DateFormatter().string(from: $0) } ?? "",
100101
]
101102
}
102103
}
@@ -105,10 +106,12 @@ struct PrintableContainer: Codable {
105106
let status: RuntimeStatus
106107
let configuration: ContainerConfiguration
107108
let networks: [Attachment]
109+
let startedDate: Date?
108110

109111
init(_ container: ClientContainer) {
110112
self.status = container.status
111113
self.configuration = container.configuration
112114
self.networks = container.networks
115+
self.startedDate = container.startedDate
113116
}
114117
}

Sources/ContainerResource/Container/ContainerSnapshot.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// limitations under the License.
1515
//===----------------------------------------------------------------------===//
1616

17+
import Foundation
18+
1719
/// A snapshot of a container along with its configuration
1820
/// and any runtime state information.
1921
public struct ContainerSnapshot: Codable, Sendable {
@@ -23,14 +25,18 @@ public struct ContainerSnapshot: Codable, Sendable {
2325
public var status: RuntimeStatus
2426
/// Network interfaces attached to the sandbox that are provided to the container.
2527
public var networks: [Attachment]
28+
/// When the container was started.
29+
public var startedDate: Date?
2630

2731
public init(
2832
configuration: ContainerConfiguration,
2933
status: RuntimeStatus,
30-
networks: [Attachment]
34+
networks: [Attachment],
35+
startedDate: Date? = nil
3136
) {
3237
self.configuration = configuration
3338
self.status = status
3439
self.networks = networks
40+
self.startedDate = startedDate
3541
}
3642
}

Sources/Services/ContainerAPIService/Client/ClientContainer.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,21 @@ public struct ClientContainer: Sendable, Codable {
4343
/// Network allocated to the container.
4444
public let networks: [Attachment]
4545

46+
/// When the container was started.
47+
public let startedDate: Date?
48+
4649
package init(configuration: ContainerConfiguration) {
4750
self.configuration = configuration
4851
self.status = .stopped
4952
self.networks = []
53+
self.startedDate = nil
5054
}
5155

5256
init(snapshot: ContainerSnapshot) {
5357
self.configuration = snapshot.configuration
5458
self.status = snapshot.status
5559
self.networks = snapshot.networks
60+
self.startedDate = snapshot.startedDate
5661
}
5762
}
5863

Sources/Services/ContainerAPIService/Server/Containers/ContainersService.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ public actor ContainersService {
8787
snapshot: .init(
8888
configuration: config,
8989
status: .stopped,
90-
networks: []
90+
networks: [],
91+
startedDate: nil
9192
)
9293
)
9394
results[config.id] = state
@@ -249,7 +250,8 @@ public actor ContainersService {
249250
let snapshot = ContainerSnapshot(
250251
configuration: configuration,
251252
status: .stopped,
252-
networks: []
253+
networks: [],
254+
startedDate: nil
253255
)
254256
await self.setContainerState(configuration.id, ContainerState(snapshot: snapshot), context: context)
255257
} catch {
@@ -371,6 +373,7 @@ public actor ContainersService {
371373
let sandboxSnapshot = try await client.state()
372374
state.snapshot.status = .running
373375
state.snapshot.networks = sandboxSnapshot.networks
376+
state.snapshot.startedDate = Date()
374377
await self.setContainerState(id, state, context: context)
375378
}
376379
}

0 commit comments

Comments
 (0)