Skip to content

Commit 4143511

Browse files
committed
ContainerRegistry: Define ImageSource protocol
1 parent 03b4d9e commit 4143511

File tree

6 files changed

+65
-9
lines changed

6 files changed

+65
-9
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 ImageSource {
18+
/// Fetches an unstructured blob of data from the registry.
19+
///
20+
/// - Parameters:
21+
/// - repository: Name of the repository containing the blob.
22+
/// - digest: Digest of the blob.
23+
/// - Returns: The downloaded data.
24+
/// - Throws: If the blob download fails.
25+
func getBlob(
26+
repository: ImageReference.Repository,
27+
digest: ImageReference.Digest
28+
) async throws -> Data
29+
30+
func getManifest(
31+
repository: ImageReference.Repository,
32+
reference: any ImageReference.Reference
33+
) async throws -> (ImageManifest, ContentDescriptor)
34+
35+
func getIndex(
36+
repository: ImageReference.Repository,
37+
reference: any ImageReference.Reference
38+
) async throws -> ImageIndex
39+
40+
/// Get an image configuration record from the registry.
41+
/// - Parameters:
42+
/// - image: Reference to the image containing the record.
43+
/// - digest: Digest of the record.
44+
/// - Returns: The image confguration record stored in `repository` with digest `digest`.
45+
/// - Throws: If the blob cannot be decoded as an `ImageConfiguration`.
46+
///
47+
/// Image configuration records are stored as blobs in the registry. This function retrieves
48+
/// the requested blob and tries to decode it as a configuration record.
49+
func getImageConfiguration(
50+
forImage image: ImageReference,
51+
digest: ImageReference.Digest
52+
) async throws -> ImageConfiguration
53+
}

Sources/ContainerRegistry/RegistryClient+ImageConfiguration.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import class Foundation.JSONDecoder
16+
1517
extension RegistryClient {
1618
/// Get an image configuration record from the registry.
1719
/// - Parameters:

Sources/ContainerRegistry/RegistryClient.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,5 +375,5 @@ extension RegistryClient {
375375
/// Make decoded registry errors throwable
376376
extension DistributionErrors: Error {}
377377

378-
/// Images can be uploaded to a registry using a RegistryClient instance
379-
extension RegistryClient: ImageDestination {}
378+
/// RegistryClient can both fetch and upload images
379+
extension RegistryClient: ImageSource, ImageDestination {}

Sources/containertool/Extensions/RegistryClient+CopyBlobs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import ContainerRegistry
1616

17-
extension RegistryClient {
17+
extension ImageSource {
1818
/// Copies a blob from another registry to this one.
1919
/// - Parameters:
2020
/// - digest: The digest of the blob to copy.

Sources/containertool/Extensions/RegistryClient+Layers.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
import struct Foundation.Data
1616
import ContainerRegistry
1717

18-
extension RegistryClient {
19-
func getImageManifest(forImage image: ImageReference, architecture: String) async throws -> (
20-
ImageManifest, ContentDescriptor
21-
) {
18+
extension ImageSource {
19+
public func getImageManifest(
20+
forImage image: ImageReference,
21+
architecture: String
22+
) async throws -> (ImageManifest, ContentDescriptor) {
2223
do {
2324
// Try to retrieve a manifest. If the object with this reference is actually an index, the content-type will not match and
2425
// an error will be thrown.

Sources/containertool/Extensions/RegistryClient+publish.swift

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

21-
func publishContainerImage<Destination: ImageDestination>(
21+
func publishContainerImage<Source: ImageSource, Destination: ImageDestination>(
2222
baseImage: ImageReference,
2323
destinationImage: ImageReference,
24-
source: RegistryClient?,
24+
source: Source?,
2525
destination: Destination,
2626
architecture: String,
2727
os: String,

0 commit comments

Comments
 (0)