Skip to content

Commit 73491b8

Browse files
committed
ContainerRegistry: Define ImageDestination protocol
1 parent 41092be commit 73491b8

File tree

6 files changed

+107
-12
lines changed

6 files changed

+107
-12
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 of unstructured data.
19+
///
20+
/// - Parameters:
21+
/// - repository: Name of the destination repository.
22+
/// - mediaType: mediaType field for returned ContentDescriptor.
23+
/// On the wire, all blob uploads are `application/octet-stream'.
24+
/// - data: Object to be uploaded.
25+
/// - Returns: An ContentDescriptor object representing the
26+
/// uploaded blob.
27+
/// - Throws: If the blob cannot be encoded or the upload fails.
28+
func putBlob(
29+
repository: ImageReference.Repository,
30+
mediaType: String,
31+
data: Data
32+
) async throws -> ContentDescriptor
33+
34+
/// Encodes and uploads a JSON object.
35+
///
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+
/// Checks whether a blob exists.
55+
///
56+
/// - Parameters:
57+
/// - repository: Name of the destination repository.
58+
/// - digest: Digest of the requested blob.
59+
/// - Returns: True if the blob exists, otherwise false.
60+
/// - Throws: If the destination encounters an error.
61+
func blobExists(
62+
repository: ImageReference.Repository,
63+
digest: ImageReference.Digest
64+
) async throws -> Bool
65+
66+
/// Encodes and uploads an image manifest.
67+
///
68+
/// - Parameters:
69+
/// - repository: Name of the destination repository.
70+
/// - reference: Optional tag to apply to this manifest.
71+
/// - manifest: Manifest to be uploaded.
72+
/// - Returns: An ContentDescriptor object representing the
73+
/// uploaded blob.
74+
/// - Throws: If the blob cannot be encoded or the upload fails.
75+
///
76+
/// Manifests are not treated as blobs by the distribution specification.
77+
/// They have their own MIME types and are uploaded to different
78+
/// registry endpoints than blobs.
79+
func putManifest(
80+
repository: ImageReference.Repository,
81+
reference: (any ImageReference.Reference)?,
82+
manifest: ImageManifest
83+
) async throws -> ContentDescriptor
84+
}
85+
86+
extension ImageDestination {
87+
public func putBlob(
88+
repository: ImageReference.Repository,
89+
mediaType: String = "application/octet-stream",
90+
data: Data
91+
) async throws -> ContentDescriptor {
92+
try await putBlob(repository: repository, mediaType: mediaType, data: data)
93+
}
94+
}

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/Extensions/RegistryClient+publish.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import struct Foundation.URL
1818
import ContainerRegistry
1919
import Tar
2020

21-
func publishContainerImage(
21+
func publishContainerImage<Destination: ImageDestination>(
2222
baseImage: ImageReference,
2323
destinationImage: ImageReference,
2424
source: RegistryClient?,
25-
destination: RegistryClient,
25+
destination: Destination,
2626
architecture: String,
2727
os: String,
2828
resources: [String],
@@ -64,7 +64,7 @@ func publishContainerImage(
6464

6565
// MARK: Upload resource layers
6666

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

0 commit comments

Comments
 (0)