Skip to content

Commit

Permalink
Allow specifying image configuration via Maps. (#360) (#1259)
Browse files Browse the repository at this point in the history
* Allow specifying image configuration via Maps. (#360)

This allows merging of configuration using the normal Maven behavior.

Signed-off-by: Wolfgang Kritzinger <wkritzinger@atlassian.com>

* Update changelog.md (#360)

Signed-off-by: Wolfgang Kritzinger <wkritzinger@atlassian.com>
  • Loading branch information
wkritzinger-atlassian authored and rhuss committed Oct 8, 2019
1 parent 76a184e commit da1e203
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* **0.31-SNAPSHOT**
- Update to jnr-unixsocket 0.23
- Add null check for null instance in config.json for email #1262
- Allow merging of image configurations using <imagesMap> ([#360](https://github.com/fabric8io/docker-maven-plugin/issues/360))
- Treat bridged and default network mode the same #1234

* **0.31.0** (2019-08-10)
Expand Down
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 da1e203

Please sign in to comment.