Skip to content

Commit 2178c28

Browse files
Polish "Add network option for image building"
See gh-27486
1 parent 8e6d03b commit 2178c28

File tree

13 files changed

+145
-23
lines changed

13 files changed

+145
-23
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ public BuildRequest withBindings(List<Binding> bindings) {
265265
this.network);
266266
}
267267

268+
/**
269+
* Return a new {@link BuildRequest} with an updated network setting.
270+
* @param network the network the build container will connect to
271+
* @return an updated build request
272+
* @since 2.6.0
273+
*/
268274
public BuildRequest withNetwork(String network) {
269275
return new BuildRequest(this.name, this.applicationContent, this.builder, this.runImage, this.creator, this.env,
270276
this.cleanCache, this.verboseLogging, this.pullPolicy, this.publish, this.buildpacks, this.bindings,
@@ -371,6 +377,11 @@ public List<Binding> getBindings() {
371377
return this.bindings;
372378
}
373379

380+
/**
381+
* Return the network the build container will connect to.
382+
* @return the network
383+
* @since 2.6.0
384+
*/
374385
public String getNetwork() {
375386
return this.network;
376387
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ private Phase createPhase() {
148148
this.request.getBindings().forEach(phase::withBinding);
149149
}
150150
phase.withEnv(PLATFORM_API_VERSION_KEY, this.platformVersion.toString());
151-
phase.withNetworkMode(this.request.getNetwork());
151+
if (this.request.getNetwork() != null) {
152+
phase.withNetworkMode(this.request.getNetwork());
153+
}
152154
return phase;
153155
}
154156

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ void withEnv(String name, String value) {
104104
this.env.put(name, value);
105105
}
106106

107+
/**
108+
* Update this phase with the network the build container will connect to.
109+
* @param networkMode the network
110+
*/
107111
void withNetworkMode(String networkMode) {
108112
this.networkMode = networkMode;
109113
}
@@ -134,7 +138,9 @@ void apply(ContainerConfig.Update update) {
134138
update.withLabel("author", "spring-boot");
135139
this.bindings.forEach(update::withBinding);
136140
this.env.forEach(update::withEnv);
137-
update.withNetworkMode(this.networkMode);
141+
if (this.networkMode != null) {
142+
update.withNetworkMode(this.networkMode);
143+
}
138144
}
139145

140146
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ public void withEnv(String name, String value) {
188188
this.env.put(name, value);
189189
}
190190

191+
/**
192+
* Update the container config with the network that the build container will
193+
* connect to.
194+
* @param networkMode the network
195+
*/
191196
public void withNetworkMode(String networkMode) {
192197
this.networkMode = networkMode;
193198
}

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ void applyUpdatesConfiguration() {
5757
phase.apply(update);
5858
verify(update).withCommand("/cnb/lifecycle/test", NO_ARGS);
5959
verify(update).withLabel("author", "spring-boot");
60-
verify(update).withNetworkMode(null);
6160
verifyNoMoreInteractions(update);
6261
}
6362

@@ -71,7 +70,6 @@ void applyWhenWithDaemonAccessUpdatesConfigurationWithRootUserAndDomainSocketBin
7170
verify(update).withBinding(Binding.from("/var/run/docker.sock", "/var/run/docker.sock"));
7271
verify(update).withCommand("/cnb/lifecycle/test", NO_ARGS);
7372
verify(update).withLabel("author", "spring-boot");
74-
verify(update).withNetworkMode(null);
7573
verifyNoMoreInteractions(update);
7674
}
7775

@@ -83,7 +81,6 @@ void applyWhenWithLogLevelArgAndVerboseLoggingUpdatesConfigurationWithLogLevel()
8381
phase.apply(update);
8482
verify(update).withCommand("/cnb/lifecycle/test", "-log-level", "debug");
8583
verify(update).withLabel("author", "spring-boot");
86-
verify(update).withNetworkMode(null);
8784
verifyNoMoreInteractions(update);
8885
}
8986

@@ -95,7 +92,6 @@ void applyWhenWithLogLevelArgAndNonVerboseLoggingDoesNotUpdateLogLevel() {
9592
phase.apply(update);
9693
verify(update).withCommand("/cnb/lifecycle/test");
9794
verify(update).withLabel("author", "spring-boot");
98-
verify(update).withNetworkMode(null);
9995
verifyNoMoreInteractions(update);
10096
}
10197

@@ -107,7 +103,6 @@ void applyWhenWithArgsUpdatesConfigurationWithArguments() {
107103
phase.apply(update);
108104
verify(update).withCommand("/cnb/lifecycle/test", "a", "b", "c");
109105
verify(update).withLabel("author", "spring-boot");
110-
verify(update).withNetworkMode(null);
111106
verifyNoMoreInteractions(update);
112107
}
113108

@@ -121,7 +116,6 @@ void applyWhenWithBindsUpdatesConfigurationWithBinds() {
121116
verify(update).withCommand("/cnb/lifecycle/test");
122117
verify(update).withLabel("author", "spring-boot");
123118
verify(update).withBinding(Binding.from(volumeName, "/test"));
124-
verify(update).withNetworkMode(null);
125119
verifyNoMoreInteractions(update);
126120
}
127121

@@ -136,7 +130,6 @@ void applyWhenWithEnvUpdatesConfigurationWithEnv() {
136130
verify(update).withLabel("author", "spring-boot");
137131
verify(update).withEnv("name1", "value1");
138132
verify(update).withEnv("name2", "value2");
139-
verify(update).withNetworkMode(null);
140133
verifyNoMoreInteractions(update);
141134
}
142135

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/packaging-oci-image.adoc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ Where `<options>` can contain:
155155
* `volume-opt=key=value` to specify key-value pairs consisting of an option name and its value
156156
|
157157

158+
| `network`
159+
| `--network`
160+
| The https://docs.docker.com/network/#network-drivers[network driver] the builder container will be configured to use.
161+
The value supplied will be passed unvalidated to Docker when creating the builder container.
162+
|
163+
158164
| `cleanCache`
159165
| `--cleanCache`
160166
| Whether to clean the cache before building.
@@ -170,13 +176,6 @@ Where `<options>` can contain:
170176
| Whether to publish the generated image to a Docker registry.
171177
| `false`
172178

173-
| `network`
174-
| `--network`
175-
| The network the build container will connect to. The value supplied for this option will be passed
176-
unvalidated as `HostConfig.NetworkMode` to the configuration which creates the build container,
177-
see https://docs.docker.com/engine/api/v1.41/#operation/ContainerCreate[Docker's engine API].
178-
Using this option is similar to running `docker build --network ...`.
179-
|
180179
|===
181180

182181
NOTE: The plugin detects the target Java compatibility of the project using the JavaPlugin's `targetCompatibility` property.

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ void buildsImageWithDefaultBuilder() throws IOException {
7171
assertThat(result.getOutput()).contains("docker.io/library/" + projectName);
7272
assertThat(result.getOutput()).contains("---> Test Info buildpack building");
7373
assertThat(result.getOutput()).contains("env: BP_JVM_VERSION=8.*");
74+
assertThat(result.getOutput()).contains("Network status: HTTP/2 200");
7475
assertThat(result.getOutput()).contains("---> Test Info buildpack done");
7576
removeImage(projectName);
7677
}
@@ -247,6 +248,20 @@ void buildsImageWithLaunchScript() throws IOException {
247248
removeImage(projectName);
248249
}
249250

251+
@TestTemplate
252+
void buildsImageWithNetworkModeNone() throws IOException {
253+
writeMainClass();
254+
writeLongNameResource();
255+
BuildResult result = this.gradleBuild.build("bootBuildImage", "--pullPolicy=IF_NOT_PRESENT");
256+
String projectName = this.gradleBuild.getProjectDir().getName();
257+
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
258+
assertThat(result.getOutput()).contains("docker.io/library/" + projectName);
259+
assertThat(result.getOutput()).contains("---> Test Info buildpack building");
260+
assertThat(result.getOutput()).contains("Network status: curl failed");
261+
assertThat(result.getOutput()).contains("---> Test Info buildpack done");
262+
removeImage(projectName);
263+
}
264+
250265
@TestTemplate
251266
void failsWithBuilderError() throws IOException {
252267
writeMainClass();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
plugins {
2+
id 'java'
3+
id 'org.springframework.boot' version '{version}'
4+
}
5+
6+
if (project.hasProperty('applyWarPlugin')) {
7+
apply plugin: 'war'
8+
}
9+
10+
sourceCompatibility = '1.8'
11+
targetCompatibility = '1.8'
12+
13+
bootBuildImage {
14+
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
15+
network = "none"
16+
}

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/docs/asciidoc/packaging-oci-image.adoc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ Where `<options>` can contain:
163163
* `volume-opt=key=value` to specify key-value pairs consisting of an option name and its value
164164
|
165165

166+
| `network` +
167+
(`spring-boot.build-image.network`)
168+
| The https://docs.docker.com/network/#network-drivers[network driver] the builder container will be configured to use.
169+
The value supplied will be passed unvalidated to Docker when creating the builder container.
170+
|
171+
166172
| `cleanCache` +
167173
(`spring-boot.build-image.cleanCache`)
168174
| Whether to clean the cache before building.
@@ -177,13 +183,6 @@ Where `<options>` can contain:
177183
| Whether to publish the generated image to a Docker registry.
178184
| `false`
179185

180-
| `network` +
181-
(`spring-boot.build-image.network`)
182-
| The network the build container will connect to. The value supplied for this option will be passed
183-
unvalidated as `HostConfig.NetworkMode` to the configuration which creates the build container,
184-
see https://docs.docker.com/engine/api/v1.41/#operation/ContainerCreate[Docker's engine API].
185-
Using this option is similar to running `docker build --network ...`.
186-
|
187186
|===
188187

189188
NOTE: The plugin detects the target Java compatibility of the project using the compiler's plugin configuration or the `maven.compiler.target` property.

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/BuildImageTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,17 @@ void whenBuildImageIsInvokedWithBinding(MavenBuild mavenBuild) {
270270
});
271271
}
272272

273+
@TestTemplate
274+
void whenBuildImageIsInvokedWithNetworkModeNone(MavenBuild mavenBuild) {
275+
mavenBuild.project("build-image-network").goals("package")
276+
.systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT").execute((project) -> {
277+
assertThat(buildLog(project)).contains("Building image")
278+
.contains("docker.io/library/build-image-network:0.0.1.BUILD-SNAPSHOT")
279+
.contains("Network status: curl failed").contains("Successfully built image");
280+
removeImage("build-image-network", "0.0.1.BUILD-SNAPSHOT");
281+
});
282+
}
283+
273284
@TestTemplate
274285
void whenBuildImageIsInvokedOnMultiModuleProjectWithPackageGoal(MavenBuild mavenBuild) {
275286
mavenBuild.project("build-image-multi-module").goals("package")

0 commit comments

Comments
 (0)