Skip to content

Commit 6e57637

Browse files
committed
ContainerRegistry: Define ImageDestination protocol
1 parent c941ef6 commit 6e57637

File tree

6 files changed

+84
-12
lines changed

6 files changed

+84
-12
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftContainerPlugin open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the SwiftContainerPlugin project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftContainerPlugin project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import Foundation
16+
17+
public protocol ImageDestination {
18+
/// Uploads a blob to the registry.
19+
///
20+
/// This function uploads a blob of unstructured data to the registry.
21+
/// - Parameters:
22+
/// - repository: Name of the destination repository.
23+
/// - mediaType: mediaType field for returned ContentDescriptor.
24+
/// On the wire, all blob uploads are `application/octet-stream'.
25+
/// - data: Object to be uploaded.
26+
/// - Returns: An ContentDescriptor object representing the
27+
/// uploaded blob.
28+
/// - Throws: If the blob cannot be encoded or the upload fails.
29+
func putBlob(repository: ImageReference.Repository, mediaType: String, data: Data)
30+
async throws -> ContentDescriptor
31+
32+
/// Uploads a blob to the registry.
33+
///
34+
/// This function converts an encodable blob to an `application/octet-stream',
35+
/// calculates its digest and uploads it to the registry.
36+
/// - Parameters:
37+
/// - repository: Name of the destination repository.
38+
/// - mediaType: mediaType field for returned ContentDescriptor.
39+
/// On the wire, all blob uploads are `application/octet-stream'.
40+
/// - data: Object to be uploaded.
41+
/// - Returns: An ContentDescriptor object representing the
42+
/// uploaded blob.
43+
/// - Throws: If the blob cannot be encoded or the upload fails.
44+
///
45+
/// Some JSON objects, such as ImageConfiguration, are stored
46+
/// in the registry as plain blobs with MIME type "application/octet-stream".
47+
/// This function encodes the data parameter and uploads it as a generic blob.
48+
func putBlob<Body: Encodable>(
49+
repository: ImageReference.Repository,
50+
mediaType: String,
51+
data: Body
52+
) async throws -> ContentDescriptor
53+
54+
func blobExists(repository: ImageReference.Repository, digest: ImageReference.Digest) async throws -> Bool
55+
56+
func putManifest(
57+
repository: ImageReference.Repository,
58+
reference: (any ImageReference.Reference)?,
59+
manifest: ImageManifest
60+
) async throws -> ContentDescriptor
61+
}
62+
63+
extension ImageDestination {
64+
public func putBlob(
65+
repository: ImageReference.Repository,
66+
mediaType: String = "application/octet-stream",
67+
data: Data
68+
) async throws -> ContentDescriptor {
69+
try await putBlob(repository: repository, mediaType: mediaType, data: data)
70+
}
71+
}

Sources/ContainerRegistry/RegistryClient+ImageConfiguration.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ extension RegistryClient {
2828
let data = try await getBlob(repository: image.repository, digest: digest)
2929
return try decoder.decode(ImageConfiguration.self, from: data)
3030
}
31+
}
3132

33+
extension ImageDestination {
3234
/// Upload an image configuration record to the registry.
3335
/// - Parameters:
3436
/// - image: Reference to the image associated with the record.

Sources/ContainerRegistry/RegistryClient.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,6 @@ extension RegistryClient {
374374

375375
/// Make decoded registry errors throwable
376376
extension DistributionErrors: Error {}
377+
378+
/// Images can be uploaded to a registry using a RegistryClient instance
379+
extension RegistryClient: ImageDestination {}

Sources/containertool/Extensions/RegistryClient+CopyBlobs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension RegistryClient {
2525
func copyBlob(
2626
digest: ImageReference.Digest,
2727
fromRepository sourceRepository: ImageReference.Repository,
28-
toClient destClient: RegistryClient,
28+
toClient destClient: ImageDestination,
2929
toRepository destRepository: ImageReference.Repository
3030
) async throws {
3131
if try await destClient.blobExists(repository: destRepository, digest: digest) {

Sources/containertool/Extensions/RegistryClient+Layers.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,21 @@ extension RegistryClient {
3737
)
3838
}
3939
}
40+
}
4041

41-
typealias DiffID = ImageReference.Digest
42-
struct ImageLayer {
43-
var descriptor: ContentDescriptor
44-
var diffID: DiffID
45-
}
46-
42+
extension ImageDestination {
4743
// A layer is a tarball, optionally compressed using gzip or zstd
4844
// See https://github.com/opencontainers/image-spec/blob/main/media-types.md
4945
func uploadLayer(
5046
repository: ImageReference.Repository,
5147
contents: [UInt8],
5248
mediaType: String = "application/vnd.oci.image.layer.v1.tar+gzip"
53-
) async throws -> ImageLayer {
49+
) async throws -> (descriptor: ContentDescriptor, diffID: ImageReference.Digest) {
5450
// The diffID is the hash of the unzipped layer tarball
5551
let diffID = ImageReference.Digest(of: contents)
5652
// The layer blob is the gzipped tarball; the descriptor is the hash of this gzipped blob
5753
let blob = Data(gzip(contents))
5854
let descriptor = try await putBlob(repository: repository, mediaType: mediaType, data: blob)
59-
return ImageLayer(descriptor: descriptor, diffID: diffID)
55+
return (descriptor: descriptor, diffID: diffID)
6056
}
6157
}

Sources/containertool/containertool.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,11 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
233233
}
234234
}
235235

236-
func publishContainerImage(
236+
func publishContainerImage<Destination: ImageDestination>(
237237
baseImage: ImageReference,
238238
destinationImage: ImageReference,
239239
source: RegistryClient?,
240-
destination: RegistryClient,
240+
destination: Destination,
241241
architecture: String,
242242
os: String,
243243
resources: [String],
@@ -279,7 +279,7 @@ func publishContainerImage(
279279

280280
// MARK: Upload resource layers
281281

282-
var resourceLayers: [RegistryClient.ImageLayer] = []
282+
var resourceLayers: [(descriptor: ContentDescriptor, diffID: ImageReference.Digest)] = []
283283
for resourceDir in resources {
284284
let resourceTardiff = try Archive().appendingRecursively(atPath: resourceDir).bytes
285285
let resourceLayer = try await destination.uploadLayer(

0 commit comments

Comments
 (0)