From 6f55360def12b77fc6f76a32ced3a0f9efbefb58 Mon Sep 17 00:00:00 2001 From: Saehej Kang Date: Tue, 2 Dec 2025 05:15:26 -0800 Subject: [PATCH] [networks]: Add creationDate field (#791) - Closes #665 - Existing containers that don't have `creationDate` default to the epoch date. --- .../ContainerNetworkService/NetworkConfiguration.swift | 7 +++++++ .../Services/ContainerNetworkService/NetworkState.swift | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/Sources/Services/ContainerNetworkService/NetworkConfiguration.swift b/Sources/Services/ContainerNetworkService/NetworkConfiguration.swift index d9c03fd8..217a7848 100644 --- a/Sources/Services/ContainerNetworkService/NetworkConfiguration.swift +++ b/Sources/Services/ContainerNetworkService/NetworkConfiguration.swift @@ -16,6 +16,7 @@ import ContainerizationError import ContainerizationExtras +import Foundation /// Configuration parameters for network creation. public struct NetworkConfiguration: Codable, Sendable, Identifiable { @@ -25,6 +26,9 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable { /// The network type public let mode: NetworkMode + /// When the network was created. + public let creationDate: Date + /// The preferred CIDR address for the subnet, if specified public let subnet: String? @@ -39,6 +43,7 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable { labels: [String: String] = [:] ) throws { self.id = id + self.creationDate = Date() self.mode = mode self.subnet = subnet self.labels = labels @@ -47,6 +52,7 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable { enum CodingKeys: String, CodingKey { case id + case creationDate case mode case subnet case labels @@ -58,6 +64,7 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable { let container = try decoder.container(keyedBy: CodingKeys.self) id = try container.decode(String.self, forKey: .id) + creationDate = try container.decodeIfPresent(Date.self, forKey: .creationDate) ?? Date(timeIntervalSince1970: 0) mode = try container.decode(NetworkMode.self, forKey: .mode) subnet = try container.decodeIfPresent(String.self, forKey: .subnet) labels = try container.decodeIfPresent([String: String].self, forKey: .labels) ?? [:] diff --git a/Sources/Services/ContainerNetworkService/NetworkState.swift b/Sources/Services/ContainerNetworkService/NetworkState.swift index 8626ff0e..0734afa5 100644 --- a/Sources/Services/ContainerNetworkService/NetworkState.swift +++ b/Sources/Services/ContainerNetworkService/NetworkState.swift @@ -53,4 +53,11 @@ public enum NetworkState: Codable, Sendable { case .running(let configuration, _): configuration.id } } + + public var creationDate: Date { + switch self { + case .created(let configuration): configuration.creationDate + case .running(let configuration, _): configuration.creationDate + } + } }