Skip to content

Commit 5817c84

Browse files
Allow image Created date to be configurable
A `createdDate` option on the Maven `spring-boot:build-image` goal and the Gradle `bootBuildImage` task can be used to set the `Created` metadata field on a generated OCI image to a specified date or to the current date. Closes gh-28798
1 parent cacc563 commit 5817c84

File tree

68 files changed

+542
-71
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+542
-71
lines changed

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/build/BuildRequest.java

+59-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
1717
package org.springframework.boot.buildpack.platform.build;
1818

1919
import java.io.File;
20+
import java.time.Instant;
21+
import java.time.format.DateTimeParseException;
2022
import java.util.Arrays;
2123
import java.util.Collections;
2224
import java.util.LinkedHashMap;
@@ -79,6 +81,8 @@ public class BuildRequest {
7981

8082
private final Cache launchCache;
8183

84+
private final Instant createdDate;
85+
8286
BuildRequest(ImageReference name, Function<Owner, TarArchive> applicationContent) {
8387
Assert.notNull(name, "Name must not be null");
8488
Assert.notNull(applicationContent, "ApplicationContent must not be null");
@@ -98,12 +102,14 @@ public class BuildRequest {
98102
this.tags = Collections.emptyList();
99103
this.buildCache = null;
100104
this.launchCache = null;
105+
this.createdDate = null;
101106
}
102107

103108
BuildRequest(ImageReference name, Function<Owner, TarArchive> applicationContent, ImageReference builder,
104109
ImageReference runImage, Creator creator, Map<String, String> env, boolean cleanCache,
105110
boolean verboseLogging, PullPolicy pullPolicy, boolean publish, List<BuildpackReference> buildpacks,
106-
List<Binding> bindings, String network, List<ImageReference> tags, Cache buildCache, Cache launchCache) {
111+
List<Binding> bindings, String network, List<ImageReference> tags, Cache buildCache, Cache launchCache,
112+
Instant createdDate) {
107113
this.name = name;
108114
this.applicationContent = applicationContent;
109115
this.builder = builder;
@@ -120,6 +126,7 @@ public class BuildRequest {
120126
this.tags = tags;
121127
this.buildCache = buildCache;
122128
this.launchCache = launchCache;
129+
this.createdDate = createdDate;
123130
}
124131

125132
/**
@@ -131,7 +138,8 @@ public BuildRequest withBuilder(ImageReference builder) {
131138
Assert.notNull(builder, "Builder must not be null");
132139
return new BuildRequest(this.name, this.applicationContent, builder.inTaggedOrDigestForm(), this.runImage,
133140
this.creator, this.env, this.cleanCache, this.verboseLogging, this.pullPolicy, this.publish,
134-
this.buildpacks, this.bindings, this.network, this.tags, this.buildCache, this.launchCache);
141+
this.buildpacks, this.bindings, this.network, this.tags, this.buildCache, this.launchCache,
142+
this.createdDate);
135143
}
136144

137145
/**
@@ -142,7 +150,8 @@ public BuildRequest withBuilder(ImageReference builder) {
142150
public BuildRequest withRunImage(ImageReference runImageName) {
143151
return new BuildRequest(this.name, this.applicationContent, this.builder, runImageName.inTaggedOrDigestForm(),
144152
this.creator, this.env, this.cleanCache, this.verboseLogging, this.pullPolicy, this.publish,
145-
this.buildpacks, this.bindings, this.network, this.tags, this.buildCache, this.launchCache);
153+
this.buildpacks, this.bindings, this.network, this.tags, this.buildCache, this.launchCache,
154+
this.createdDate);
146155
}
147156

148157
/**
@@ -154,7 +163,7 @@ public BuildRequest withCreator(Creator creator) {
154163
Assert.notNull(creator, "Creator must not be null");
155164
return new BuildRequest(this.name, this.applicationContent, this.builder, this.runImage, creator, this.env,
156165
this.cleanCache, this.verboseLogging, this.pullPolicy, this.publish, this.buildpacks, this.bindings,
157-
this.network, this.tags, this.buildCache, this.launchCache);
166+
this.network, this.tags, this.buildCache, this.launchCache, this.createdDate);
158167
}
159168

160169
/**
@@ -170,7 +179,8 @@ public BuildRequest withEnv(String name, String value) {
170179
env.put(name, value);
171180
return new BuildRequest(this.name, this.applicationContent, this.builder, this.runImage, this.creator,
172181
Collections.unmodifiableMap(env), this.cleanCache, this.verboseLogging, this.pullPolicy, this.publish,
173-
this.buildpacks, this.bindings, this.network, this.tags, this.buildCache, this.launchCache);
182+
this.buildpacks, this.bindings, this.network, this.tags, this.buildCache, this.launchCache,
183+
this.createdDate);
174184
}
175185

176186
/**
@@ -185,7 +195,7 @@ public BuildRequest withEnv(Map<String, String> env) {
185195
return new BuildRequest(this.name, this.applicationContent, this.builder, this.runImage, this.creator,
186196
Collections.unmodifiableMap(updatedEnv), this.cleanCache, this.verboseLogging, this.pullPolicy,
187197
this.publish, this.buildpacks, this.bindings, this.network, this.tags, this.buildCache,
188-
this.launchCache);
198+
this.launchCache, this.createdDate);
189199
}
190200

191201
/**
@@ -196,7 +206,7 @@ public BuildRequest withEnv(Map<String, String> env) {
196206
public BuildRequest withCleanCache(boolean cleanCache) {
197207
return new BuildRequest(this.name, this.applicationContent, this.builder, this.runImage, this.creator, this.env,
198208
cleanCache, this.verboseLogging, this.pullPolicy, this.publish, this.buildpacks, this.bindings,
199-
this.network, this.tags, this.buildCache, this.launchCache);
209+
this.network, this.tags, this.buildCache, this.launchCache, this.createdDate);
200210
}
201211

202212
/**
@@ -207,7 +217,7 @@ public BuildRequest withCleanCache(boolean cleanCache) {
207217
public BuildRequest withVerboseLogging(boolean verboseLogging) {
208218
return new BuildRequest(this.name, this.applicationContent, this.builder, this.runImage, this.creator, this.env,
209219
this.cleanCache, verboseLogging, this.pullPolicy, this.publish, this.buildpacks, this.bindings,
210-
this.network, this.tags, this.buildCache, this.launchCache);
220+
this.network, this.tags, this.buildCache, this.launchCache, this.createdDate);
211221
}
212222

213223
/**
@@ -218,7 +228,7 @@ public BuildRequest withVerboseLogging(boolean verboseLogging) {
218228
public BuildRequest withPullPolicy(PullPolicy pullPolicy) {
219229
return new BuildRequest(this.name, this.applicationContent, this.builder, this.runImage, this.creator, this.env,
220230
this.cleanCache, this.verboseLogging, pullPolicy, this.publish, this.buildpacks, this.bindings,
221-
this.network, this.tags, this.buildCache, this.launchCache);
231+
this.network, this.tags, this.buildCache, this.launchCache, this.createdDate);
222232
}
223233

224234
/**
@@ -229,7 +239,7 @@ public BuildRequest withPullPolicy(PullPolicy pullPolicy) {
229239
public BuildRequest withPublish(boolean publish) {
230240
return new BuildRequest(this.name, this.applicationContent, this.builder, this.runImage, this.creator, this.env,
231241
this.cleanCache, this.verboseLogging, this.pullPolicy, publish, this.buildpacks, this.bindings,
232-
this.network, this.tags, this.buildCache, this.launchCache);
242+
this.network, this.tags, this.buildCache, this.launchCache, this.createdDate);
233243
}
234244

235245
/**
@@ -253,7 +263,7 @@ public BuildRequest withBuildpacks(List<BuildpackReference> buildpacks) {
253263
Assert.notNull(buildpacks, "Buildpacks must not be null");
254264
return new BuildRequest(this.name, this.applicationContent, this.builder, this.runImage, this.creator, this.env,
255265
this.cleanCache, this.verboseLogging, this.pullPolicy, this.publish, buildpacks, this.bindings,
256-
this.network, this.tags, this.buildCache, this.launchCache);
266+
this.network, this.tags, this.buildCache, this.launchCache, this.createdDate);
257267
}
258268

259269
/**
@@ -277,7 +287,7 @@ public BuildRequest withBindings(List<Binding> bindings) {
277287
Assert.notNull(bindings, "Bindings must not be null");
278288
return new BuildRequest(this.name, this.applicationContent, this.builder, this.runImage, this.creator, this.env,
279289
this.cleanCache, this.verboseLogging, this.pullPolicy, this.publish, this.buildpacks, bindings,
280-
this.network, this.tags, this.buildCache, this.launchCache);
290+
this.network, this.tags, this.buildCache, this.launchCache, this.createdDate);
281291
}
282292

283293
/**
@@ -289,7 +299,7 @@ public BuildRequest withBindings(List<Binding> bindings) {
289299
public BuildRequest withNetwork(String network) {
290300
return new BuildRequest(this.name, this.applicationContent, this.builder, this.runImage, this.creator, this.env,
291301
this.cleanCache, this.verboseLogging, this.pullPolicy, this.publish, this.buildpacks, this.bindings,
292-
network, this.tags, this.buildCache, this.launchCache);
302+
network, this.tags, this.buildCache, this.launchCache, this.createdDate);
293303
}
294304

295305
/**
@@ -311,7 +321,7 @@ public BuildRequest withTags(List<ImageReference> tags) {
311321
Assert.notNull(tags, "Tags must not be null");
312322
return new BuildRequest(this.name, this.applicationContent, this.builder, this.runImage, this.creator, this.env,
313323
this.cleanCache, this.verboseLogging, this.pullPolicy, this.publish, this.buildpacks, this.bindings,
314-
this.network, tags, this.buildCache, this.launchCache);
324+
this.network, tags, this.buildCache, this.launchCache, this.createdDate);
315325
}
316326

317327
/**
@@ -323,7 +333,7 @@ public BuildRequest withBuildCache(Cache buildCache) {
323333
Assert.notNull(buildCache, "BuildCache must not be null");
324334
return new BuildRequest(this.name, this.applicationContent, this.builder, this.runImage, this.creator, this.env,
325335
this.cleanCache, this.verboseLogging, this.pullPolicy, this.publish, this.buildpacks, this.bindings,
326-
this.network, this.tags, buildCache, this.launchCache);
336+
this.network, this.tags, buildCache, this.launchCache, this.createdDate);
327337
}
328338

329339
/**
@@ -335,7 +345,31 @@ public BuildRequest withLaunchCache(Cache launchCache) {
335345
Assert.notNull(launchCache, "LaunchCache must not be null");
336346
return new BuildRequest(this.name, this.applicationContent, this.builder, this.runImage, this.creator, this.env,
337347
this.cleanCache, this.verboseLogging, this.pullPolicy, this.publish, this.buildpacks, this.bindings,
338-
this.network, this.tags, this.buildCache, launchCache);
348+
this.network, this.tags, this.buildCache, launchCache, this.createdDate);
349+
}
350+
351+
/**
352+
* Return a new {@link BuildRequest} with an updated created date.
353+
* @param createdDate the created date
354+
* @return an updated build request
355+
*/
356+
public BuildRequest withCreatedDate(String createdDate) {
357+
Assert.notNull(createdDate, "CreatedDate must not be null");
358+
return new BuildRequest(this.name, this.applicationContent, this.builder, this.runImage, this.creator, this.env,
359+
this.cleanCache, this.verboseLogging, this.pullPolicy, this.publish, this.buildpacks, this.bindings,
360+
this.network, this.tags, this.buildCache, this.launchCache, parseCreatedDate(createdDate));
361+
}
362+
363+
private Instant parseCreatedDate(String createdDate) {
364+
if ("now".equalsIgnoreCase(createdDate)) {
365+
return Instant.now();
366+
}
367+
try {
368+
return Instant.parse(createdDate);
369+
}
370+
catch (DateTimeParseException ex) {
371+
throw new IllegalArgumentException("Error parsing '" + createdDate + "' as an image created date", ex);
372+
}
339373
}
340374

341375
/**
@@ -471,6 +505,14 @@ public Cache getLaunchCache() {
471505
return this.launchCache;
472506
}
473507

508+
/**
509+
* Return the custom created date that should be used by the lifecycle.
510+
* @return the created date
511+
*/
512+
public Instant getCreatedDate() {
513+
return this.createdDate;
514+
}
515+
474516
/**
475517
* Factory method to create a new {@link BuildRequest} from a JAR file.
476518
* @param jarFile the source jar file

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/build/Lifecycle.java

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class Lifecycle implements Closeable {
5050

5151
private static final String PLATFORM_API_VERSION_KEY = "CNB_PLATFORM_API";
5252

53+
private static final String SOURCE_DATE_EPOCH_KEY = "SOURCE_DATE_EPOCH";
54+
5355
private static final String DOMAIN_SOCKET_PATH = "/var/run/docker.sock";
5456

5557
private final BuildLog log;
@@ -184,6 +186,9 @@ private Phase createPhase() {
184186
if (this.request.getNetwork() != null) {
185187
phase.withNetworkMode(this.request.getNetwork());
186188
}
189+
if (this.request.getCreatedDate() != null) {
190+
phase.withEnv(SOURCE_DATE_EPOCH_KEY, Long.toString(this.request.getCreatedDate().getEpochSecond()));
191+
}
187192
return phase;
188193
}
189194

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,12 +44,15 @@ public class Image extends MappedObject {
4444

4545
private final String os;
4646

47+
private final String created;
48+
4749
Image(JsonNode node) {
4850
super(node, MethodHandles.lookup());
4951
this.digests = getDigests(getNode().at("/RepoDigests"));
5052
this.config = new ImageConfig(getNode().at("/Config"));
5153
this.layers = extractLayers(valueAt("/RootFS/Layers", String[].class));
5254
this.os = valueAt("/Os", String.class);
55+
this.created = valueAt("/Created", String.class);
5356
}
5457

5558
private List<String> getDigests(JsonNode node) {
@@ -100,6 +103,14 @@ public String getOs() {
100103
return (this.os != null) ? this.os : "linux";
101104
}
102105

106+
/**
107+
* Return the created date of the image.
108+
* @return the image created date
109+
*/
110+
public String getCreated() {
111+
return this.created;
112+
}
113+
103114
/**
104115
* Create a new {@link Image} instance from the specified JSON content.
105116
* @param content the JSON content

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

+34
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import java.io.File;
2222
import java.io.IOException;
2323
import java.nio.charset.StandardCharsets;
24+
import java.time.Instant;
25+
import java.time.OffsetDateTime;
26+
import java.time.ZoneId;
2427
import java.util.LinkedHashMap;
2528
import java.util.List;
2629
import java.util.Map;
@@ -258,6 +261,37 @@ void withLaunchVolumeCacheWhenCacheIsNullThrowsException() throws IOException {
258261
.withMessage("LaunchCache must not be null");
259262
}
260263

264+
@Test
265+
void withCreatedDateSetsCreatedDate() throws Exception {
266+
Instant createDate = Instant.now();
267+
BuildRequest request = BuildRequest.forJarFile(writeTestJarFile("my-app-0.0.1.jar"));
268+
BuildRequest withCreatedDate = request.withCreatedDate(createDate.toString());
269+
assertThat(withCreatedDate.getCreatedDate()).isEqualTo(createDate);
270+
}
271+
272+
@Test
273+
void withCreatedDateNowSetsCreatedDate() throws Exception {
274+
OffsetDateTime now = OffsetDateTime.now();
275+
BuildRequest request = BuildRequest.forJarFile(writeTestJarFile("my-app-0.0.1.jar"));
276+
BuildRequest withCreatedDate = request.withCreatedDate("now");
277+
OffsetDateTime createdDate = OffsetDateTime.ofInstant(withCreatedDate.getCreatedDate(), ZoneId.of("UTC"));
278+
assertThat(createdDate.getYear()).isEqualTo(now.getYear());
279+
assertThat(createdDate.getMonth()).isEqualTo(now.getMonth());
280+
assertThat(createdDate.getDayOfMonth()).isEqualTo(now.getDayOfMonth());
281+
withCreatedDate = request.withCreatedDate("NOW");
282+
createdDate = OffsetDateTime.ofInstant(withCreatedDate.getCreatedDate(), ZoneId.of("UTC"));
283+
assertThat(createdDate.getYear()).isEqualTo(now.getYear());
284+
assertThat(createdDate.getMonth()).isEqualTo(now.getMonth());
285+
assertThat(createdDate.getDayOfMonth()).isEqualTo(now.getDayOfMonth());
286+
}
287+
288+
@Test
289+
void withCreatedDateAndInvalidDateThrowsException() throws Exception {
290+
BuildRequest request = BuildRequest.forJarFile(writeTestJarFile("my-app-0.0.1.jar"));
291+
assertThatIllegalArgumentException().isThrownBy(() -> request.withCreatedDate("not a date"))
292+
.withMessageContaining("'not a date'");
293+
}
294+
261295
private void hasExpectedJarContent(TarArchive archive) {
262296
try {
263297
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void getArchiveHasTag() throws Exception {
116116
}
117117

118118
@Test
119-
void getArchiveHasFixedCreateDate() throws Exception {
119+
void getArchiveHasFixedCreatedDate() throws Exception {
120120
EphemeralBuilder builder = new EphemeralBuilder(this.owner, this.image, this.targetImage, this.metadata,
121121
this.creator, this.env, this.buildpacks);
122122
Instant createInstant = builder.getArchive().getCreateDate();

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

+11
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,17 @@ void executeWithCacheVolumeNamesExecutesPhases() throws Exception {
218218
assertThat(this.out.toString()).contains("Successfully built image 'docker.io/library/my-application:latest'");
219219
}
220220

221+
@Test
222+
void executeWithCreatedDateExecutesPhases() throws Exception {
223+
given(this.docker.container().create(any())).willAnswer(answerWithGeneratedContainerId());
224+
given(this.docker.container().create(any(), any())).willAnswer(answerWithGeneratedContainerId());
225+
given(this.docker.container().wait(any())).willReturn(ContainerStatus.of(0, null));
226+
BuildRequest request = getTestRequest().withCreatedDate("2020-07-01T12:34:56Z");
227+
createLifecycle(request).execute();
228+
assertPhaseWasRun("creator", withExpectedConfig("lifecycle-creator-created-date.json"));
229+
assertThat(this.out.toString()).contains("Successfully built image 'docker.io/library/my-application:latest'");
230+
}
231+
221232
@Test
222233
void executeWithDockerHostAndRemoteAddressExecutesPhases() throws Exception {
223234
given(this.docker.container().create(any())).willAnswer(answerWithGeneratedContainerId());

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

+6
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ void getOsReturnsOs() throws Exception {
6767
assertThat(image.getOs()).isEqualTo("linux");
6868
}
6969

70+
@Test
71+
void getCreatedReturnsDate() throws Exception {
72+
Image image = getImage();
73+
assertThat(image.getCreated()).isEqualTo("2019-10-30T19:34:56.296666503Z");
74+
}
75+
7076
private Image getImage() throws IOException {
7177
return Image.of(getContent("image.json"));
7278
}

0 commit comments

Comments
 (0)