Skip to content

Commit

Permalink
Allow specifying image configuration via Maps. (#360)
Browse files Browse the repository at this point in the history
This allows merging of configuration using the normal Maven behavior.

Signed-off-by: Wolfgang Kritzinger <wkritzinger@atlassian.com>
  • Loading branch information
wkritzinger-atlassian committed Aug 21, 2019
1 parent b66dfaa commit 37cfcd1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/main/asciidoc/inc/_image-configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ Either a `<build>` or `<run>` section must be present (except when you are using

include::image/_example.adoc[]

When using Maven profiles, it can be useful to override settings
of a particular image. To facilitate this, the element `<imagesMap>`
can be used alongside the `<images>` element. Each entry in `<imagesMap>`
translates to an image configuration where the **alias** of the image
is set to the map entry's **key**. The examples above and below produce
identical image configurations.

include::image/_example_imagesMap.adoc[]

include::image/_naming.adoc[]
15 changes: 15 additions & 0 deletions src/main/asciidoc/inc/image/_example_imagesMap.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

.Example
[source,xml]
----
<configuration>
....
<imagesMap>
<service>
<name>%g/docker-demo:0.1</name>
<run>....</run>
<build>....</build>
</service>
</images>
</configuration>
----
28 changes: 26 additions & 2 deletions src/main/java/io/fabric8/maven/docker/AbstractDockerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.google.common.collect.ImmutableList;
import io.fabric8.maven.docker.access.DockerAccess;
import io.fabric8.maven.docker.access.ExecException;
import io.fabric8.maven.docker.config.BuildImageConfiguration;
Expand Down Expand Up @@ -181,6 +183,12 @@ public abstract class AbstractDockerMojo extends AbstractMojo implements Context
@Parameter
private List<ImageConfiguration> images;

/**
* Image configurations configured via maps to allow overriding.
*/
@Parameter
private Map<String, ImageConfiguration> imagesMap;

// Docker-machine configuration
@Parameter
private DockerMachineConfiguration machine;
Expand Down Expand Up @@ -216,7 +224,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {

LogOutputSpecFactory logSpecFactory = new LogOutputSpecFactory(useColor, logStdout, logDate);

ConfigHelper.validateExternalPropertyActivation(project, images);
ConfigHelper.validateExternalPropertyActivation(project, getAllImages());

DockerAccess access = null;
try {
Expand Down Expand Up @@ -324,7 +332,7 @@ private String initImageConfiguration(Date buildTimeStamp) {
// Resolve images
resolvedImages = ConfigHelper.resolveImages(
log,
images, // Unresolved images
getAllImages(), // Unresolved images
new ConfigHelper.Resolver() {
@Override
public List<ImageConfiguration> resolve(ImageConfiguration image) {
Expand Down Expand Up @@ -417,6 +425,22 @@ protected LogDispatcher getLogDispatcher(ServiceHub hub) {
return dispatcher;
}

private ImmutableList<ImageConfiguration> getAllImages() {
ImmutableList.Builder<ImageConfiguration> allImages = ImmutableList.builder();
if (images != null) {
allImages.addAll(images);
}
if (imagesMap != null) {
imagesMap.forEach((alias, config) -> {
if (config.getAlias() == null) {
config.setAlias(alias);
}
allImages.add(config);
});
}
return allImages.build();
}

public ImagePullManager getImagePullManager(String imagePullPolicy, String autoPull) {
return new ImagePullManager(getSessionCacheStore(), imagePullPolicy, autoPull);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public String getAlias() {
return alias;
}

public void setAlias(String alias) {
this.alias = alias;
}

public String getStopNamePattern() {
return stopNamePattern;
}
Expand Down

0 comments on commit 37cfcd1

Please sign in to comment.