-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
BuildpackLayersMetadata.java
194 lines (158 loc) · 5.77 KB
/
BuildpackLayersMetadata.java
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
/*
* Copyright 2012-2022 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.build;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.boot.buildpack.platform.docker.type.Image;
import org.springframework.boot.buildpack.platform.docker.type.ImageConfig;
import org.springframework.boot.buildpack.platform.json.MappedObject;
import org.springframework.boot.buildpack.platform.json.SharedObjectMapper;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Buildpack layers metadata information.
*
* @author Scott Frederick
*/
final class BuildpackLayersMetadata extends MappedObject {
private static final String LABEL_NAME = "io.buildpacks.buildpack.layers";
private final Buildpacks buildpacks;
private BuildpackLayersMetadata(JsonNode node) {
super(node, MethodHandles.lookup());
this.buildpacks = Buildpacks.fromJson(getNode());
}
/**
* Return the metadata details of a buildpack with the given ID and version.
* @param id the buildpack ID
* @param version the buildpack version
* @return the buildpack details or {@code null} if a buildpack with the given ID and
* version does not exist in the metadata
*/
BuildpackLayerDetails getBuildpack(String id, String version) {
return this.buildpacks.getBuildpack(id, version);
}
/**
* Create a {@link BuildpackLayersMetadata} from an image.
* @param image the source image
* @return the buildpack layers metadata
* @throws IOException on IO error
*/
static BuildpackLayersMetadata fromImage(Image image) throws IOException {
Assert.notNull(image, "Image must not be null");
return fromImageConfig(image.getConfig());
}
/**
* Create a {@link BuildpackLayersMetadata} from image config.
* @param imageConfig the source image config
* @return the buildpack layers metadata
* @throws IOException on IO error
*/
static BuildpackLayersMetadata fromImageConfig(ImageConfig imageConfig) throws IOException {
Assert.notNull(imageConfig, "ImageConfig must not be null");
String json = imageConfig.getLabels().get(LABEL_NAME);
Assert.notNull(json, () -> "No '" + LABEL_NAME + "' label found in image config labels '"
+ StringUtils.collectionToCommaDelimitedString(imageConfig.getLabels().keySet()) + "'");
return fromJson(json);
}
/**
* Create a {@link BuildpackLayersMetadata} from JSON.
* @param json the source JSON
* @return the buildpack layers metadata
* @throws IOException on IO error
*/
static BuildpackLayersMetadata fromJson(String json) throws IOException {
return fromJson(SharedObjectMapper.get().readTree(json));
}
/**
* Create a {@link BuildpackLayersMetadata} from JSON.
* @param node the source JSON
* @return the buildpack layers metadata
*/
static BuildpackLayersMetadata fromJson(JsonNode node) {
return new BuildpackLayersMetadata(node);
}
private static class Buildpacks {
private final Map<String, BuildpackVersions> buildpacks = new HashMap<>();
private BuildpackLayerDetails getBuildpack(String id, String version) {
if (this.buildpacks.containsKey(id)) {
return this.buildpacks.get(id).getBuildpack(version);
}
return null;
}
private void addBuildpackVersions(String id, BuildpackVersions versions) {
this.buildpacks.put(id, versions);
}
private static Buildpacks fromJson(JsonNode node) {
Buildpacks buildpacks = new Buildpacks();
node.fields().forEachRemaining((field) -> buildpacks.addBuildpackVersions(field.getKey(),
BuildpackVersions.fromJson(field.getValue())));
return buildpacks;
}
}
private static class BuildpackVersions {
private final Map<String, BuildpackLayerDetails> versions = new HashMap<>();
private BuildpackLayerDetails getBuildpack(String version) {
return this.versions.get(version);
}
private void addBuildpackVersion(String version, BuildpackLayerDetails details) {
this.versions.put(version, details);
}
private static BuildpackVersions fromJson(JsonNode node) {
BuildpackVersions versions = new BuildpackVersions();
node.fields().forEachRemaining((field) -> versions.addBuildpackVersion(field.getKey(),
BuildpackLayerDetails.fromJson(field.getValue())));
return versions;
}
}
static final class BuildpackLayerDetails extends MappedObject {
private final String name;
private final String homepage;
private final String layerDiffId;
private BuildpackLayerDetails(JsonNode node) {
super(node, MethodHandles.lookup());
this.name = valueAt("/name", String.class);
this.homepage = valueAt("/homepage", String.class);
this.layerDiffId = valueAt("/layerDiffID", String.class);
}
/**
* Return the buildpack name.
* @return the name
*/
String getName() {
return this.name;
}
/**
* Return the buildpack homepage address.
* @return the homepage address
*/
String getHomepage() {
return this.homepage;
}
/**
* Return the buildpack layer {@code diffID}.
* @return the layer {@code diffID}
*/
String getLayerDiffId() {
return this.layerDiffId;
}
private static BuildpackLayerDetails fromJson(JsonNode node) {
return new BuildpackLayerDetails(node);
}
}
}