|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftContainerPlugin open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 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 struct Foundation.Data |
| 16 | + |
| 17 | +extension RegistryClient: 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 | + public func getBlob( |
| 26 | + repository: ImageReference.Repository, |
| 27 | + digest: ImageReference.Digest |
| 28 | + ) async throws -> Data { |
| 29 | + try await executeRequestThrowing( |
| 30 | + .get(repository, path: "blobs/\(digest)", accepting: ["application/octet-stream"]), |
| 31 | + decodingErrors: [.notFound] |
| 32 | + ) |
| 33 | + .data |
| 34 | + } |
| 35 | + |
| 36 | + /// Fetches an image manifest. |
| 37 | + /// |
| 38 | + /// - Parameters: |
| 39 | + /// - repository: Name of the source repository. |
| 40 | + /// - reference: Tag or digest of the manifest to fetch. |
| 41 | + /// - Returns: The downloaded manifest. |
| 42 | + /// - Throws: If the download fails or the manifest cannot be decoded. |
| 43 | + public func getManifest( |
| 44 | + repository: ImageReference.Repository, |
| 45 | + reference: any ImageReference.Reference |
| 46 | + ) async throws -> (ImageManifest, ContentDescriptor) { |
| 47 | + // See https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pulling-manifests |
| 48 | + let (data, response) = try await executeRequestThrowing( |
| 49 | + .get( |
| 50 | + repository, |
| 51 | + path: "manifests/\(reference)", |
| 52 | + accepting: [ |
| 53 | + "application/vnd.oci.image.manifest.v1+json", |
| 54 | + "application/vnd.docker.distribution.manifest.v2+json", |
| 55 | + ] |
| 56 | + ), |
| 57 | + decodingErrors: [.notFound] |
| 58 | + ) |
| 59 | + return ( |
| 60 | + try decoder.decode(ImageManifest.self, from: data), |
| 61 | + ContentDescriptor( |
| 62 | + mediaType: response.headerFields[.contentType] ?? "application/vnd.oci.image.manifest.v1+json", |
| 63 | + digest: "\(ImageReference.Digest(of: data))", |
| 64 | + size: Int64(data.count) |
| 65 | + ) |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + /// Fetches an image index. |
| 70 | + /// |
| 71 | + /// - Parameters: |
| 72 | + /// - repository: Name of the source repository. |
| 73 | + /// - reference: Tag or digest of the index to fetch. |
| 74 | + /// - Returns: The downloaded index. |
| 75 | + /// - Throws: If the download fails or the index cannot be decoded. |
| 76 | + public func getIndex( |
| 77 | + repository: ImageReference.Repository, |
| 78 | + reference: any ImageReference.Reference |
| 79 | + ) async throws -> ImageIndex { |
| 80 | + // See https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pulling-manifests |
| 81 | + let (data, _) = try await executeRequestThrowing( |
| 82 | + .get( |
| 83 | + repository, |
| 84 | + path: "manifests/\(reference)", |
| 85 | + accepting: [ |
| 86 | + "application/vnd.oci.image.index.v1+json", |
| 87 | + "application/vnd.docker.distribution.manifest.list.v2+json", |
| 88 | + ] |
| 89 | + ), |
| 90 | + decodingErrors: [.notFound] |
| 91 | + ) |
| 92 | + return try decoder.decode(ImageIndex.self, from: data) |
| 93 | + } |
| 94 | + |
| 95 | + /// Get an image configuration record from the registry. |
| 96 | + /// - Parameters: |
| 97 | + /// - image: Reference to the image containing the record. |
| 98 | + /// - digest: Digest of the record. |
| 99 | + /// - Returns: The image confguration record stored in `repository` with digest `digest`. |
| 100 | + /// - Throws: If the blob cannot be decoded as an `ImageConfiguration`. |
| 101 | + /// |
| 102 | + /// Image configuration records are stored as blobs in the registry. This function retrieves the requested blob and tries to decode it as a configuration record. |
| 103 | + public func getImageConfiguration( |
| 104 | + forImage image: ImageReference, |
| 105 | + digest: ImageReference.Digest |
| 106 | + ) async throws -> ImageConfiguration { |
| 107 | + let data = try await getBlob(repository: image.repository, digest: digest) |
| 108 | + return try decoder.decode(ImageConfiguration.self, from: data) |
| 109 | + } |
| 110 | +} |
0 commit comments