-
Notifications
You must be signed in to change notification settings - Fork 745
Expand file tree
/
Copy pathImagesServiceHarness.swift
More file actions
300 lines (275 loc) · 11.6 KB
/
ImagesServiceHarness.swift
File metadata and controls
300 lines (275 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//===----------------------------------------------------------------------===//
// Copyright © 2025-2026 Apple Inc. and the container project authors.
//
// 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 ContainerAPIClient
import ContainerImagesServiceClient
import ContainerResource
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 maxConcurrentDownloads = message.int64(key: .maxConcurrentDownloads)
let progressUpdateService = ProgressUpdateService(message: message)
let imageDescription = try await service.pull(
reference: ref, platform: platform, insecure: insecure, progressUpdate: progressUpdateService?.handler, maxConcurrentDownloads: Int(maxConcurrentDownloads))
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 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 allTags = message.bool(key: .allTags)
let progressUpdateService = ProgressUpdateService(message: message)
if allTags {
let repository = message.string(key: .imageRepository)
guard let repository else {
throw ContainerizationError(
.invalidArgument,
message: "missing image repository"
)
}
let maxConcurrentUploads = message.int64(key: .maxConcurrentUploads)
try await service.pushAllTags(
repositoryName: repository, platform: platform, insecure: insecure,
maxConcurrentUploads: Int(maxConcurrentUploads), progressUpdate: progressUpdateService?.handler)
} else {
let ref = message.string(key: .imageReference)
guard let ref else {
throw ContainerizationError(
.invalidArgument,
message: "missing image reference"
)
}
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)
let force = message.bool(key: .forceLoad)
guard let input else {
throw ContainerizationError(
.invalidArgument,
message: "missing input file path"
)
}
let (images, rejectedMembers) = try await service.load(
from: URL(filePath: input),
force: force
)
let reply = message.reply()
let imagesData = try JSONEncoder().encode(images)
reply.set(key: .imageDescriptions, value: imagesData)
let rejectedData = try JSONEncoder().encode(rejectedMembers)
reply.set(key: .rejectedMembers, value: rejectedData)
return reply
}
@Sendable
public func cleanUpOrphanedBlobs(_ message: XPCMessage) async throws -> XPCMessage {
let (deleted, size) = try await service.cleanUpOrphanedBlobs()
let reply = message.reply()
let data = try JSONEncoder().encode(deleted)
reply.set(key: .digests, value: data)
reply.set(key: .imageSize, value: size)
return reply
}
@Sendable
public func calculateDiskUsage(_ message: XPCMessage) async throws -> XPCMessage {
// Decode active image references from the message
let activeRefsData = message.dataNoCopy(key: .activeImageReferences)
let activeRefs: Set<String>
if let activeRefsData {
activeRefs = try JSONDecoder().decode(Set<String>.self, from: activeRefsData)
} else {
activeRefs = Set<String>()
}
let (total, active, size, reclaimable) = try await service.calculateDiskUsage(activeReferences: activeRefs)
let reply = message.reply()
reply.set(key: .totalCount, value: Int64(total))
reply.set(key: .activeCount, value: Int64(active))
reply.set(key: .imageSize, value: size)
reply.set(key: .reclaimableSize, value: reclaimable)
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
}
}