Skip to content

Commit db8578d

Browse files
committed
ContainerRegistry: Add TarImageDestination
1 parent 8b2fdaa commit db8578d

File tree

3 files changed

+138
-8
lines changed

3 files changed

+138
-8
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 struct Foundation.Data
16+
import class Foundation.OutputStream
17+
import class Foundation.JSONDecoder
18+
import class Foundation.JSONEncoder
19+
import Tar
20+
21+
public class TarImageDestination {
22+
public var decoder: JSONDecoder
23+
var encoder: JSONEncoder
24+
25+
var archive: Archive
26+
27+
public init(toStream stream: OutputStream) throws {
28+
self.archive = Archive(toStream: stream)
29+
self.decoder = JSONDecoder()
30+
self.encoder = containerJSONEncoder()
31+
32+
try archive.appendFile(name: "oci-layout", data: [UInt8](encoder.encode(ImageLayoutHeader())))
33+
try archive.appendDirectory(name: "blobs")
34+
try archive.appendDirectory(name: "blobs/sha256")
35+
}
36+
}
37+
38+
extension TarImageDestination: ImageDestination {
39+
/// Saves a blob of unstructured data to the destination.
40+
/// - Parameters:
41+
/// - repository: Name of the destination repository.
42+
/// - mediaType: mediaType field for returned ContentDescriptor.
43+
/// On the wire, all blob uploads are `application/octet-stream'.
44+
/// - data: Object to be uploaded.
45+
/// - Returns: An ContentDescriptor object representing the
46+
/// saved blob.
47+
/// - Throws: If the blob cannot be encoded or the save fails.
48+
public func putBlob(
49+
repository: ImageReference.Repository,
50+
mediaType: String,
51+
data: Data
52+
) async throws -> ContentDescriptor {
53+
let digest = ImageReference.Digest(of: Data(data))
54+
try archive.appendFile(name: "\(digest.value)", prefix: "blobs/\(digest.algorithm)", data: [UInt8](data))
55+
return .init(mediaType: mediaType, digest: "\(digest)", size: Int64(data.count))
56+
}
57+
58+
/// Saves a JSON object to the destination, serialized as an unstructured blob.
59+
/// - Parameters:
60+
/// - repository: Name of the destination repository.
61+
/// - mediaType: mediaType field for returned ContentDescriptor.
62+
/// On the wire, all blob uploads are `application/octet-stream'.
63+
/// - data: Object to be uploaded.
64+
/// - Returns: An ContentDescriptor object representing the
65+
/// saved blob.
66+
/// - Throws: If the blob cannot be encoded or the save fails.
67+
public func putBlob<Body: Encodable>(
68+
repository: ImageReference.Repository,
69+
mediaType: String,
70+
data: Body
71+
) async throws -> ContentDescriptor {
72+
let encoded = try encoder.encode(data)
73+
return try await putBlob(repository: repository, mediaType: mediaType, data: encoded)
74+
}
75+
76+
public func blobExists(
77+
repository: ImageReference.Repository,
78+
digest: ImageReference.Digest
79+
) async throws -> Bool {
80+
false
81+
}
82+
83+
public func putManifest(
84+
repository: ImageReference.Repository,
85+
reference: (any ImageReference.Reference)?,
86+
manifest: ImageManifest
87+
) async throws -> ContentDescriptor {
88+
// Manifests are not special in the on-disk representation - they are just stored as blobs
89+
try await self.putBlob(
90+
repository: repository,
91+
mediaType: "application/vnd.oci.image.manifest.v1+json",
92+
data: manifest
93+
)
94+
}
95+
96+
public func putIndex(
97+
repository: ImageReference.Repository,
98+
reference: (any ImageReference.Reference)?,
99+
index: ImageIndex
100+
) async throws -> ContentDescriptor {
101+
// Unlike Manifest, Index is not written as a blob
102+
let encoded = try encoder.encode(index)
103+
let digest = ImageReference.Digest(of: encoded)
104+
let mediaType = index.mediaType ?? "application/vnd.oci.image.index.v1+json"
105+
106+
try archive.appendFile(name: "index.json", data: [UInt8](encoded))
107+
108+
try archive.appendFile(name: "\(digest.value)", prefix: "blobs/\(digest.algorithm)", data: [UInt8](encoded))
109+
return .init(mediaType: mediaType, digest: "\(digest)", size: Int64(encoded.count))
110+
}
111+
}
112+
113+
struct ImageLayoutHeader: Codable {
114+
var imageLayoutVersion: String = "1.0.0"
115+
}

Sources/Tar/tar.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ public struct Archive: ~Copyable {
349349
var output: OutputStream
350350

351351
/// Creates an empty Archive
352-
public init() {
353-
output = OutputStream.toMemory()
352+
public init(toStream: OutputStream = .toMemory()) {
353+
output = toStream
354354
output.open()
355355
output.schedule(in: .current, forMode: .default) // is this needed?
356356
}

Sources/containertool/containertool.swift

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
4343

4444
@Option(help: "The base container image name and optional tag")
4545
var from: String?
46+
47+
@Option(name: [.long, .short], help: "File in which the container image should be saved")
48+
var output: URL
4649
}
4750

4851
@OptionGroup(title: "Source and destination repository options")
@@ -203,12 +206,17 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
203206
if verbose { log("Connected to source registry: \(baseImage.registry)") }
204207
}
205208

206-
let destination = try await RegistryClient(
207-
registry: destinationImage.registry,
208-
insecure: authenticationOptions.allowInsecureHttp == .destination
209-
|| authenticationOptions.allowInsecureHttp == .both,
210-
auth: .init(username: username, password: password, auth: authProvider)
211-
)
209+
// let destination = try await RegistryClient(
210+
// registry: destinationImage.registry,
211+
// insecure: authenticationOptions.allowInsecureHttp == .destination
212+
// || authenticationOptions.allowInsecureHttp == .both,
213+
// auth: .init(username: username, password: password, auth: authProvider)
214+
// )
215+
216+
guard let saveStream = OutputStream(url: repositoryOptions.output, append: false) else {
217+
fatalError("failed to create tarball")
218+
}
219+
let destination = try TarImageDestination(toStream: saveStream)
212220

213221
if verbose { log("Connected to destination registry: \(destinationImage.registry)") }
214222
if verbose { log("Using base image: \(baseImage)") }
@@ -231,3 +239,10 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
231239
print(finalImage)
232240
}
233241
}
242+
243+
// Parse URL path arguments
244+
extension Foundation.URL: ArgumentParser.ExpressibleByArgument {
245+
public init?(argument: String) {
246+
self.init(fileURLWithPath: argument)
247+
}
248+
}

0 commit comments

Comments
 (0)