|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.buildpack.platform.build; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.lang.invoke.MethodHandles; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import com.fasterxml.jackson.databind.JsonNode; |
| 25 | + |
| 26 | +import org.springframework.boot.buildpack.platform.docker.type.Image; |
| 27 | +import org.springframework.boot.buildpack.platform.docker.type.ImageConfig; |
| 28 | +import org.springframework.boot.buildpack.platform.json.MappedObject; |
| 29 | +import org.springframework.boot.buildpack.platform.json.SharedObjectMapper; |
| 30 | +import org.springframework.util.Assert; |
| 31 | +import org.springframework.util.StringUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * Buildpack layers metadata information. |
| 35 | + * |
| 36 | + * @author Scott Frederick |
| 37 | + */ |
| 38 | +final class BuildpackLayersMetadata extends MappedObject { |
| 39 | + |
| 40 | + private static final String LABEL_NAME = "io.buildpacks.buildpack.layers"; |
| 41 | + |
| 42 | + private final Buildpacks buildpacks; |
| 43 | + |
| 44 | + private BuildpackLayersMetadata(JsonNode node) { |
| 45 | + super(node, MethodHandles.lookup()); |
| 46 | + this.buildpacks = Buildpacks.fromJson(getNode()); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Return the metadata details of a buildpack with the given ID and version. |
| 51 | + * @param id the buildpack ID |
| 52 | + * @param version the buildpack version |
| 53 | + * @return the buildpack details or {@code null} if a buildpack with the given ID and |
| 54 | + * version does not exist in the metadata |
| 55 | + */ |
| 56 | + BuildpackLayerDetails getBuildpack(String id, String version) { |
| 57 | + return this.buildpacks.getBuildpack(id, version); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Create a {@link BuildpackLayersMetadata} from an image. |
| 62 | + * @param image the source image |
| 63 | + * @return the buildpack layers metadata |
| 64 | + * @throws IOException on IO error |
| 65 | + */ |
| 66 | + static BuildpackLayersMetadata fromImage(Image image) throws IOException { |
| 67 | + Assert.notNull(image, "Image must not be null"); |
| 68 | + return fromImageConfig(image.getConfig()); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Create a {@link BuildpackLayersMetadata} from image config. |
| 73 | + * @param imageConfig the source image config |
| 74 | + * @return the buildpack layers metadata |
| 75 | + * @throws IOException on IO error |
| 76 | + */ |
| 77 | + static BuildpackLayersMetadata fromImageConfig(ImageConfig imageConfig) throws IOException { |
| 78 | + Assert.notNull(imageConfig, "ImageConfig must not be null"); |
| 79 | + String json = imageConfig.getLabels().get(LABEL_NAME); |
| 80 | + Assert.notNull(json, () -> "No '" + LABEL_NAME + "' label found in image config labels '" |
| 81 | + + StringUtils.collectionToCommaDelimitedString(imageConfig.getLabels().keySet()) + "'"); |
| 82 | + return fromJson(json); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Create a {@link BuildpackLayersMetadata} from JSON. |
| 87 | + * @param json the source JSON |
| 88 | + * @return the buildpack layers metadata |
| 89 | + * @throws IOException on IO error |
| 90 | + */ |
| 91 | + static BuildpackLayersMetadata fromJson(String json) throws IOException { |
| 92 | + return fromJson(SharedObjectMapper.get().readTree(json)); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Create a {@link BuildpackLayersMetadata} from JSON. |
| 97 | + * @param node the source JSON |
| 98 | + * @return the buildpack layers metadata |
| 99 | + */ |
| 100 | + static BuildpackLayersMetadata fromJson(JsonNode node) { |
| 101 | + return new BuildpackLayersMetadata(node); |
| 102 | + } |
| 103 | + |
| 104 | + private static class Buildpacks { |
| 105 | + |
| 106 | + private final Map<String, BuildpackVersions> buildpacks = new HashMap<>(); |
| 107 | + |
| 108 | + private BuildpackLayerDetails getBuildpack(String id, String version) { |
| 109 | + if (this.buildpacks.containsKey(id)) { |
| 110 | + return this.buildpacks.get(id).getBuildpack(version); |
| 111 | + } |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + private void addBuildpackVersions(String id, BuildpackVersions versions) { |
| 116 | + this.buildpacks.put(id, versions); |
| 117 | + } |
| 118 | + |
| 119 | + private static Buildpacks fromJson(JsonNode node) { |
| 120 | + Buildpacks buildpacks = new Buildpacks(); |
| 121 | + node.fields().forEachRemaining((field) -> buildpacks.addBuildpackVersions(field.getKey(), |
| 122 | + BuildpackVersions.fromJson(field.getValue()))); |
| 123 | + return buildpacks; |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + private static class BuildpackVersions { |
| 129 | + |
| 130 | + private final Map<String, BuildpackLayerDetails> versions = new HashMap<>(); |
| 131 | + |
| 132 | + private BuildpackLayerDetails getBuildpack(String version) { |
| 133 | + return this.versions.get(version); |
| 134 | + } |
| 135 | + |
| 136 | + private void addBuildpackVersion(String version, BuildpackLayerDetails details) { |
| 137 | + this.versions.put(version, details); |
| 138 | + } |
| 139 | + |
| 140 | + private static BuildpackVersions fromJson(JsonNode node) { |
| 141 | + BuildpackVersions versions = new BuildpackVersions(); |
| 142 | + node.fields().forEachRemaining((field) -> versions.addBuildpackVersion(field.getKey(), |
| 143 | + BuildpackLayerDetails.fromJson(field.getValue()))); |
| 144 | + return versions; |
| 145 | + } |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + static final class BuildpackLayerDetails extends MappedObject { |
| 150 | + |
| 151 | + private final String name; |
| 152 | + |
| 153 | + private final String homepage; |
| 154 | + |
| 155 | + private final String layerDiffId; |
| 156 | + |
| 157 | + private BuildpackLayerDetails(JsonNode node) { |
| 158 | + super(node, MethodHandles.lookup()); |
| 159 | + this.name = valueAt("/name", String.class); |
| 160 | + this.homepage = valueAt("/homepage", String.class); |
| 161 | + this.layerDiffId = valueAt("/layerDiffID", String.class); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Return the buildpack name. |
| 166 | + * @return the name |
| 167 | + */ |
| 168 | + String getName() { |
| 169 | + return this.name; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Return the buildpack homepage address. |
| 174 | + * @return the homepage address |
| 175 | + */ |
| 176 | + String getHomepage() { |
| 177 | + return this.homepage; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Return the buildpack layer {@code diffID}. |
| 182 | + * @return the layer {@code diffID} |
| 183 | + */ |
| 184 | + String getLayerDiffId() { |
| 185 | + return this.layerDiffId; |
| 186 | + } |
| 187 | + |
| 188 | + private static BuildpackLayerDetails fromJson(JsonNode node) { |
| 189 | + return new BuildpackLayerDetails(node); |
| 190 | + } |
| 191 | + |
| 192 | + } |
| 193 | + |
| 194 | +} |
0 commit comments