mirror of
https://github.com/apple/container.git
synced 2026-07-12 20:47:04 +00:00
23169055b9
The `ContainerResource` target uses `OrderedDictionary` (added in #1360) but doesn't declare a dependency on `swift-collections` in `Package.swift`, and the source files import the `Collections` umbrella module instead of `OrderedCollections`. This causes build failures when `OrderedDictionary` is used in public declarations, since the compiler requires the specific submodule to be imported in that context. - Add `swift-collections` dependency to the `ContainerResource` target in `Package.swift` - Change `import Collections` to `import OrderedCollections` in `ApplicationError.swift` and `ResourceLabels.swift`
118 lines
4.3 KiB
Swift
118 lines
4.3 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 OrderedCollections
|
|
|
|
/// Metadata for a managed resource.
|
|
public struct ResourceLabels: Sendable, Equatable {
|
|
public static let keyLengthMax = 128
|
|
|
|
public static let labelLengthMax = 4096
|
|
|
|
public let dictionary: [String: String]
|
|
|
|
public struct LabelError: AppError {
|
|
public var code: AppErrorCode
|
|
|
|
public var metadata: OrderedDictionary<String, String>
|
|
|
|
public var underlyingError: (any Error)? { nil }
|
|
}
|
|
|
|
public init() {
|
|
dictionary = [:]
|
|
}
|
|
|
|
public init(_ labels: [String: String]) throws {
|
|
for (key, value) in labels {
|
|
try Self.validateLabel(key: key, value: value)
|
|
}
|
|
self.dictionary = labels
|
|
}
|
|
|
|
public static func validateLabelKey(_ key: String) throws {
|
|
guard key.count <= Self.keyLengthMax else {
|
|
throw LabelError(code: .invalidLabelKeyLength, metadata: ["key": key, "maxLength": "\(Self.keyLengthMax)"])
|
|
}
|
|
let dockerPattern = #/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)*$/#
|
|
let ociPattern = #/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)*(?:/(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)*))*$/#
|
|
let dockerMatch = !key.ranges(of: dockerPattern).isEmpty
|
|
let ociMatch = !key.ranges(of: ociPattern).isEmpty
|
|
guard dockerMatch || ociMatch else {
|
|
throw LabelError(code: .invalidLabelKeyContent, metadata: ["key": key])
|
|
}
|
|
}
|
|
|
|
public static func validateLabel(key: String, value: String) throws {
|
|
try validateLabelKey(key)
|
|
let fullLabel = "\(key)=\(value)"
|
|
guard fullLabel.count <= labelLengthMax else {
|
|
throw LabelError(code: .invalidLabelLength, metadata: ["label": fullLabel, "maxLength": "\(Self.labelLengthMax)"])
|
|
}
|
|
}
|
|
}
|
|
|
|
extension ResourceLabels: Codable {
|
|
public func encode(to encoder: Encoder) throws {
|
|
try dictionary.encode(to: encoder)
|
|
}
|
|
|
|
public init(from decoder: Decoder) throws {
|
|
let dict = try [String: String](from: decoder)
|
|
try self.init(dict)
|
|
}
|
|
}
|
|
|
|
extension ResourceLabels: Collection {
|
|
public typealias Index = Dictionary<String, String>.Index
|
|
public typealias Element = Dictionary<String, String>.Element
|
|
|
|
public var startIndex: Index { dictionary.startIndex }
|
|
public var endIndex: Index { dictionary.endIndex }
|
|
|
|
public subscript(position: Index) -> Element { dictionary[position] }
|
|
public func index(after i: Index) -> Index { dictionary.index(after: i) }
|
|
|
|
// Direct key access
|
|
public subscript(key: String) -> String? {
|
|
get { dictionary[key] }
|
|
}
|
|
}
|
|
|
|
extension AppErrorCode {
|
|
public static let invalidLabelKeyContent = AppErrorCode(rawValue: "invalid_label_key_content")
|
|
public static let invalidLabelKeyLength = AppErrorCode(rawValue: "invalid_label_key_length")
|
|
public static let invalidLabelLength = AppErrorCode(rawValue: "invalid_label_length")
|
|
}
|
|
|
|
/// System-defined keys for resource labels.
|
|
public struct ResourceLabelKeys {
|
|
/// Indicates a owner of a resource managed by a plugin.
|
|
public static let plugin = "com.apple.container.plugin"
|
|
|
|
/// Indicates a resource with a reserved or dedicated purpose.
|
|
public static let role = "com.apple.container.resource.role"
|
|
}
|
|
|
|
/// System-defined values for resource the resource role label.
|
|
public struct ResourceRoleValues {
|
|
/// Indicates a container that can build images.
|
|
public static let builder = "builder"
|
|
|
|
/// Indicates a system-created resource that cannot be deleted by the user.
|
|
public static let builtin = "builtin"
|
|
}
|