Skip to content

Commit 8196e0a

Browse files
committed
containertool: Squash unnecessary layers of RegistryClient extensions
1 parent 0ce61e9 commit 8196e0a

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

Sources/containertool/Extensions/RegistryClient+Layers.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,24 @@ extension RegistryClient {
3636
}
3737
}
3838

39+
typealias DiffID = String
40+
struct ImageLayer {
41+
var descriptor: ContentDescriptor
42+
var diffID: DiffID
43+
}
44+
3945
// A layer is a tarball, optionally compressed using gzip or zstd
4046
// See https://github.com/opencontainers/image-spec/blob/main/media-types.md
41-
func uploadImageLayer(
47+
func uploadLayer(
4248
repository: String,
43-
layer: [UInt8],
49+
contents: [UInt8],
4450
mediaType: String = "application/vnd.oci.image.layer.v1.tar+gzip"
45-
) async throws -> ContentDescriptor {
46-
// The layer blob is the gzipped tarball
47-
let blob = Data(gzip(layer))
48-
return try await putBlob(repository: repository, mediaType: mediaType, data: blob)
51+
) async throws -> ImageLayer {
52+
// The diffID is the hash of the unzipped layer tarball
53+
let diffID = digest(of: contents)
54+
// The layer blob is the gzipped tarball; the descriptor is the hash of this gzipped blob
55+
let blob = Data(gzip(contents))
56+
let descriptor = try await putBlob(repository: repository, mediaType: mediaType, data: blob)
57+
return ImageLayer(descriptor: descriptor, diffID: diffID)
4958
}
5059
}

Sources/containertool/containertool.swift

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,19 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
152152

153153
// MARK: Upload resource layers
154154

155-
typealias DiffID = String
156-
var resourceLayers: [(descriptor: ContentDescriptor, diffID: String)] = []
155+
var resourceLayers: [RegistryClient.ImageLayer] = []
157156
for resourceDir in resources {
158157
let resourceTardiff = try Archive().appendingRecursively(atPath: resourceDir).bytes
159-
let resourceLayer = try await destination.uploadImageLayer(
158+
let resourceLayer = try await destination.uploadLayer(
160159
repository: destination_image.repository,
161-
layer: resourceTardiff
160+
contents: resourceTardiff
162161
)
163162

164163
if verbose {
165-
log("resource layer: \(resourceLayer.digest) (\(resourceLayer.size) bytes)")
164+
log("resource layer: \(resourceLayer.descriptor.digest) (\(resourceLayer.descriptor.size) bytes)")
166165
}
167166

168-
resourceLayers.append((resourceLayer, digest(of: resourceTardiff)))
167+
resourceLayers.append(resourceLayer)
169168
}
170169

171170
// MARK: Upload the application layer
@@ -256,16 +255,3 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
256255
print(destination_image)
257256
}
258257
}
259-
260-
extension RegistryClient {
261-
typealias DiffID = String
262-
struct ImageLayer {
263-
var descriptor: ContentDescriptor
264-
var diffID: DiffID
265-
}
266-
func uploadLayer(repository: String, contents: [UInt8]) async throws -> ImageLayer {
267-
let diffID = digest(of: contents)
268-
let descriptor = try await self.uploadImageLayer(repository: repository, layer: contents)
269-
return ImageLayer(descriptor: descriptor, diffID: diffID)
270-
}
271-
}

scripts/test-containertool-resources.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ touch "$PKGPATH/resourcedir/resource1.txt" "$PKGPATH/resourcedir/resource2.txt"
3333
touch "$PKGPATH/resourcefile.dat"
3434
swift run containertool \
3535
"$PKGPATH/hello" \
36-
--repository localhost:5000/resource_test \
36+
--repository localhost:5555/resource_test \
3737
--from scratch \
3838
--resources "$PKGPATH/resourcedir" \
3939
--resources "$PKGPATH/resourcefile.dat"
4040

4141
$RUNTIME rm -f resource-test
42-
$RUNTIME create --pull always --name resource-test localhost:5000/resource_test:latest
42+
$RUNTIME create --pull always --name resource-test localhost:5555/resource_test:latest
4343

4444
cleanup() {
4545
log "Deleting temporary package $PKGPATH"

0 commit comments

Comments
 (0)