Skip to content

Commit 588d544

Browse files
Merge branch '3.2.x'
Closes gh-39348
2 parents f4cd903 + 5b76416 commit 588d544

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/DockerApi.java

+23-20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.BufferedReader;
2020
import java.io.ByteArrayInputStream;
21+
import java.io.FileInputStream;
2122
import java.io.IOException;
2223
import java.io.InputStream;
2324
import java.io.InputStreamReader;
@@ -30,9 +31,7 @@
3031
import java.util.Arrays;
3132
import java.util.Collection;
3233
import java.util.Collections;
33-
import java.util.HashMap;
3434
import java.util.List;
35-
import java.util.Map;
3635
import java.util.Objects;
3736
import java.util.stream.Collectors;
3837

@@ -293,29 +292,20 @@ public void exportLayerFiles(ImageReference reference, IOBiConsumer<String, Path
293292
Assert.notNull(exports, "Exports must not be null");
294293
URI saveUri = buildUrl("/images/" + reference + "/get");
295294
Response response = http().get(saveUri);
296-
ImageArchiveManifest manifest = null;
297-
Map<String, Path> layerFiles = new HashMap<>();
298-
try (TarArchiveInputStream tar = new TarArchiveInputStream(response.getContent())) {
295+
Path exportFile = copyToTemp(response.getContent());
296+
ImageArchiveManifest manifest = getManifest(reference, exportFile);
297+
try (TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(exportFile.toFile()))) {
299298
TarArchiveEntry entry = tar.getNextEntry();
300299
while (entry != null) {
301-
if (entry.getName().equals("manifest.json")) {
302-
manifest = readManifest(tar);
303-
}
304-
if (entry.getName().endsWith(".tar")) {
305-
layerFiles.put(entry.getName(), copyToTemp(tar));
300+
if (manifestContainsLayerEntry(manifest, entry.getName())) {
301+
Path layerFile = copyToTemp(tar);
302+
exports.accept(entry.getName(), layerFile);
303+
Files.delete(layerFile);
306304
}
307305
entry = tar.getNextEntry();
308306
}
309307
}
310-
Assert.notNull(manifest, "Manifest not found in image " + reference);
311-
for (Map.Entry<String, Path> entry : layerFiles.entrySet()) {
312-
String name = entry.getKey();
313-
Path path = entry.getValue();
314-
if (manifestContainsLayerEntry(manifest, name)) {
315-
exports.accept(name, path);
316-
}
317-
Files.delete(path);
318-
}
308+
Files.delete(exportFile);
319309
}
320310

321311
/**
@@ -355,13 +345,26 @@ public void tag(ImageReference sourceReference, ImageReference targetReference)
355345
http().post(uri).close();
356346
}
357347

348+
private ImageArchiveManifest getManifest(ImageReference reference, Path exportFile) throws IOException {
349+
try (TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(exportFile.toFile()))) {
350+
TarArchiveEntry entry = tar.getNextEntry();
351+
while (entry != null) {
352+
if (entry.getName().equals("manifest.json")) {
353+
return readManifest(tar);
354+
}
355+
entry = tar.getNextEntry();
356+
}
357+
}
358+
throw new IllegalArgumentException("Manifest not found in image " + reference);
359+
}
360+
358361
private ImageArchiveManifest readManifest(TarArchiveInputStream tar) throws IOException {
359362
String manifestContent = new BufferedReader(new InputStreamReader(tar, StandardCharsets.UTF_8)).lines()
360363
.collect(Collectors.joining());
361364
return ImageArchiveManifest.of(new ByteArrayInputStream(manifestContent.getBytes(StandardCharsets.UTF_8)));
362365
}
363366

364-
private Path copyToTemp(TarArchiveInputStream in) throws IOException {
367+
private Path copyToTemp(InputStream in) throws IOException {
365368
Path path = Files.createTempFile("create-builder-scratch-", null);
366369
try (OutputStream out = Files.newOutputStream(path)) {
367370
StreamUtils.copy(in, out);

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/build/ImageBuildpackTests.java

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.File;
2222
import java.io.FileOutputStream;
2323
import java.io.IOException;
24+
import java.nio.file.Files;
2425
import java.nio.file.Path;
2526
import java.util.ArrayList;
2627
import java.util.List;
@@ -189,6 +190,7 @@ private Object withMockLayers(InvocationOnMock invocation) {
189190
tarOut.finish();
190191
}
191192
consumer.accept("test", tarFile.toPath());
193+
Files.delete(tarFile.toPath());
192194
}
193195
catch (IOException ex) {
194196
fail("Error writing mock layers", ex);

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/DockerApiTests.java

+1
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ void exportLayersWithSymlinksExportsLayerTars() throws Exception {
382382
}
383383

384384
@Test
385+
@SuppressWarnings("removal")
385386
void exportLayerFilesDeletesTempFiles() throws Exception {
386387
ImageReference reference = ImageReference.of("gcr.io/paketo-buildpacks/builder:base");
387388
URI exportUri = new URI(IMAGES_URL + "/gcr.io/paketo-buildpacks/builder:base/get");

0 commit comments

Comments
 (0)