Skip to content

Commit 12c9bff

Browse files
committed
Merge branch '3.4.x'
Closes gh-44640
2 parents ef3eaf3 + d93f4f5 commit 12c9bff

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,14 @@ Image fetchImage(ImageType type, ImageReference reference) throws IOException {
237237
() -> String.format("%s '%s' must be pulled from the '%s' authenticated registry",
238238
StringUtils.capitalize(type.getDescription()), reference, this.domain));
239239
if (this.pullPolicy == PullPolicy.ALWAYS) {
240-
return pullImage(reference, type);
240+
return checkPlatformMismatch(pullImage(reference, type), reference);
241241
}
242242
try {
243-
return Builder.this.docker.image().inspect(reference);
243+
return checkPlatformMismatch(Builder.this.docker.image().inspect(reference), reference);
244244
}
245245
catch (DockerEngineException ex) {
246246
if (this.pullPolicy == PullPolicy.IF_NOT_PRESENT && ex.getStatusCode() == 404) {
247-
return pullImage(reference, type);
247+
return checkPlatformMismatch(pullImage(reference, type), reference);
248248
}
249249
throw ex;
250250
}
@@ -261,6 +261,26 @@ private Image pullImage(ImageReference reference, ImageType imageType) throws IO
261261
return image;
262262
}
263263

264+
private Image checkPlatformMismatch(Image image, ImageReference imageReference) {
265+
if (this.defaultPlatform != null) {
266+
ImagePlatform imagePlatform = ImagePlatform.from(image);
267+
if (!imagePlatform.equals(this.defaultPlatform)) {
268+
throw new PlatformMismatchException(imageReference, this.defaultPlatform, imagePlatform);
269+
}
270+
}
271+
return image;
272+
}
273+
274+
}
275+
276+
private static final class PlatformMismatchException extends RuntimeException {
277+
278+
private PlatformMismatchException(ImageReference imageReference, ImagePlatform requestedPlatform,
279+
ImagePlatform actualPlatform) {
280+
super("Image platform mismatch detected. The configured platform '%s' is not supported by the image '%s'. Requested platform '%s' but got '%s'"
281+
.formatted(requestedPlatform, imageReference, requestedPlatform, actualPlatform));
282+
}
283+
264284
}
265285

266286
/**

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/dockerTest/java/org/springframework/boot/gradle/tasks/bundling/BootBuildImageIntegrationTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,14 @@ void failsWhenCachesAreConfiguredTwice() throws IOException {
513513
assertThat(result.getOutput()).containsPattern("Each image building cache can be configured only once");
514514
}
515515

516+
@TestTemplate
517+
void failsWithIncompatiblePlatform() throws IOException {
518+
writeMainClass();
519+
BuildResult result = this.gradleBuild.buildAndFail("bootBuildImage");
520+
assertThat(result.getOutput()).contains(
521+
"Image platform mismatch detected. The configured platform 'invalid/platform' is not supported by the image 'ghcr.io/spring-io/spring-boot-cnb-test-builder:0.0.1'. Requested platform 'invalid/platform' but got 'linux/amd64'");
522+
}
523+
516524
private void writeMainClass() throws IOException {
517525
File examplePackage = new File(this.gradleBuild.getProjectDir(), "src/main/java/example");
518526
examplePackage.mkdirs();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
plugins {
2+
id 'java'
3+
id 'org.springframework.boot' version '{version}'
4+
}
5+
6+
bootBuildImage {
7+
builder = "ghcr.io/spring-io/spring-boot-cnb-test-builder:0.0.1"
8+
runImage = "paketobuildpacks/run-jammy-tiny"
9+
buildpacks = ["ghcr.io/spring-io/spring-boot-test-info:0.0.1"]
10+
imagePlatform = "invalid/platform"
11+
}

0 commit comments

Comments
 (0)