Skip to content

Commit c30a557

Browse files
committed
Polish
1 parent 890b4ed commit c30a557

File tree

3 files changed

+36
-42
lines changed

3 files changed

+36
-42
lines changed

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,10 @@ public void start(BuildRequest request) {
4646
@Override
4747
public Consumer<TotalProgressEvent> pullingImage(ImageReference imageReference, ImagePlatform platform,
4848
ImageType imageType) {
49-
String message;
50-
if (platform != null) {
51-
message = String.format(" > Pulling %s '%s' for platform '%s'", imageType.getDescription(), imageReference,
52-
platform);
53-
}
54-
else {
55-
message = String.format(" > Pulling %s '%s'", imageType.getDescription(), imageReference);
56-
}
57-
return getProgressConsumer(message);
49+
return (platform != null)
50+
? getProgressConsumer(" > Pulling %s '%s' for platform '%s'".formatted(imageType.getDescription(),
51+
imageReference, platform))
52+
: getProgressConsumer(" > Pulling %s '%s'".formatted(imageType.getDescription(), imageReference));
5853
}
5954

6055
@Override

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

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public class DockerApi {
6464

6565
private static final List<String> FORCE_PARAMS = Collections.unmodifiableList(Arrays.asList("force", "1"));
6666

67-
static final ApiVersion MINIMUM_API_VERSION = ApiVersion.parse("1.24");
67+
static final ApiVersion MINIMUM_API_VERSION = ApiVersion.of(1, 24);
68+
69+
static final ApiVersion MINIMUM_PLATFORM_API_VERSION = ApiVersion.of(1, 41);
6870

6971
static final String API_VERSION_HEADER_NAME = "API-Version";
7072

@@ -80,7 +82,7 @@ public class DockerApi {
8082

8183
private final SystemApi system;
8284

83-
private ApiVersion apiVersion = null;
85+
private volatile ApiVersion apiVersion = null;
8486

8587
/**
8688
* Create a new {@link DockerApi} instance.
@@ -126,10 +128,7 @@ private URI buildUrl(String path, Collection<?> params) {
126128

127129
private URI buildUrl(String path, Object... params) {
128130
try {
129-
if (this.apiVersion == null) {
130-
this.apiVersion = this.system.getApiVersion();
131-
}
132-
URIBuilder builder = new URIBuilder("/v" + this.apiVersion + path);
131+
URIBuilder builder = new URIBuilder("/v" + getApiVersion() + path);
133132
int param = 0;
134133
while (param < params.length) {
135134
builder.addParameter(Objects.toString(params[param++]), Objects.toString(params[param++]));
@@ -141,11 +140,19 @@ private URI buildUrl(String path, Object... params) {
141140
}
142141
}
143142

144-
private void verifyApiVersionForPlatform() {
145-
ApiVersion minimumPlatformApiVersion = ApiVersion.of(1, 41);
146-
Assert.isTrue(this.apiVersion.supports(minimumPlatformApiVersion),
147-
"Docker API version must be at least " + minimumPlatformApiVersion
148-
+ " to support the 'imagePlatform' option, but current API version is " + this.apiVersion);
143+
private void verifyApiVersionForPlatform(ImagePlatform platform) {
144+
Assert.isTrue(platform == null || getApiVersion().supports(MINIMUM_PLATFORM_API_VERSION),
145+
() -> "Docker API version must be at least " + MINIMUM_PLATFORM_API_VERSION
146+
+ " to support the 'imagePlatform' option, but current API version is " + getApiVersion());
147+
}
148+
149+
private ApiVersion getApiVersion() {
150+
ApiVersion apiVersion = this.apiVersion;
151+
if (this.apiVersion == null) {
152+
apiVersion = this.system.getApiVersion();
153+
this.apiVersion = apiVersion;
154+
}
155+
return apiVersion;
149156
}
150157

151158
/**
@@ -206,14 +213,10 @@ public Image pull(ImageReference reference, ImagePlatform platform,
206213
UpdateListener<PullImageUpdateEvent> listener, String registryAuth) throws IOException {
207214
Assert.notNull(reference, "Reference must not be null");
208215
Assert.notNull(listener, "Listener must not be null");
209-
URI createUri;
210-
if (platform != null) {
211-
createUri = buildUrl("/images/create", "fromImage", reference, "platform", platform);
212-
verifyApiVersionForPlatform();
213-
}
214-
else {
215-
createUri = buildUrl("/images/create", "fromImage", reference);
216-
}
216+
verifyApiVersionForPlatform(platform);
217+
URI createUri = (platform != null)
218+
? buildUrl("/images/create", "fromImage", reference, "platform", platform)
219+
: buildUrl("/images/create", "fromImage", reference);
217220
DigestCaptureUpdateListener digestCapture = new DigestCaptureUpdateListener();
218221
listener.onStart();
219222
try {
@@ -400,14 +403,9 @@ public ContainerReference create(ContainerConfig config, ImagePlatform platform,
400403
}
401404

402405
private ContainerReference createContainer(ContainerConfig config, ImagePlatform platform) throws IOException {
403-
URI createUri;
404-
if (platform != null) {
405-
createUri = buildUrl("/containers/create", "platform", platform);
406-
verifyApiVersionForPlatform();
407-
}
408-
else {
409-
createUri = buildUrl("/containers/create");
410-
}
406+
verifyApiVersionForPlatform(platform);
407+
URI createUri = (platform != null) ? buildUrl("/containers/create", "platform", platform)
408+
: buildUrl("/containers/create");
411409
try (Response response = http().post(createUri, "application/json", config::writeTo)) {
412410
return ContainerReference
413411
.of(SharedObjectMapper.get().readTree(response.getContent()).at("/Id").asText());

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,21 @@ public class ImagePlatform {
4242
}
4343

4444
@Override
45-
public boolean equals(Object o) {
46-
if (this == o) {
45+
public boolean equals(Object obj) {
46+
if (this == obj) {
4747
return true;
4848
}
49-
if (!(o instanceof ImagePlatform that)) {
49+
if (obj == null || getClass() != obj.getClass()) {
5050
return false;
5151
}
52-
return Objects.equals(this.os, that.os) && Objects.equals(this.architecture, that.architecture)
53-
&& Objects.equals(this.variant, that.variant);
52+
ImagePlatform other = (ImagePlatform) obj;
53+
return Objects.equals(this.architecture, other.architecture) && Objects.equals(this.os, other.os)
54+
&& Objects.equals(this.variant, other.variant);
5455
}
5556

5657
@Override
5758
public int hashCode() {
58-
return Objects.hash(this.os, this.architecture, this.variant);
59+
return Objects.hash(this.architecture, this.os, this.variant);
5960
}
6061

6162
@Override

0 commit comments

Comments
 (0)