Files
container/Sources/Services/ContainerImagesService/Server/ImagesServiceHarness.swift
J Logan 98402bf4e6 Adds multiple image save to tarfile. (#560)
## Type of Change
- [ ] Bug fix
- [x] New feature  
- [ ] Breaking change
- [ ] Documentation update

## Description

```bash
% container image save -o container.tar python:alpine alpine:latest
Warning! Running debug build. Performance may be degraded.
Image(s) saved        

% tar tf container.tar 
oci-layout
blobs/
blobs/sha256/
blobs/sha256/02f8efbefad605a169e89926147edd0676646263268f303c6fb3cdfdbc4a9612
blobs/sha256/a4bb08daca6b0385b17761b170fc91b20ab2ec072f70f9260149f8d61846ac13
blobs/sha256/588270f913bc82b4dbeee27bc249e4d314894becd18cccdc13645f669972c91e
blobs/sha256/692b7bac6678f5809640189eb1d95a3277689ec643201c465ecd44e72db7d029
blobs/sha256/1d24a57b1de9b287d9a9e1e231b71a235b836dc1852155b943b927a411d8c394
blobs/sha256/f9841e55dcbf5a6fcc702b25ce6e411ebdcb30680f94afd8060cb20bb20bd75c
blobs/sha256/0b83d017db6efafadf6b3f18d087d2ce1d67d8f0e927dc7254b0ad088074cd3a
blobs/sha256/b2236d9e1563c507613962c1ebbd7b3d307969ec2ee355b781b68440f4f0bee3
blobs/sha256/6e174226ea690ced550e5641249a412cdbefd2d09871f3e64ab52137a54ba606
blobs/sha256/c879780ac011609647c8714eef9e6490c42bf20128b32f4b31c4daad1242647b
blobs/sha256/26a1da51444d4cbbba3233caa342d0397ac4f93dc8e305e31989fd782b3107da
index.json

% tar -xOf container.tar index.json | python3 -m json.tool
{
    "schemaVersion": 2,
    "mediaType": "application/vnd.oci.image.index.v1+json",
    "manifests": [
        {
            "mediaType": "application/vnd.oci.image.index.v1+json",
            "annotations": {
                "org.opencontainers.image.ref.name": "docker.io/library/python:alpine",
                "io.containerd.image.name": "docker.io/library/python:alpine",
                "com.apple.containerization.image.name": "docker.io/library/python:alpine"
            },
            "size": 944,
            "digest": "sha256:1d24a57b1de9b287d9a9e1e231b71a235b836dc1852155b943b927a411d8c394"
        },
        {
            "mediaType": "application/vnd.oci.image.index.v1+json",
            "annotations": {
                "io.containerd.image.name": "docker.io/library/alpine:latest",
                "org.opencontainers.image.ref.name": "docker.io/library/alpine:latest",
                "com.apple.containerization.image.name": "docker.io/library/alpine:latest"
            },
            "size": 497,
            "digest": "sha256:692b7bac6678f5809640189eb1d95a3277689ec643201c465ecd44e72db7d029"
        }
    ]
}
```

## Motivation and Context
`image load` can read multiple images from a tar file, but `image save`
cannot save multiple images today.

## Testing
- [x] Tested locally
- [ ] Added/updated tests (TODO: roundtrip test pull-save-rm-load)
- [ ] Added/updated docs (TODO: check command reference)
2025-09-09 16:46:18 -07:00

256 lines
9.6 KiB
Swift

//===----------------------------------------------------------------------===//
// Copyright © 2025 Apple Inc. and the container project authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//
import ContainerClient
import ContainerImagesServiceClient
import ContainerXPC
import Containerization
import ContainerizationError
import ContainerizationOCI
import Foundation
import Logging
public struct ImagesServiceHarness: Sendable {
let log: Logging.Logger
let service: ImagesService
public init(service: ImagesService, log: Logging.Logger) {
self.log = log
self.service = service
}
@Sendable
public func pull(_ message: XPCMessage) async throws -> XPCMessage {
let ref = message.string(key: .imageReference)
guard let ref else {
throw ContainerizationError(
.invalidArgument,
message: "missing image reference"
)
}
let platformData = message.dataNoCopy(key: .ociPlatform)
var platform: Platform? = nil
if let platformData {
platform = try JSONDecoder().decode(ContainerizationOCI.Platform.self, from: platformData)
}
let insecure = message.bool(key: .insecureFlag)
let progressUpdateService = ProgressUpdateService(message: message)
let imageDescription = try await service.pull(reference: ref, platform: platform, insecure: insecure, progressUpdate: progressUpdateService?.handler)
let imageData = try JSONEncoder().encode(imageDescription)
let reply = message.reply()
reply.set(key: .imageDescription, value: imageData)
return reply
}
@Sendable
public func push(_ message: XPCMessage) async throws -> XPCMessage {
let ref = message.string(key: .imageReference)
guard let ref else {
throw ContainerizationError(
.invalidArgument,
message: "missing image reference"
)
}
let platformData = message.dataNoCopy(key: .ociPlatform)
var platform: Platform? = nil
if let platformData {
platform = try JSONDecoder().decode(ContainerizationOCI.Platform.self, from: platformData)
}
let insecure = message.bool(key: .insecureFlag)
let progressUpdateService = ProgressUpdateService(message: message)
try await service.push(reference: ref, platform: platform, insecure: insecure, progressUpdate: progressUpdateService?.handler)
let reply = message.reply()
return reply
}
@Sendable
public func tag(_ message: XPCMessage) async throws -> XPCMessage {
let old = message.string(key: .imageReference)
guard let old else {
throw ContainerizationError(
.invalidArgument,
message: "missing image reference"
)
}
let new = message.string(key: .imageNewReference)
guard let new else {
throw ContainerizationError(
.invalidArgument,
message: "missing new image reference"
)
}
let newDescription = try await service.tag(old: old, new: new)
let descData = try JSONEncoder().encode(newDescription)
let reply = message.reply()
reply.set(key: .imageDescription, value: descData)
return reply
}
@Sendable
public func list(_ message: XPCMessage) async throws -> XPCMessage {
let images = try await service.list()
let imageData = try JSONEncoder().encode(images)
let reply = message.reply()
reply.set(key: .imageDescriptions, value: imageData)
return reply
}
@Sendable
public func delete(_ message: XPCMessage) async throws -> XPCMessage {
let ref = message.string(key: .imageReference)
guard let ref else {
throw ContainerizationError(
.invalidArgument,
message: "missing image reference"
)
}
let garbageCollect = message.bool(key: .garbageCollect)
try await self.service.delete(reference: ref, garbageCollect: garbageCollect)
let reply = message.reply()
return reply
}
@Sendable
public func save(_ message: XPCMessage) async throws -> XPCMessage {
let data = message.dataNoCopy(key: .imageDescriptions)
guard let data else {
throw ContainerizationError(
.invalidArgument,
message: "missing image description"
)
}
let imageDescriptions = try JSONDecoder().decode([ImageDescription].self, from: data)
let references = imageDescriptions.map { $0.reference }
let platformData = message.dataNoCopy(key: .ociPlatform)
var platform: Platform? = nil
if let platformData {
platform = try JSONDecoder().decode(ContainerizationOCI.Platform.self, from: platformData)
}
let out = message.string(key: .filePath)
guard let out else {
throw ContainerizationError(
.invalidArgument,
message: "missing output file path"
)
}
try await service.save(references: references, out: URL(filePath: out), platform: platform)
let reply = message.reply()
return reply
}
@Sendable
public func load(_ message: XPCMessage) async throws -> XPCMessage {
let input = message.string(key: .filePath)
guard let input else {
throw ContainerizationError(
.invalidArgument,
message: "missing input file path"
)
}
let images = try await service.load(from: URL(filePath: input))
let data = try JSONEncoder().encode(images)
let reply = message.reply()
reply.set(key: .imageDescriptions, value: data)
return reply
}
@Sendable
public func prune(_ message: XPCMessage) async throws -> XPCMessage {
let (deleted, size) = try await service.prune()
let reply = message.reply()
let data = try JSONEncoder().encode(deleted)
reply.set(key: .digests, value: data)
reply.set(key: .size, value: size)
return reply
}
}
// MARK: Image Snapshot Methods
extension ImagesServiceHarness {
@Sendable
public func unpack(_ message: XPCMessage) async throws -> XPCMessage {
let descriptionData = message.dataNoCopy(key: .imageDescription)
guard let descriptionData else {
throw ContainerizationError(
.invalidArgument,
message: "missing Image description"
)
}
let description = try JSONDecoder().decode(ImageDescription.self, from: descriptionData)
var platform: Platform?
if let platformData = message.dataNoCopy(key: .ociPlatform) {
platform = try JSONDecoder().decode(ContainerizationOCI.Platform.self, from: platformData)
}
let progressUpdateService = ProgressUpdateService(message: message)
try await self.service.unpack(description: description, platform: platform, progressUpdate: progressUpdateService?.handler)
let reply = message.reply()
return reply
}
@Sendable
public func deleteSnapshot(_ message: XPCMessage) async throws -> XPCMessage {
let descriptionData = message.dataNoCopy(key: .imageDescription)
guard let descriptionData else {
throw ContainerizationError(
.invalidArgument,
message: "missing image description"
)
}
let description = try JSONDecoder().decode(ImageDescription.self, from: descriptionData)
let platformData = message.dataNoCopy(key: .ociPlatform)
var platform: Platform?
if let platformData {
platform = try JSONDecoder().decode(ContainerizationOCI.Platform.self, from: platformData)
}
try await self.service.deleteImageSnapshot(description: description, platform: platform)
let reply = message.reply()
return reply
}
@Sendable
public func getSnapshot(_ message: XPCMessage) async throws -> XPCMessage {
let descriptionData = message.dataNoCopy(key: .imageDescription)
guard let descriptionData else {
throw ContainerizationError(
.invalidArgument,
message: "missing image description"
)
}
let description = try JSONDecoder().decode(ImageDescription.self, from: descriptionData)
let platformData = message.dataNoCopy(key: .ociPlatform)
guard let platformData else {
throw ContainerizationError(
.invalidArgument,
message: "missing OCI platform"
)
}
let platform = try JSONDecoder().decode(ContainerizationOCI.Platform.self, from: platformData)
let fs = try await self.service.getImageSnapshot(description: description, platform: platform)
let fsData = try JSONEncoder().encode(fs)
let reply = message.reply()
reply.set(key: .filesystem, value: fsData)
return reply
}
}