Skip to content

Commit bfecf48

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

File tree

6 files changed

+90
-12
lines changed

6 files changed

+90
-12
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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(
30+
repository: ImageReference.Repository,
31+
mediaType: String,
32+
data: Data
33+
) async throws -> ContentDescriptor
34+
35+
/// Uploads a blob to the registry.
36+
///
37+
/// This function converts an encodable blob to an `application/octet-stream',
38+
/// calculates its digest and uploads it to the registry.
39+
/// - Parameters:
40+
/// - repository: Name of the destination repository.
41+
/// - mediaType: mediaType field for returned ContentDescriptor.
42+
/// On the wire, all blob uploads are `application/octet-stream'.
43+
/// - data: Object to be uploaded.
44+
/// - Returns: An ContentDescriptor object representing the
45+
/// uploaded blob.
46+
/// - Throws: If the blob cannot be encoded or the upload fails.
47+
///
48+
/// Some JSON objects, such as ImageConfiguration, are stored
49+
/// in the registry as plain blobs with MIME type "application/octet-stream".
50+
/// This function encodes the data parameter and uploads it as a generic blob.
51+
func putBlob<Body: Encodable>(
52+
repository: ImageReference.Repository,
53+
mediaType: String,
54+
data: Body
55+
) async throws -> ContentDescriptor
56+
57+
func blobExists(
58+
repository: ImageReference.Repository,
59+
digest: ImageReference.Digest
60+
) async throws -> Bool
61+
62+
func putManifest(
63+
repository: ImageReference.Repository,
64+
reference: (any ImageReference.Reference)?,
65+
manifest: ImageManifest
66+
) async throws -> ContentDescriptor
67+
}
68+
69+
extension ImageDestination {
70+
public func putBlob(
71+
repository: ImageReference.Repository,
72+
mediaType: String = "application/octet-stream",
73+
data: Data
74+
) async throws -> ContentDescriptor {
75+
try await putBlob(repository: repository, mediaType: mediaType, data: data)
76+
}
77+
}

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)