Skip to content

Commit

Permalink
Hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
philwebb committed Apr 23, 2024
1 parent ee6677b commit a4f59b4
Show file tree
Hide file tree
Showing 11 changed files with 426 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -293,6 +292,7 @@ public void exportLayerFiles(ImageReference reference, IOBiConsumer<String, Path
URI saveUri = buildUrl("/images/" + reference + "/get");
Response response = http().get(saveUri);
Path exportFile = copyToTemp(response.getContent());

ImageArchiveManifest manifest = getManifest(reference, exportFile);
try (TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(exportFile.toFile()))) {
TarArchiveEntry entry = tar.getNextTarEntry();
Expand Down Expand Up @@ -366,9 +366,7 @@ private ImageArchiveManifest readManifest(TarArchiveInputStream tar) throws IOEx

private Path copyToTemp(InputStream in) throws IOException {
Path path = Files.createTempFile("create-builder-scratch-", null);
try (OutputStream out = Files.newOutputStream(path)) {
StreamUtils.copy(in, out);
}
Files.copy(in, path);
return path;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright 2012-2024 the original author or 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.
*/

package org.springframework.boot.buildpack.platform.docker.type;

import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;

import com.fasterxml.jackson.databind.JsonNode;

import org.springframework.boot.buildpack.platform.json.MappedObject;

/**
* A distribution manifest list as defined in
* {@code application/vnd.docker.distribution.manifest.list} files.
*
* @author Phillip Webb
* @since 3.1.12
* @see <a href="https://github.com/opencontainers/image-spec/blob/main/manifest.md">OCI
* Image Manifest Specification</a>
*/
public class DistributionManifestList extends MappedObject {

private final Integer schemaVersion;

private final String mediaType;

private final List<Manifest> manifests;

private final List<Layer> layers;

protected DistributionManifestList(JsonNode node) {
super(node, MethodHandles.lookup());
this.schemaVersion = valueAt("/schemaVersion", Integer.class);
this.mediaType = valueAt("/mediaType", String.class);
this.manifests = loadChildren(getNode().at("/manifests"), Manifest::new);
this.layers = loadChildren(getNode().at("/layers"), Layer::new);
}

private static <T> List<T> loadChildren(JsonNode node, Function<JsonNode, T> factory) {
if (node.isEmpty()) {
return Collections.emptyList();
}
List<T> children = new ArrayList<>();
node.elements().forEachRemaining((childNode) -> children.add(factory.apply(childNode)));
return Collections.unmodifiableList(children);
}

public Integer getSchemaVersion() {
return this.schemaVersion;
}

public String getMediaType() {
return this.mediaType;
}

public List<Manifest> getManifests() {
return this.manifests;
}

public List<Layer> getLayers() {
return this.layers;
}

public static class Manifest extends MappedObject {

private final String mediaType;

private final String digest;

protected Manifest(JsonNode node) {
super(node, MethodHandles.lookup());
this.mediaType = valueAt("/mediaType", String.class);
this.digest = valueAt("/digest", String.class);
}

public String getMediaType() {
return this.mediaType;
}

public String getDigest() {
return this.digest;
}

}

public static class Layer extends MappedObject {

private final String mediaType;

private final String digest;

protected Layer(JsonNode node) {
super(node, MethodHandles.lookup());
this.mediaType = valueAt("/mediaType", String.class);
this.digest = valueAt("/digest", String.class);
}

public String getMediaType() {
return this.mediaType;
}

public String getDigest() {
return this.digest;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2012-2024 the original author or 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.
*/

package org.springframework.boot.buildpack.platform.docker.type;

import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.fasterxml.jackson.databind.JsonNode;

import org.springframework.boot.buildpack.platform.json.MappedObject;

/**
* Image archive index information as provided by {@code index.json}.
*
* @author Phillip Webb
* @since 3.1.12
* @see <a href=
* "https://github.com/opencontainers/image-spec/blob/main/image-index.md">OCI Image Index
* Specification</a>
*/
public class ImageArchiveIndex extends MappedObject {

private final Integer schemaVersion;

private final List<Manifest> manifests;

protected ImageArchiveIndex(JsonNode node) {
super(node, MethodHandles.lookup());
this.schemaVersion = valueAt("/schemaVersion", Integer.class);
this.manifests = loadManifests(getNode().at("/manifests"));
}

private static List<Manifest> loadManifests(JsonNode node) {
if (node.isEmpty()) {
return Collections.emptyList();
}
List<Manifest> manifests = new ArrayList<>();
node.elements().forEachRemaining((manifestNode) -> manifests.add(new Manifest(manifestNode)));
return Collections.unmodifiableList(manifests);
}

public Integer getSchemaVersion() {
return this.schemaVersion;
}

public List<Manifest> getManifests() {
return this.manifests;
}

public static class Manifest extends MappedObject {

private final String mediaType;

private final String digest;

protected Manifest(JsonNode node) {
super(node, MethodHandles.lookup());
this.mediaType = valueAt("/mediaType", String.class);
this.digest = valueAt("/digest", String.class);
}

public String getMediaType() {
return this.mediaType;
}

public String getDigest() {
return this.digest;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.springframework.boot.buildpack.platform.json.MappedObject;

/**
* Image archive manifest information.
* Image archive manifest information as provided by {@code manifest.json}.
*
* @author Scott Frederick
* @since 2.7.10
Expand All @@ -39,7 +39,11 @@ public class ImageArchiveManifest extends MappedObject {

protected ImageArchiveManifest(JsonNode node) {
super(node, MethodHandles.lookup());
getNode().elements().forEachRemaining((element) -> this.entries.add(ManifestEntry.of(element)));
getNode().elements().forEachRemaining(this::addEntry);
}

private boolean addEntry(JsonNode node) {
return this.entries.add(ManifestEntry.of(node));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2012-2024 the original author or 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.
*/

package org.springframework.boot.buildpack.platform.docker;

import java.io.IOException;
import java.nio.file.Files;

import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.junit.jupiter.api.Test;

import org.springframework.boot.buildpack.platform.docker.type.ImageReference;

/**
* @author Phillip Webb
*/
class Gh40100 {

@Test
void testName() throws IOException {
DockerApi api = new DockerApi();
ImageReference reference = ImageReference.of("gcr.io/paketo-buildpacks/adoptium:latest");
api.image().pull(reference, UpdateListener.none());
api.image().exportLayerFiles(reference, (name, path) -> {
System.out.println(name);
System.out.println(path);
TarArchiveInputStream tarIn = new TarArchiveInputStream(Files.newInputStream(path));
tarIn.getNextEntry();
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2012-2024 the original author or 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.
*/

package org.springframework.boot.buildpack.platform.docker.type;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import org.springframework.boot.buildpack.platform.json.AbstractJsonTests;

/**
* Tests for {@link DistributionManifestList}.
*
* @author Phillip Webb
*/
class DistributionManifestListTests extends AbstractJsonTests {

@Test
void loadJsonWhenHasManifests() throws IOException {
String content = getContentAsString("distribution-manifest-list-with-manifests.json");
DistributionManifestList manifestList = getManifestList(content);
}

@Test
void loadJsonWhenHasLayers() throws IOException {
String content = getContentAsString("distribution-manifest-list-with-layers.json");
DistributionManifestList manifestList = getManifestList(content);
}

private DistributionManifestList getManifestList(String content) throws IOException {
return new DistributionManifestList(getObjectMapper().readTree(content));
}

}

0 comments on commit a4f59b4

Please sign in to comment.