-
Notifications
You must be signed in to change notification settings - Fork 743
Use NetworkResource for network management in API server. #1421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
Sources/ContainerCommands/Network/NetworkResource+ListDisplayable.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // Copyright © 2026 Apple Inc. and the container project authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import ContainerResource | ||
|
|
||
| extension NetworkResource: ListDisplayable { | ||
| public static var tableHeader: [String] { | ||
| ["NETWORK", "STATE", "SUBNET"] | ||
| } | ||
|
|
||
| public var tableRow: [String] { | ||
| [id, status.phase, status.ipv4Subnet?.description ?? "none"] | ||
| } | ||
|
|
||
| public var quietValue: String { | ||
| id | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
Sources/ContainerResource/Network/NetworkResource.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // Copyright © 2026 Apple Inc. and the container project authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import ContainerizationExtras | ||
| import Foundation | ||
|
|
||
| /// A network resource, representing a configured virtual network and its runtime status. | ||
| /// | ||
| /// `NetworkResource` conforms to `ManagedResource` and separates the network's | ||
| /// intrinsic configuration from its ephemeral runtime status — following the same | ||
| /// config/status split used by Kubernetes and Docker. `config` is persisted; | ||
| /// `status` reflects what the network plugin reports at runtime. | ||
| /// | ||
| /// The JSON encoding uses a single `status` object containing a `phase` field | ||
| /// alongside any runtime-allocated address properties, replacing the prior flat | ||
| /// `state`/`status` pair in the CLI output. | ||
| public struct NetworkResource: ManagedResource { | ||
| /// The network's configuration — its persistent, intrinsic properties. | ||
| public let config: NetworkConfiguration | ||
|
|
||
| /// The network's current status, including lifecycle phase and any | ||
| /// runtime-allocated address properties. | ||
| public let status: NetworkStatus | ||
|
|
||
| // MARK: ManagedResource | ||
|
|
||
| /// The unique identifier for this network. Identical to ``config/id``. | ||
| public var id: String { config.id } | ||
|
|
||
| /// The user-assigned name for this network. For networks, name and ID are the same. | ||
| public var name: String { config.id } | ||
|
|
||
| /// The time at which this network was created. | ||
| public var creationDate: Date { config.creationDate } | ||
|
|
||
| /// Key-value labels for this network. | ||
| public var labels: ResourceLabels { config.labels } | ||
|
|
||
| /// Returns `true` for a system-managed network that cannot be deleted by the user. | ||
| public var isBuiltin: Bool { labels.isBuiltin } | ||
|
|
||
| /// Returns `true` if `name` is a syntactically valid network identifier. | ||
| /// | ||
| /// Valid network names are lowercase alphanumeric strings of up to 63 | ||
| /// characters, allowing dots, hyphens, and underscores in interior positions. | ||
| public static func nameValid(_ name: String) -> Bool { | ||
|
jglogan marked this conversation as resolved.
|
||
| let pattern = #"^[a-z0-9](?:[a-z0-9._-]{0,61}[a-z0-9])?$"# | ||
| return name.range(of: pattern, options: .regularExpression) != nil | ||
| } | ||
|
|
||
| // MARK: Initialization | ||
|
|
||
| /// Creates a network resource. | ||
| /// | ||
| /// - Parameters: | ||
| /// - config: The network's intrinsic configuration. | ||
| /// - networkStatus: The plugin-reported runtime status, or `nil` if the | ||
| /// network is not yet running. | ||
| public init(config: NetworkConfiguration, networkStatus: NetworkPluginStatus? = nil) { | ||
| self.config = config | ||
| self.status = networkStatus.map { NetworkStatus(running: $0) } ?? .created | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Conversion from NetworkState | ||
|
|
||
| extension NetworkResource { | ||
| /// Creates a network resource from a ``NetworkState``. | ||
| /// | ||
| /// Used when translating from the internal plugin-protocol type to the | ||
| /// public API surface type. | ||
| public init(_ networkState: NetworkState) { | ||
| switch networkState { | ||
| case .created(let config): | ||
| self.init(config: config) | ||
| case .running(let config, let status): | ||
| self.init(config: config, networkStatus: status) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Codable | ||
|
|
||
| extension NetworkResource { | ||
| enum CodingKeys: String, CodingKey { | ||
| case id | ||
|
ajemory marked this conversation as resolved.
|
||
| case config | ||
| case status | ||
| } | ||
|
|
||
| public func encode(to encoder: Encoder) throws { | ||
| var container = encoder.container(keyedBy: CodingKeys.self) | ||
| try container.encode(id, forKey: .id) | ||
| try container.encode(config, forKey: .config) | ||
| try container.encode(status, forKey: .status) | ||
| } | ||
|
|
||
| public init(from decoder: Decoder) throws { | ||
| let container = try decoder.container(keyedBy: CodingKeys.self) | ||
| self.config = try container.decode(NetworkConfiguration.self, forKey: .config) | ||
| self.status = try container.decode(NetworkStatus.self, forKey: .status) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // Copyright © 2026 Apple Inc. and the container project authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import ContainerizationExtras | ||
| import Foundation | ||
|
|
||
| /// The runtime status of a network resource. | ||
| /// | ||
| /// `phase` names the current lifecycle stage; the address fields are present | ||
| /// only when `phase` is `"running"` and are `nil` otherwise. Clients should | ||
| /// treat unrecognised `phase` values as unknown forward-compatible stages rather | ||
| /// than treating them as errors. | ||
| public struct NetworkStatus: Codable, Sendable { | ||
| /// The current lifecycle phase of the network. | ||
| /// | ||
| /// Defined values: `"created"` (configured, plugin not yet active) and | ||
| /// `"running"` (plugin active, subnet and gateway assigned). | ||
| public let phase: String | ||
|
|
||
| /// The allocated IPv4 subnet. Present only when `phase` is `"running"`. | ||
| public let ipv4Subnet: CIDRv4? | ||
|
|
||
| /// The IPv4 gateway address. Present only when `phase` is `"running"`. | ||
| public let ipv4Gateway: IPv4Address? | ||
|
|
||
| /// The allocated IPv6 subnet. Present only when `phase` is `"running"` and | ||
| /// the network has IPv6 enabled. | ||
| public let ipv6Subnet: CIDRv6? | ||
|
|
||
| public init( | ||
| phase: String, | ||
| ipv4Subnet: CIDRv4? = nil, | ||
| ipv4Gateway: IPv4Address? = nil, | ||
| ipv6Subnet: CIDRv6? = nil | ||
| ) { | ||
| self.phase = phase | ||
| self.ipv4Subnet = ipv4Subnet | ||
| self.ipv4Gateway = ipv4Gateway | ||
| self.ipv6Subnet = ipv6Subnet | ||
| } | ||
| } | ||
|
|
||
| extension NetworkStatus { | ||
| /// The status value for a network that is configured but not yet running. | ||
| public static let created = NetworkStatus(phase: "created") | ||
|
|
||
| /// Creates a running-phase status from a ``NetworkPluginStatus``. | ||
| init(running networkStatus: NetworkPluginStatus) { | ||
| self.init( | ||
| phase: "running", | ||
| ipv4Subnet: networkStatus.ipv4Subnet, | ||
| ipv4Gateway: networkStatus.ipv4Gateway, | ||
| ipv6Subnet: networkStatus.ipv6Subnet | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.