Skip to content

Commit 9fd15f0

Browse files
saehejkangedsai
andauthored
[networks]: add decoder/encoder to Attachment and NetworkStatus (#1216)
## Type of Change - [x] Bug fix - [ ] New feature - [x] Breaking change - [ ] Documentation update ## Motivation and Context Adds backward-compatible JSON decoding/encoding fallback for `Attachment` and `NetworkStatus` to prevent `keyNotFound` errors when communicating with older `container-apiserver` versions. Temporary fix but closes #1196 ## Testing Tests ✅ based on comment [here](#1196 (comment)) --------- Co-authored-by: Ed Saipetch <ed@twentybelow.com>
1 parent 85ec7ed commit 9fd15f0

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

Sources/ContainerResource/Network/Attachment.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,52 @@ public struct Attachment: Codable, Sendable {
5151
self.macAddress = macAddress
5252
self.mtu = mtu
5353
}
54+
55+
enum CodingKeys: String, CodingKey {
56+
case network
57+
case hostname
58+
case ipv4Address
59+
case ipv4Gateway
60+
case ipv6Address
61+
case macAddress
62+
case mtu
63+
// TODO: retain for deserialization compatibility for now, remove later
64+
case address
65+
case gateway
66+
}
67+
68+
/// Create a configuration from the supplied Decoder, initializing missing
69+
/// values where possible to reasonable defaults.
70+
public init(from decoder: Decoder) throws {
71+
let container = try decoder.container(keyedBy: CodingKeys.self)
72+
73+
network = try container.decode(String.self, forKey: .network)
74+
hostname = try container.decode(String.self, forKey: .hostname)
75+
if let address = try? container.decode(CIDRv4.self, forKey: .ipv4Address) {
76+
ipv4Address = address
77+
} else {
78+
ipv4Address = try container.decode(CIDRv4.self, forKey: .address)
79+
}
80+
if let gateway = try? container.decode(IPv4Address.self, forKey: .ipv4Gateway) {
81+
ipv4Gateway = gateway
82+
} else {
83+
ipv4Gateway = try container.decode(IPv4Address.self, forKey: .gateway)
84+
}
85+
ipv6Address = try container.decodeIfPresent(CIDRv6.self, forKey: .ipv6Address)
86+
macAddress = try container.decodeIfPresent(MACAddress.self, forKey: .macAddress)
87+
mtu = try container.decodeIfPresent(UInt32.self, forKey: .mtu)
88+
}
89+
90+
/// Encode the configuration to the supplied Encoder.
91+
public func encode(to encoder: Encoder) throws {
92+
var container = encoder.container(keyedBy: CodingKeys.self)
93+
94+
try container.encode(network, forKey: .network)
95+
try container.encode(hostname, forKey: .hostname)
96+
try container.encode(ipv4Address, forKey: .ipv4Address)
97+
try container.encode(ipv4Gateway, forKey: .ipv4Gateway)
98+
try container.encodeIfPresent(ipv6Address, forKey: .ipv6Address)
99+
try container.encodeIfPresent(macAddress, forKey: .macAddress)
100+
try container.encodeIfPresent(mtu, forKey: .mtu)
101+
}
54102
}

Sources/ContainerResource/Network/NetworkConfiguration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable {
8080
case ipv6Subnet
8181
case labels
8282
case pluginInfo
83-
// TODO: retain for deserialization compatability for now, remove later
83+
// TODO: retain for deserialization compatibility for now, remove later
8484
case subnet
8585
}
8686

Sources/ContainerResource/Network/NetworkState.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,43 @@ public struct NetworkStatus: Codable, Sendable {
3939
self.ipv4Gateway = ipv4Gateway
4040
self.ipv6Subnet = ipv6Subnet
4141
}
42+
43+
enum CodingKeys: String, CodingKey {
44+
case ipv4Subnet
45+
case ipv4Gateway
46+
case ipv6Subnet
47+
// TODO: retain for deserialization compatibility for now, remove later
48+
case address
49+
case gateway
50+
}
51+
52+
/// Create a configuration from the supplied Decoder, initializing missing
53+
/// values where possible to reasonable defaults.
54+
public init(from decoder: Decoder) throws {
55+
let container = try decoder.container(keyedBy: CodingKeys.self)
56+
57+
if let address = try? container.decode(CIDRv4.self, forKey: .ipv4Subnet) {
58+
ipv4Subnet = address
59+
} else {
60+
ipv4Subnet = try container.decode(CIDRv4.self, forKey: .address)
61+
}
62+
if let gateway = try? container.decode(IPv4Address.self, forKey: .ipv4Gateway) {
63+
ipv4Gateway = gateway
64+
} else {
65+
ipv4Gateway = try container.decode(IPv4Address.self, forKey: .gateway)
66+
}
67+
ipv6Subnet = try container.decodeIfPresent(String.self, forKey: .ipv6Subnet)
68+
.map { try CIDRv6($0) }
69+
}
70+
71+
/// Encode the configuration to the supplied Encoder.
72+
public func encode(to encoder: Encoder) throws {
73+
var container = encoder.container(keyedBy: CodingKeys.self)
74+
75+
try container.encode(ipv4Subnet, forKey: .ipv4Subnet)
76+
try container.encode(ipv4Gateway, forKey: .ipv4Gateway)
77+
try container.encodeIfPresent(ipv6Subnet, forKey: .ipv6Subnet)
78+
}
4279
}
4380

4481
/// The configuration and runtime attributes for a network.

0 commit comments

Comments
 (0)