Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ public void sensitiveTargetBindingDetected(Binding binding) {
}

@Override
public void stackIdsDoNotMatch(String runImageStackId, String builderImageStackId) {
log("Warning: Run image stack '%s' does not match builder stack '%s'. Stack IDs are deprecated, but the images may not be compatible."
.formatted(runImageStackId, builderImageStackId));
public void distrosDoNotMatch(String runImageDistro, String builderImageDistro) {
log("Warning: Run image distribution '%s' does not match builder distribution '%s'. The images may not be compatible."
.formatted(runImageDistro, builderImageDistro));
log();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ Consumer<TotalProgressEvent> pullingImage(ImageReference imageReference, @Nullab
void sensitiveTargetBindingDetected(Binding binding);

/**
* Log that the stack ID of the run image does not match the stack ID of the builder
* image.
* @param runImageStackId the stack ID of the run image
* @param builderImageStackId the stack ID of the builder image
* Log that the OS distribution of the run image does not match the OS distribution of
* the builder image.
* @param runImageDistro the OS distribution of the run image
* @param builderImageDistro the OS distribution of the builder image
* @since 4.0.9
*/
default void stackIdsDoNotMatch(String runImageStackId, String builderImageStackId) {
default void distrosDoNotMatch(String runImageDistro, String builderImageDistro) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void build(BuildRequest request) throws DockerEngineException, IOExceptio
request = request.withRunImage(request.getRunImage().withDigest(runImage.getPrimaryDigest()));
runImage = imageFetcher.fetchImage(ImageType.RUNNER, request.getRunImage(), platform);
}
warnIfStackIdsDoNotMatch(runImage, builderImage);
warnIfDistrosDoNotMatch(runImage, builderImage);
BuildOwner buildOwner = BuildOwner.fromEnv(builderImage.getConfig().getEnv());
BuildpackLayersMetadata buildpackLayersMetadata = BuildpackLayersMetadata.fromImage(builderImage);
Buildpacks buildpacks = getBuildpacks(request, imageFetcher, platform, builderMetadata,
Expand Down Expand Up @@ -159,11 +159,11 @@ private ImageReference getRunImageReference(BuilderMetadata metadata) {
return ImageReference.of(runImageName).inTaggedOrDigestForm();
}

private void warnIfStackIdsDoNotMatch(Image runImage, Image builderImage) {
StackId runImageStackId = StackId.fromImage(runImage);
StackId builderImageStackId = StackId.fromImage(builderImage);
if (runImageStackId.hasId() && builderImageStackId.hasId() && !runImageStackId.equals(builderImageStackId)) {
this.log.stackIdsDoNotMatch(runImageStackId.toString(), builderImageStackId.toString());
private void warnIfDistrosDoNotMatch(Image runImage, Image builderImage) {
Distro runImageDistro = Distro.fromImage(runImage);
Distro builderImageDistro = Distro.fromImage(builderImage);
if (!runImageDistro.matches(builderImageDistro)) {
this.log.distrosDoNotMatch(runImageDistro.toString(), builderImageDistro.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2012-present 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.util.Map;

import org.jspecify.annotations.Nullable;

import org.springframework.boot.buildpack.platform.docker.type.Image;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* The OS distribution of an image, as described by the target data of the CNB platform
* specification.
*
* @author Moritz Halbritter
*/
final class Distro {

private static final String NAME_LABEL = "io.buildpacks.base.distro.name";

private static final String VERSION_LABEL = "io.buildpacks.base.distro.version";

// Paketo images only set these non-spec labels
private static final String STACK_NAME_LABEL = "io.buildpacks.stack.distro.name";

private static final String STACK_VERSION_LABEL = "io.buildpacks.stack.distro.version";

private final @Nullable String name;

private final @Nullable String version;

private Distro(@Nullable String name, @Nullable String version) {
this.name = name;
this.version = version;
}

/**
* Return whether this distribution matches the given distribution. A missing name or
* version matches any value.
* @param other the distribution to compare with
* @return {@code true} if the distributions match
*/
boolean matches(Distro other) {
return matches(this.name, other.name) && matches(this.version, other.version);
}

private static boolean matches(@Nullable String value, @Nullable String other) {
if (value == null || other == null) {
return true;
}
return value.equals(other);
}

@Override
public String toString() {
StringBuilder result = new StringBuilder();
if (this.name != null) {
result.append(this.name);
}
if (this.version != null) {
if (!result.isEmpty()) {
result.append(" ");
}
result.append(this.version);
}
return result.toString();
}

/**
* Factory method to create a {@link Distro} from an {@link Image}.
* @param image the source image
* @return the extracted distribution
*/
static Distro fromImage(Image image) {
Assert.notNull(image, "'image' must not be null");
Map<String, String> labels = image.getConfig().getLabels();
String name = getLabel(labels, NAME_LABEL, STACK_NAME_LABEL);
String version = getLabel(labels, VERSION_LABEL, STACK_VERSION_LABEL);
return new Distro(name, version);
}

private static @Nullable String getLabel(Map<String, String> labels, String label, String fallbackLabel) {
String value = labels.get(label);
if (StringUtils.hasText(value)) {
return value;
}
value = labels.get(fallbackLabel);
return StringUtils.hasText(value) ? value : null;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -447,22 +447,25 @@ void buildInvokesBuilderWithPlatform() throws Exception {
}

@Test
void buildWhenStackIdDoesNotMatchLogsWarning() throws Exception {
TestPrintStream out = new TestPrintStream();
DockerApi docker = mockDockerApi();
Image builderImage = loadImage("image.json");
Image runImage = loadImage("run-image-with-bad-stack.json");
given(docker.image().pull(eq(DEFAULT_BUILDER), isNull(), any(), isNull()))
.willAnswer(withPulledImage(builderImage));
given(docker.image().pull(eq(BASE_CNB), eq(ImagePlatform.from(builderImage)), any(), isNull()))
.willAnswer(withPulledImage(runImage));
Builder builder = new Builder(BuildLog.to(out), docker, null);
BuildRequest request = getTestRequest();
builder.build(request);
assertThat(out.toString()).contains(
"Warning: Run image stack 'org.cloudfoundry.stacks.cfwindowsfs3' does not match builder stack 'io.buildpacks.stacks.bionic'");
assertThat(out.toString()).contains("Running creator");
assertThat(out.toString()).contains("Successfully built image 'docker.io/library/my-application:latest'");
void shouldNotWarnWhenStackIdsDifferWithoutDistro() throws Exception {
String output = buildWith("image.json", "run-image-with-bad-stack.json");
assertThat(output).doesNotContain("Warning");
assertThat(output).contains("Successfully built image 'docker.io/library/my-application:latest'");
}

@Test
void shouldNotWarnWhenStackIdsDifferButDistrosMatch() throws Exception {
String output = buildWith("image-with-distro.json", "run-image-with-same-distro.json");
assertThat(output).doesNotContain("Warning");
assertThat(output).contains("Successfully built image 'docker.io/library/my-application:latest'");
}

@Test
void shouldWarnWhenDistrosDoNotMatch() throws Exception {
String output = buildWith("image-with-distro.json", "run-image-with-other-distro.json");
assertThat(output).contains(
"Warning: Run image distribution 'ubuntu 24.04' does not match builder distribution 'ubuntu 26.04'");
assertThat(output).contains("Successfully built image 'docker.io/library/my-application:latest'");
}

@Test
Expand Down Expand Up @@ -553,6 +556,20 @@ private BuildRequest getTestRequest() {
return BuildRequest.of(name, (owner) -> content).withTrustBuilder(true);
}

private String buildWith(String builderImageName, String runImageName) throws Exception {
TestPrintStream out = new TestPrintStream();
DockerApi docker = mockDockerApi();
Image builderImage = loadImage(builderImageName);
Image runImage = loadImage(runImageName);
given(docker.image().pull(eq(DEFAULT_BUILDER), isNull(), any(), isNull()))
.willAnswer(withPulledImage(builderImage));
given(docker.image().pull(eq(BASE_CNB), eq(ImagePlatform.from(builderImage)), any(), isNull()))
.willAnswer(withPulledImage(runImage));
Builder builder = new Builder(BuildLog.to(out), docker, null);
builder.build(getTestRequest());
return out.toString();
}

private Image loadImage(String name) throws IOException {
return Image.of(getClass().getResourceAsStream(name));
}
Expand Down
Loading
Loading