Files
container/Sources/Services/ContainerAPIService/Client/ClientNetwork.swift
T
J Logan 3ad0914fd2 Fix CI integration test coldstart issues. (#1230)
- Closes #1206.
- Closes #1185.
- Closes #507.
- Addresses existing log messages for #642.
- Nondeterministic CI errors are resulting from very slow launch times
for the first runtime helper, which causes ContainersService to be
locked for longer than our 20 sec timeout. Bumping the timeout to 60
seconds addresses this case for now.
- Since many log messages needed to be changed to troubleshoot the
issue, updated all log messages to use structured logging, and
implemented consistent entry/exit logging for all service operations.
- Added logging for ContainerService lock acquisition to help with
finding root cause for the slow service startup.
- Plumbed the `--debug` flag on both `container system start` and
`container system logs` so that the flag is actually useful.
- Updated the `install-init.sh` script so that can install in a custom
app root directory.
2026-02-19 09:58:09 -08:00

98 lines
3.6 KiB
Swift

//===----------------------------------------------------------------------===//
// 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
import ContainerXPC
import ContainerizationError
import ContainerizationExtras
import ContainerizationOS
import Foundation
public struct ClientNetwork {
static let serviceIdentifier = "com.apple.container.apiserver"
public static let defaultNetworkName = "default"
public static let noNetworkName = "none"
}
extension ClientNetwork {
private static func newClient() -> XPCClient {
XPCClient(service: serviceIdentifier)
}
private static func xpcSend(
client: XPCClient,
message: XPCMessage,
timeout: Duration? = XPCClient.xpcRegistrationTimeout
) async throws -> XPCMessage {
try await client.send(message, responseTimeout: timeout)
}
public static func create(configuration: NetworkConfiguration) async throws -> NetworkState {
let client = Self.newClient()
let request = XPCMessage(route: .networkCreate)
request.set(key: .networkId, value: configuration.id)
let data = try JSONEncoder().encode(configuration)
request.set(key: .networkConfig, value: data)
let response = try await xpcSend(client: client, message: request)
let responseData = response.dataNoCopy(key: .networkState)
guard let responseData else {
throw ContainerizationError(.invalidArgument, message: "network configuration not received")
}
let state = try JSONDecoder().decode(NetworkState.self, from: responseData)
return state
}
public static func list() async throws -> [NetworkState] {
let client = Self.newClient()
let request = XPCMessage(route: .networkList)
let response = try await xpcSend(client: client, message: request, timeout: .seconds(1))
let responseData = response.dataNoCopy(key: .networkStates)
guard let responseData else {
return []
}
let states = try JSONDecoder().decode([NetworkState].self, from: responseData)
return states
}
/// Get the network for the provided id.
public static func get(id: String) async throws -> NetworkState {
let networks = try await list()
guard let network = networks.first(where: { $0.id == id }) else {
throw ContainerizationError(.notFound, message: "network \(id) not found")
}
return network
}
/// Delete the network with the given id.
public static func delete(id: String) async throws {
let client = Self.newClient()
let request = XPCMessage(route: .networkDelete)
request.set(key: .networkId, value: id)
let _ = try await xpcSend(client: client, message: request)
}
/// Retrieve the builtin network.
public static var builtin: NetworkState? {
get async throws {
try await list().first { $0.isBuiltin }
}
}
}