Files
container/Sources/ContainerResource/Common/ManagedResource.swift
T
J Logan b3b5c3e609 Use labels instead of id to discriminate the builtin network. (#1123)
- Closes #1122.
- Adds placeholder ManagedResource and unit tests. Nothing is using
these yet.
- Adds system-defined resource labels for owning plugin and resource
role. The system discriminates the builtin network using role "builtin".
- Adds builtin role when creating builtin network at startup, and
ensures that a preexisting network with ID "default" gets updated with
the role label.
- Replace all network ID checks for "default" with the builtin role
check.
- Adds "builder" role to builder VM.

## Type of Change
- [ ] Bug fix
- [x] New feature  
- [ ] Breaking change
- [ ] Documentation update

## Motivation and Context
Role and owner labels should make cross-cutting resource policy easier
to implement.

## Testing
- [x] Tested locally
- [x] Added/updated tests
- [ ] Added/updated docs
2026-02-02 12:24:27 -08:00

59 lines
2.4 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 Foundation
/// Common properties for all managed resources.
public protocol ManagedResource: Identifiable, Sendable, Codable {
/// A 64 byte hexadecimal string, assigned by the system, that uniquely
/// identifies the resource.
var id: String { get }
/// A user assigned name that shall be unique within the namespace of
/// the resource category. If the user does not assign a name, this value
/// shall be the same as the system-assigned identifier.
var name: String { get }
/// The time at which the system created the resource.
var creationDate: Date { get }
/// Key-value properties for the resource. The user and system may both
/// make use of labels to read and write annotations or other metadata.
/// A good practice is to use
var labels: [String: String] { get }
/// Generates a unique resource ID value.
static func generateId() -> String
/// Returns true only if the specified resource name is syntactically valid.
static func nameValid(_ name: String) -> Bool
}
extension ManagedResource {
/// Generate a random identifier that has the format of an ASCII SHA-256 hash.
public static func randomId() -> String {
(0..<2)
.map { _ in UInt128.random(in: 0...UInt128.max) }
.map { String($0, radix: 16).padding(toLength: 32, withPad: "0", startingAt: 0) }
.joined()
}
}
// FIXME: This moves to ManagedResource and/or a ResourceLabels typealias eventually.
extension [String: String] {
public var isBuiltin: Bool { self.contains { $0 == ResourceLabelKeys.role && $1 == ResourceRoleValues.builtin } }
}