Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Sources/APIServer/Containers/ContainersService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ actor ContainersService {
struct Item: Sendable {
let bundle: ContainerClient.Bundle
var state: State
var startedAt: Date?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startedAt is really only relevant when the container is alive, isn't it? We might want to consider associating this value with State.alive.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing seems a bit awkward on the SandboxService side of things (no fault of yours, you're just having to work with it) is that we have a model for runtime state that uses different types on the client (RuntimeStatus) and server (State) side.

The approach in the NetworkService is to have a NetworkState enum type that has an associated value for configuration data for the non-running states, and both config and runtime state values for the running states, and NetworkState is used on both client and server.

Let me look into whether we can take this approach here and I'll offer some guidance.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zk2k2 If you're still interested in moving this forward, have a look at the ContainersService now. The ContainersService.Item type has now been eliminated which should make adding this field a lot simpler.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jglogan, sure I'll check it out


enum State: Sendable {
case dead
Expand Down Expand Up @@ -81,7 +82,7 @@ actor ContainersService {
do {
let bundle = ContainerClient.Bundle(path: dir)
let config = try bundle.configuration
results[config.id] = .init(bundle: bundle, state: .dead)
results[config.id] = .init(bundle: bundle, state: .dead, startedAt: nil)
let plugin = runtimePlugins.first { $0.name == config.runtimeHandler }
guard let plugin else {
throw ContainerizationError(.internalError, message: "Failed to find runtime plugin \(config.runtimeHandler)")
Expand Down Expand Up @@ -158,7 +159,7 @@ actor ContainersService {
}
throw error
}
self.containers[configuration.id] = Item(bundle: bundle, state: .dead)
self.containers[configuration.id] = Item(bundle: bundle, state: .dead, startedAt: nil)
}

private func getInitBlock(for platform: Platform) async throws -> Filesystem {
Expand Down Expand Up @@ -277,6 +278,7 @@ actor ContainersService {
let configuration = try item.bundle.configuration
let client = SandboxClient(id: configuration.id, runtime: configuration.runtimeHandler)
item.state = .alive(client)
item.startedAt = Date()
await self.setContainer(id, item, context: context)
} catch {
self.log.error(
Expand Down Expand Up @@ -347,6 +349,7 @@ extension ContainersService.Item {
configuration: config,
status: RuntimeStatus.stopped,
networks: []
started: self.startedAt
), .stopped
)
case .alive(let client):
Expand All @@ -355,7 +358,8 @@ extension ContainersService.Item {
.init(
configuration: config,
status: state.status,
networks: state.networks
networks: state.networks,
startedAt: self.startedAt
), state.status
)
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/CLI/Container/ContainerList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ extension ClientContainer {
self.configuration.platform.os,
self.configuration.platform.architecture,
self.status.rawValue,
self.configuration.startedAt.map { ISO8601DateFormatter().string(from: $0) } ?? "",
self.networks.compactMap { try? CIDRAddress($0.address).address.description }.joined(separator: ","),
]
}
Expand All @@ -101,10 +102,12 @@ struct PrintableContainer: Codable {
let status: RuntimeStatus
let configuration: ContainerConfiguration
let networks: [Attachment]
let startedAt: Date?

init(_ container: ClientContainer) {
self.status = container.status
self.configuration = container.configuration
self.networks = container.networks
self.startedAt = container.startedAt
}
}
6 changes: 6 additions & 0 deletions Sources/ContainerClient/Core/ClientContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public struct ClientContainer: Sendable, Codable {
/// Network allocated to the container.
public let networks: [Attachment]

/// When the container was started.
public let startedAt: Date?

package init(configuration: ContainerConfiguration) {
self.configuration = configuration
self.status = .stopped
Expand All @@ -57,6 +60,7 @@ public struct ClientContainer: Sendable, Codable {
self.configuration = snapshot.configuration
self.status = snapshot.status
self.networks = snapshot.networks
self.startedAt = snapshot.startedAt
}

public var initProcess: ClientProcess {
Expand Down Expand Up @@ -248,4 +252,6 @@ extension ClientContainer {
)
}
}


}
6 changes: 5 additions & 1 deletion Sources/ContainerClient/Core/ContainerSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ public struct ContainerSnapshot: Codable, Sendable {
public let status: RuntimeStatus
/// Network interfaces attached to the sandbox that are provided to the container.
public let networks: [Attachment]
/// When the container was started.
public let startedAt: Date?

public init(
configuration: ContainerConfiguration,
status: RuntimeStatus,
networks: [Attachment]
networks: [Attachment],
startedAt: Date? = nil
) {
self.configuration = configuration
self.status = status
self.networks = networks
self.startedAt = startedAt
}
}
Loading