Skip to content

Commit

Permalink
fix : Introduce pullRetries and pushRetries fields
Browse files Browse the repository at this point in the history
Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Jul 28, 2023
1 parent e823b55 commit 671eae8
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 22 deletions.
3 changes: 0 additions & 3 deletions src/main/asciidoc/inc/build/_configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ a| Scan the archive specified in `dockerArchive` and find the actual repository
| *shell*
| Shell to be used for the *runCmds*. It contains *arg* elements which are defining the executable and its params.

| *retries*
| If pulling an image is required, how often should a pull be retried before giving up. This useful for flaky registries which tend to return 500 error codes from time to time. The default is 0 which means no retry at all.

| *runCmds*
| Commands to be run during the build process. It contains *run* elements which are passed to the shell. Whitespace is trimmed from each element and empty elements are ignored. The run commands are inserted right after the assembly and after *workdir* into the Dockerfile. This tag is not to be confused with the `<run>` section for this image which specifies the runtime behaviour when starting containers.

Expand Down
2 changes: 1 addition & 1 deletion src/main/asciidoc/inc/start/_configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ In addition to the <<global-configuration>>, this goal supports the following gl
| Default pattern for naming all containers when they are created. See <<container-name, Container Names>> for details.
| `docker.containerNamePattern`

| *retries*
| *pullRetries*
| If pulling an image is required, how often should a pull be retried before giving up. This useful for flaky registries which tend to return 500 error codes from time to time. The default is 0 which means no retry at all.
| `docker.pull.retries`

Expand Down
4 changes: 2 additions & 2 deletions src/main/asciidoc/inc/watch/_configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ below how this can be specified.
| `docker.keepRunning`

| *keepContainer*
| As for `{plugin}:stop`, if this is set to `true` (and `keepRunning` is disabled) then all container will be removed after they have been stopped. The default is `true`.
| As for `{plugin}:stop`, if this is set to `true` (and `keepRunning` is disabled) then all container will be removed after they have been stopped. The default is `false`.
| `docker.keepContainer`

| *removeVolumes*
| if set to `true` will remove any volumes associated to the container as well. This option will be ignored if either `keepContainer` or `keepRunning` are `true`.
| `docker.removeVolumes`

| *retries*
| *pullRetries*
| If pulling an image is required, how often should a pull be retried before giving up. This useful for flaky registries which tend to return 500 error codes from time to time. The default is 0 which means no retry at all.
| `docker.pull.retries`
|===
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/io/fabric8/maven/docker/AbstractDockerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,15 @@ public abstract class AbstractDockerMojo extends AbstractMojo implements Context
@Parameter(property = "docker.removeVolumes", defaultValue = "false")
protected boolean removeVolumes;

@Parameter(property = "docker.pull.retries", defaultValue = "0")
@Parameter(property = "docker.retries", defaultValue = "0")
protected int retries;

@Parameter(property = "docker.pull.retries", defaultValue = "0")
protected int pullRetries;

@Parameter(property = "docker.push.retries", defaultValue = "0")
protected int pushRetries;

@Parameter(property = "docker.apiVersion")
private String apiVersion;

Expand Down Expand Up @@ -580,7 +586,7 @@ protected void pullImage(RegistryService registryService, ImageConfiguration ima
RunImageConfiguration runConfiguration = imageConfig.getRunConfiguration();
ImagePullManager pullManager = getImagePullManager(determinePullPolicy(runConfiguration), autoPull);
RegistryConfig registryConfig = getRegistryConfig(pullRegistry);
registryService.pullImageWithPolicy(imageName, pullManager, registryConfig, imageConfig.getBuildConfiguration(), retries);
registryService.pullImageWithPolicy(imageName, pullManager, registryConfig, imageConfig.getBuildConfiguration(), getPullRetries());
}

protected boolean shouldSkipPom() {
Expand Down Expand Up @@ -611,4 +617,18 @@ protected String determinePullPolicy(RunImageConfiguration runConfig) {
protected ProjectPaths createProjectPaths() {
return new ProjectPaths(project.getBasedir(), outputDirectory);
}

protected int getPullRetries() {
if (pullRetries > 0) {
return pullRetries;
}
return retries;
}

protected int getPushRetries() {
if (pushRetries > 0) {
return pushRetries;
}
return retries;
}
}
4 changes: 1 addition & 3 deletions src/main/java/io/fabric8/maven/docker/BuildMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ public class BuildMojo extends AbstractBuildSupportMojo {
*/
@Parameter(property = "docker.skip.tag", defaultValue = "false")
protected boolean skipTag;
@Parameter(property = "docker.pull.retries", defaultValue = "0")
private int retries;

@Override
protected void executeInternal(ServiceHub hub) throws IOException, MojoExecutionException {
Expand Down Expand Up @@ -113,7 +111,7 @@ private void proceedWithDockerBuild(ServiceHub hub, BuildService.BuildContext bu
if (imageConfig.isBuildX()) {
hub.getBuildXService().build(createProjectPaths(), imageConfig, null, getAuthConfig(imageConfig), buildArchiveFile);
} else {
buildService.buildImage(imageConfig, pullManager, buildContext, buildArchiveFile, retries);
buildService.buildImage(imageConfig, pullManager, buildContext, buildArchiveFile, getPullRetries());
if (!skipTag) {
buildService.tagImage(imageConfig);
}
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/io/fabric8/maven/docker/PushMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ public class PushMojo extends AbstractDockerMojo {
*/
@Parameter(property = "docker.skip.tag", defaultValue = "false")
private boolean skipTag;

@Parameter(property = "docker.push.retries", defaultValue = "0")
private int retries;

/**
* {@inheritDoc}
Expand All @@ -51,14 +48,14 @@ public void executeInternal(ServiceHub hub) throws DockerAccessException, MojoEx
}

private void executeDockerPush(ServiceHub hub) throws MojoExecutionException, DockerAccessException {
hub.getRegistryService().pushImages(createProjectPaths(), getResolvedImages(), retries, getRegistryConfig(pushRegistry), skipTag);
hub.getRegistryService().pushImages(createProjectPaths(), getResolvedImages(), getPushRetries(), getRegistryConfig(pushRegistry), skipTag);
}

private void executeJibPush(ServiceHub hub) throws MojoExecutionException {
log.info("Pushing Container image with [[B]]JIB(Java Image Builder)[[B]] mode");
JibBuildService jibBuildService = new JibBuildService(hub, new MojoParameters(session, project, null, null, null,
settings, sourceDirectory, outputDirectory, null), log);
jibBuildService.push(getResolvedImages(), retries, getRegistryConfig(pushRegistry), skipTag);
jibBuildService.push(getResolvedImages(), getPushRetries(), getRegistryConfig(pushRegistry), skipTag);
}

}
1 change: 0 additions & 1 deletion src/main/java/io/fabric8/maven/docker/StartMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import io.fabric8.maven.docker.config.RunVolumeConfiguration;
import io.fabric8.maven.docker.config.VolumeConfiguration;
import io.fabric8.maven.docker.log.LogDispatcher;
import io.fabric8.maven.docker.service.ImagePullManager;
import io.fabric8.maven.docker.service.QueryService;
import io.fabric8.maven.docker.service.RegistryService;
import io.fabric8.maven.docker.service.RunService;
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/io/fabric8/maven/docker/WatchMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,14 @@ public class WatchMojo extends AbstractBuildSupportMojo {
@Parameter(property = "docker.autoCreateCustomNetworks", defaultValue = "false")
protected boolean autoCreateCustomNetworks;

@Parameter(property = "docker.pull.retries", defaultValue = "0")
private int retries;

@Override
protected synchronized void executeInternal(ServiceHub hub) throws IOException,
MojoExecutionException {

BuildService.BuildContext buildContext = getBuildContext();
WatchService.WatchContext watchContext = getWatchContext(hub);

hub.getWatchService().watch(watchContext, buildContext, getResolvedImages(), retries);
hub.getWatchService().watch(watchContext, buildContext, getResolvedImages(), getPullRetries());
}

protected WatchService.WatchContext getWatchContext(ServiceHub hub) throws IOException {
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/io/fabric8/maven/docker/BuildMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(MockitoExtension.class)
class BuildMojoTest extends MojoTestBase {
private static final String NON_NATIVE_PLATFORM = "linux/amd64";
Expand Down Expand Up @@ -241,6 +243,29 @@ void buildUsingBuildxWithMultipleAuth() throws IOException, MojoExecutionExcepti
thenAuthContainsRegistry("custom-registry.org");
}

@Test
void getPullRetries_whenPullRetriesConfigured_thenUsePullRetries() {
givenMavenProject(buildMojo);
buildMojo.pullRetries = 2;

assertEquals(2, buildMojo.getPullRetries());
}

@Test
void getPullRetries_whenRetriesConfigured_thenUseRetries() {
givenMavenProject(buildMojo);
buildMojo.retries = 2;

assertEquals(2, buildMojo.getPullRetries());
}

@Test
void getPullRetries_whenNothingConfigured_thenReturnDefaultValue() {
givenMavenProject(buildMojo);

assertEquals(0, buildMojo.getPullRetries());
}

private void givenBuildXService() {
BuildXService buildXService = new BuildXService(dockerAccess, dockerAssemblyManager, log, exec);

Expand Down
25 changes: 25 additions & 0 deletions src/test/java/io/fabric8/maven/docker/PushMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(MockitoExtension.class)
class PushMojoTest extends MojoTestBase {
@InjectMocks
Expand Down Expand Up @@ -71,6 +73,29 @@ void executeInternal_whenSkipDisabled_thenImageIsPushed() throws MojoExecutionEx
thenImagePushed();
}

@Test
void getPushRetries_whenPushRetriesConfigured_thenUsePushRetries() {
givenMavenProject(pushMojo);
pushMojo.pushRetries = 2;

assertEquals(2, pushMojo.getPushRetries());
}

@Test
void getPushRetries_whenRetriesConfigured_thenUseRetries() {
givenMavenProject(pushMojo);
pushMojo.retries = 2;

assertEquals(2, pushMojo.getPushRetries());
}

@Test
void getPushRetries_whenNothingConfigured_thenReturnDefaultValue() {
givenMavenProject(pushMojo);

assertEquals(0, pushMojo.getPushRetries());
}

private void thenImagePushed() throws MojoExecutionException, DockerAccessException {
verifyPush(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.times;

@ExtendWith(MockitoExtension.class)
class DockerAccessWithHcClientTest {
Expand Down Expand Up @@ -552,7 +553,7 @@ private void givenPostCallThrowsException() throws IOException {

private void thenImageWasPulled(int pushPullRetries) throws IOException {
ArgumentCaptor<String> urlCapture = ArgumentCaptor.forClass(String.class);
Mockito.verify(mockDelegate)
Mockito.verify(mockDelegate, times(pushPullRetries))
.post(urlCapture.capture(), Mockito.isNull(), Mockito.anyMap(), Mockito.any(ResponseHandler.class), Mockito.eq(HTTP_OK));

String postUrl = urlCapture.getValue();
Expand Down

0 comments on commit 671eae8

Please sign in to comment.