Skip to content

Commit d64b739

Browse files
committed
Add logging for Docker API version retrieval failures
This commit introduces a new constructor in `DockerApi` that accepts `BuildLog` as a parameter. The `BuildLog` is utilized for logging errors within `DockerApi See gh-43460 Signed-off-by: Dmytro Nosan <[email protected]>
1 parent 48e3de0 commit d64b739

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -126,6 +126,17 @@ public void sensitiveTargetBindingDetected(Binding binding) {
126126
log();
127127
}
128128

129+
@Override
130+
public void failedToGetApiVersion(Exception exception) {
131+
StringBuilder message = new StringBuilder("Warning: Failed to determine Docker API version");
132+
if (exception != null) {
133+
message.append(": ").append(exception.getMessage());
134+
}
135+
log();
136+
log(message.toString());
137+
log();
138+
}
139+
129140
private String getDigest(Image image) {
130141
List<String> digests = image.getDigests();
131142
return (digests.isEmpty() ? "" : digests.get(0));

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -133,6 +133,14 @@ Consumer<TotalProgressEvent> pullingImage(ImageReference imageReference, ImagePl
133133
*/
134134
void sensitiveTargetBindingDetected(Binding binding);
135135

136+
/**
137+
* Log that the Docker API version could not be retrieved.
138+
* @param exception the exception that occurred when attempting to get the Docker API
139+
* version
140+
* @since 3.5.0
141+
*/
142+
void failedToGetApiVersion(Exception exception);
143+
136144
/**
137145
* Factory method that returns a {@link BuildLog} the outputs to {@link System#out}.
138146
* @return a build log instance that logs to system out

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public Builder(DockerConfiguration dockerConfiguration) {
7575
* @param log a logger used to record output
7676
*/
7777
public Builder(BuildLog log) {
78-
this(log, new DockerApi(), null);
78+
this(log, null);
7979
}
8080

8181
/**
@@ -85,7 +85,7 @@ public Builder(BuildLog log) {
8585
* @since 2.4.0
8686
*/
8787
public Builder(BuildLog log, DockerConfiguration dockerConfiguration) {
88-
this(log, new DockerApi((dockerConfiguration != null) ? dockerConfiguration.getHost() : null),
88+
this(log, new DockerApi((dockerConfiguration != null) ? dockerConfiguration.getHost() : null, log),
8989
dockerConfiguration);
9090
}
9191

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.hc.core5.http.Header;
2929
import org.apache.hc.core5.net.URIBuilder;
3030

31+
import org.springframework.boot.buildpack.platform.build.BuildLog;
3132
import org.springframework.boot.buildpack.platform.docker.configuration.DockerConfiguration.DockerHostConfiguration;
3233
import org.springframework.boot.buildpack.platform.docker.transport.HttpTransport;
3334
import org.springframework.boot.buildpack.platform.docker.transport.HttpTransport.Response;
@@ -87,7 +88,7 @@ public class DockerApi {
8788
* Create a new {@link DockerApi} instance.
8889
*/
8990
public DockerApi() {
90-
this(HttpTransport.create(null));
91+
this(HttpTransport.create(null), BuildLog.toSystemOut());
9192
}
9293

9394
/**
@@ -96,21 +97,34 @@ public DockerApi() {
9697
* @since 2.4.0
9798
*/
9899
public DockerApi(DockerHostConfiguration dockerHost) {
99-
this(HttpTransport.create(dockerHost));
100+
this(HttpTransport.create(dockerHost), BuildLog.toSystemOut());
101+
}
102+
103+
/**
104+
* Create a new {@link DockerApi} instance.
105+
* @param dockerHost the Docker daemon host information
106+
* @param log a logger used to record output
107+
* @since 3.5.0
108+
*/
109+
public DockerApi(DockerHostConfiguration dockerHost, BuildLog log) {
110+
this(HttpTransport.create(dockerHost), log);
100111
}
101112

102113
/**
103114
* Create a new {@link DockerApi} instance backed by a specific {@link HttpTransport}
104115
* implementation.
105116
* @param http the http implementation
117+
* @param log a logger used to record output
106118
*/
107-
DockerApi(HttpTransport http) {
119+
DockerApi(HttpTransport http, BuildLog log) {
120+
Assert.notNull(http, "'http' must not be null");
121+
Assert.notNull(log, "'log' must not be null");
108122
this.http = http;
109123
this.jsonStream = new JsonStream(SharedObjectMapper.get());
110124
this.image = new ImageApi();
111125
this.container = new ContainerApi();
112126
this.volume = new VolumeApi();
113-
this.system = new SystemApi();
127+
this.system = new SystemApi(log);
114128
}
115129

116130
private HttpTransport http() {
@@ -485,7 +499,10 @@ public void delete(VolumeName name, boolean force) throws IOException {
485499
*/
486500
class SystemApi {
487501

488-
SystemApi() {
502+
private final BuildLog log;
503+
504+
SystemApi(BuildLog log) {
505+
this.log = log;
489506
}
490507

491508
/**
@@ -502,6 +519,7 @@ ApiVersion getApiVersion() {
502519
}
503520
}
504521
catch (Exception ex) {
522+
this.log.failedToGetApiVersion(ex);
505523
// fall through to return default value
506524
}
507525
return UNKNOWN_API_VERSION;

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.mockito.Mock;
4040
import org.mockito.junit.jupiter.MockitoExtension;
4141

42+
import org.springframework.boot.buildpack.platform.build.BuildLog;
4243
import org.springframework.boot.buildpack.platform.docker.DockerApi.ContainerApi;
4344
import org.springframework.boot.buildpack.platform.docker.DockerApi.ImageApi;
4445
import org.springframework.boot.buildpack.platform.docker.DockerApi.SystemApi;
@@ -59,6 +60,8 @@
5960
import org.springframework.boot.buildpack.platform.io.IOConsumer;
6061
import org.springframework.boot.buildpack.platform.io.Owner;
6162
import org.springframework.boot.buildpack.platform.io.TarArchive;
63+
import org.springframework.boot.testsupport.system.CapturedOutput;
64+
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
6265
import org.springframework.util.LinkedMultiValueMap;
6366
import org.springframework.util.MultiValueMap;
6467

@@ -82,7 +85,7 @@
8285
* @author Rafael Ceccone
8386
* @author Moritz Halbritter
8487
*/
85-
@ExtendWith(MockitoExtension.class)
88+
@ExtendWith({ MockitoExtension.class, OutputCaptureExtension.class })
8689
class DockerApiTests {
8790

8891
private static final String API_URL = "/v" + DockerApi.API_VERSION;
@@ -108,7 +111,7 @@ class DockerApiTests {
108111

109112
@BeforeEach
110113
void setup() {
111-
this.dockerApi = new DockerApi(this.http);
114+
this.dockerApi = new DockerApi(this.http, BuildLog.toSystemOut());
112115
}
113116

114117
private HttpTransport http() {
@@ -732,9 +735,10 @@ void getApiVersionWithNoVersionHeaderReturnsUnknownVersion() throws Exception {
732735
}
733736

734737
@Test
735-
void getApiVersionWithExceptionReturnsUnknownVersion() throws Exception {
738+
void getApiVersionWithExceptionReturnsUnknownVersion(CapturedOutput output) throws Exception {
736739
given(http().head(eq(new URI(PING_URL)))).willThrow(new IOException("simulated error"));
737740
assertThat(this.api.getApiVersion()).isEqualTo(DockerApi.UNKNOWN_API_VERSION);
741+
assertThat(output).contains("Warning: Failed to determine Docker API version: simulated error");
738742
}
739743

740744
}

0 commit comments

Comments
 (0)