mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Merge branch '3.1.x' into 3.2.x
Closes gh-39347
This commit is contained in:
commit
5b76416b06
@ -18,6 +18,7 @@ package org.springframework.boot.buildpack.platform.docker;
|
|||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
@ -30,9 +31,7 @@ import java.nio.file.Path;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -293,29 +292,20 @@ public class DockerApi {
|
|||||||
Assert.notNull(exports, "Exports must not be null");
|
Assert.notNull(exports, "Exports must not be null");
|
||||||
URI saveUri = buildUrl("/images/" + reference + "/get");
|
URI saveUri = buildUrl("/images/" + reference + "/get");
|
||||||
Response response = http().get(saveUri);
|
Response response = http().get(saveUri);
|
||||||
ImageArchiveManifest manifest = null;
|
Path exportFile = copyToTemp(response.getContent());
|
||||||
Map<String, Path> layerFiles = new HashMap<>();
|
ImageArchiveManifest manifest = getManifest(reference, exportFile);
|
||||||
try (TarArchiveInputStream tar = new TarArchiveInputStream(response.getContent())) {
|
try (TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(exportFile.toFile()))) {
|
||||||
TarArchiveEntry entry = tar.getNextEntry();
|
TarArchiveEntry entry = tar.getNextEntry();
|
||||||
while (entry != null) {
|
while (entry != null) {
|
||||||
if (entry.getName().equals("manifest.json")) {
|
if (manifestContainsLayerEntry(manifest, entry.getName())) {
|
||||||
manifest = readManifest(tar);
|
Path layerFile = copyToTemp(tar);
|
||||||
}
|
exports.accept(entry.getName(), layerFile);
|
||||||
if (entry.getName().endsWith(".tar")) {
|
Files.delete(layerFile);
|
||||||
layerFiles.put(entry.getName(), copyToTemp(tar));
|
|
||||||
}
|
}
|
||||||
entry = tar.getNextEntry();
|
entry = tar.getNextEntry();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Assert.notNull(manifest, "Manifest not found in image " + reference);
|
Files.delete(exportFile);
|
||||||
for (Map.Entry<String, Path> entry : layerFiles.entrySet()) {
|
|
||||||
String name = entry.getKey();
|
|
||||||
Path path = entry.getValue();
|
|
||||||
if (manifestContainsLayerEntry(manifest, name)) {
|
|
||||||
exports.accept(name, path);
|
|
||||||
}
|
|
||||||
Files.delete(path);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -355,13 +345,26 @@ public class DockerApi {
|
|||||||
http().post(uri).close();
|
http().post(uri).close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ImageArchiveManifest getManifest(ImageReference reference, Path exportFile) throws IOException {
|
||||||
|
try (TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(exportFile.toFile()))) {
|
||||||
|
TarArchiveEntry entry = tar.getNextEntry();
|
||||||
|
while (entry != null) {
|
||||||
|
if (entry.getName().equals("manifest.json")) {
|
||||||
|
return readManifest(tar);
|
||||||
|
}
|
||||||
|
entry = tar.getNextEntry();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException("Manifest not found in image " + reference);
|
||||||
|
}
|
||||||
|
|
||||||
private ImageArchiveManifest readManifest(TarArchiveInputStream tar) throws IOException {
|
private ImageArchiveManifest readManifest(TarArchiveInputStream tar) throws IOException {
|
||||||
String manifestContent = new BufferedReader(new InputStreamReader(tar, StandardCharsets.UTF_8)).lines()
|
String manifestContent = new BufferedReader(new InputStreamReader(tar, StandardCharsets.UTF_8)).lines()
|
||||||
.collect(Collectors.joining());
|
.collect(Collectors.joining());
|
||||||
return ImageArchiveManifest.of(new ByteArrayInputStream(manifestContent.getBytes(StandardCharsets.UTF_8)));
|
return ImageArchiveManifest.of(new ByteArrayInputStream(manifestContent.getBytes(StandardCharsets.UTF_8)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Path copyToTemp(TarArchiveInputStream in) throws IOException {
|
private Path copyToTemp(InputStream in) throws IOException {
|
||||||
Path path = Files.createTempFile("create-builder-scratch-", null);
|
Path path = Files.createTempFile("create-builder-scratch-", null);
|
||||||
try (OutputStream out = Files.newOutputStream(path)) {
|
try (OutputStream out = Files.newOutputStream(path)) {
|
||||||
StreamUtils.copy(in, out);
|
StreamUtils.copy(in, out);
|
||||||
|
@ -21,6 +21,7 @@ import java.io.ByteArrayOutputStream;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -189,6 +190,7 @@ class ImageBuildpackTests extends AbstractJsonTests {
|
|||||||
tarOut.finish();
|
tarOut.finish();
|
||||||
}
|
}
|
||||||
consumer.accept("test", tarFile.toPath());
|
consumer.accept("test", tarFile.toPath());
|
||||||
|
Files.delete(tarFile.toPath());
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
catch (IOException ex) {
|
||||||
fail("Error writing mock layers", ex);
|
fail("Error writing mock layers", ex);
|
||||||
|
@ -382,6 +382,7 @@ class DockerApiTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@SuppressWarnings("removal")
|
||||||
void exportLayerFilesDeletesTempFiles() throws Exception {
|
void exportLayerFilesDeletesTempFiles() throws Exception {
|
||||||
ImageReference reference = ImageReference.of("gcr.io/paketo-buildpacks/builder:base");
|
ImageReference reference = ImageReference.of("gcr.io/paketo-buildpacks/builder:base");
|
||||||
URI exportUri = new URI(IMAGES_URL + "/gcr.io/paketo-buildpacks/builder:base/get");
|
URI exportUri = new URI(IMAGES_URL + "/gcr.io/paketo-buildpacks/builder:base/get");
|
||||||
|
Loading…
Reference in New Issue
Block a user