Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
added Java11 base images for docker builds (#1652)
Browse files Browse the repository at this point in the history
* Added support for Java 11 base image.

* Enabled spring-boot-with-yaml sample to use Java 11 base image

* Updated Documentation
  • Loading branch information
Devang Gaur authored Jul 24, 2019
1 parent 2a11dcd commit d4c586a
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 109 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ After this we will switch probably to real [Semantic Versioning 2.0.0](http://se
* Fix 1666: switchToDeployment does not work
* removed redundant profiles java11 and java9-plus
* Upgrade Fabric8 kubernetes client to v4.3.1
* Feature #1647: Added Base Image support for Java 11

### 4.1.0 (16-02-2019)
* Fix 1578: Fmp is generating ImageChange triggers in the DeploymentConfig
Expand Down
21 changes: 21 additions & 0 deletions doc/src/main/asciidoc/inc/goals/build/_fabric8-build.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ If the mode is set to `kubernetes` then a normal Docker build is performed. The

In order to make the generated images available to the Kubernetes cluster the generated images need to be pushed to a registry with the goal <<fabric8:push>>. This is not necessary for single node clusters, though as there is no need to distribute images.

Currently, Apart from Java 8, base images for Java 11 are supported for Kubernetes build. To enable Java 11 base images,
user needs to specift release version 11 in their `maven-compiler-plugin` configuration in their project pom.

.Example for Enabling Java 11 base image.
[source,xml]
----
<plugins>
....
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
----

Refer to the https://github.com/fabric8io/fabric8-maven-plugin/tree/master/samples/spring-boot-with-yaml[spring-boot-with-yaml] sample for a demo.

[[build-openshift]]
=== OpenShift Build

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@
*/
package io.fabric8.maven.generator.api;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import io.fabric8.maven.core.config.OpenShiftBuildStrategy;
import io.fabric8.maven.core.config.RuntimeMode;
import io.fabric8.maven.core.util.Configs;
import io.fabric8.maven.core.util.MavenUtil;
import org.apache.maven.model.Plugin;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;

import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.regex.Pattern;

import static io.fabric8.maven.core.config.OpenShiftBuildStrategy.SourceStrategy.kind;
import static io.fabric8.maven.core.config.OpenShiftBuildStrategy.SourceStrategy.name;
Expand All @@ -40,6 +45,8 @@ public abstract class FromSelector {

private final Pattern REDHAT_VERSION_PATTERN = Pattern.compile("^.*\\.(redhat|fuse)-.*$");

private Plugin plugin;
private Plugin compilerPlugin;
public FromSelector(GeneratorContext context) {
this.context = context;
}
Expand Down Expand Up @@ -68,7 +75,8 @@ public Map<String, String> getImageStreamTagFromExt() {

public boolean isRedHat() {
MavenProject project = context.getProject();
Plugin plugin = project.getPlugin("io.fabric8:fabric8-maven-plugin");

plugin = MavenUtil.getPlugin(project, "io.fabric8", "fabric8-maven-plugin");
if (plugin == null) {
// This plugin might be repackaged.
plugin = project.getPlugin("org.jboss.redhat-fuse:fabric8-maven-plugin");
Expand All @@ -81,6 +89,46 @@ public boolean isRedHat() {
return REDHAT_VERSION_PATTERN.matcher(version).matches();
}

public boolean isJava11() {
MavenProject project = context.getProject();
compilerPlugin = MavenUtil.getPlugin(project, "org.apache.maven.plugins", "maven-compiler-plugin");

if (compilerPlugin == null) {
return false;
}

Xpp3Dom dom = (Xpp3Dom)compilerPlugin.getConfiguration();

String releaseVersion = "8";
if (dom != null && dom.getChild("release") != null) {
releaseVersion = dom.getChild("release").getValue();
}

Properties properties = project.getProperties();

if (isEleven(releaseVersion)) {
return true;
}

try {
releaseVersion = (String) properties.entrySet()
.stream().filter(entry -> entry.getKey().equals("maven.compiler.release")).findFirst()
.get().getValue();
} catch (NoSuchElementException e) {
return false;
}

if (isEleven(releaseVersion)) {
return true;
}

return false;
}

private boolean isEleven(String s) {
return Configs.asInt(s) == 11;
}

public static class Default extends FromSelector {

private final String upstreamDocker;
Expand All @@ -94,7 +142,10 @@ public Default(GeneratorContext context, String prefix) {
super(context);
DefaultImageLookup lookup = new DefaultImageLookup(Default.class);

this.upstreamDocker = lookup.getImageName(prefix + ".upstream.docker");
this.upstreamDocker = prefix.equals("java") || prefix.equals("test")?
isJava11()? lookup.getImageName(prefix + ".11.upstream.docker"):
lookup.getImageName(prefix + ".8.upstream.docker"):
lookup.getImageName(prefix + ".upstream.docker");
this.upstreamS2i = lookup.getImageName(prefix + ".upstream.s2i");
this.upstreamIstag = lookup.getImageName(prefix + ".upstream.istag");

Expand All @@ -117,4 +168,4 @@ protected String getIstagFrom() {
return isRedHat() ? redhatIstag : upstreamIstag;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
# Properties for specifying the default images to use for java based
# image generators.
# The replacement values are defined in the parent pom.xml as properties
java.upstream.docker=${image.java.upstream.docker}
java.8.upstream.docker=${image.java.8.upstream.docker}
java.11.upstream.docker=${image.java.11.upstream.docker}
java.upstream.s2i=${image.java.upstream.s2i}
java.upstream.istag=${image.java.upstream.istag}
java.redhat.docker=${image.java.redhat.docker}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
*/
package io.fabric8.maven.generator.api;

import java.util.Map;

import io.fabric8.maven.core.config.OpenShiftBuildStrategy;
import io.fabric8.maven.core.config.RuntimeMode;
import io.fabric8.maven.core.config.ProcessorConfig;
import io.fabric8.maven.docker.util.Logger;
import mockit.Expectations;
import mockit.Mocked;
import org.apache.maven.model.Plugin;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import static io.fabric8.maven.core.config.OpenShiftBuildStrategy.SourceStrategy;
import static io.fabric8.maven.core.config.OpenShiftBuildStrategy.docker;
import static io.fabric8.maven.core.config.OpenShiftBuildStrategy.s2i;
Expand All @@ -43,10 +45,7 @@ public class FromSelectorTest {
MavenProject project;

@Mocked
Plugin plugin;

@Mocked
Logger logger;
GeneratorContext ctx;

@Test
public void simple() {
Expand All @@ -56,37 +55,25 @@ public void simple() {
openshift, s2i, "1.2.3.fuse-00009", "redhat-s2i-prop", "redhat-istag-prop",
openshift, docker, "1.2.3.fuse-00009", "redhat-docker-prop", "redhat-istag-prop",
openshift, s2i, "1.2.3.foo-00009", "s2i-prop", "istag-prop",
openshift, docker, "1.2.3.foo-00009", "docker-prop", "istag-prop",
openshift, docker, "1.2.3.foo-00009", "docker-prop-8", "istag-prop",
openshift, s2i, "1.2.3", "s2i-prop", "istag-prop",
openshift, docker, "1.2.3", "docker-prop", "istag-prop",
openshift, docker, "1.2.3", "docker-prop-8", "istag-prop",
null, s2i, "1.2.3.redhat-00009", "redhat-docker-prop", "redhat-istag-prop",
null, docker, "1.2.3.redhat-00009", "redhat-docker-prop", "redhat-istag-prop",
null, s2i, "1.2.3.fuse-00009", "redhat-docker-prop", "redhat-istag-prop",
null, docker, "1.2.3.fuse-00009", "redhat-docker-prop", "redhat-istag-prop",
null, s2i, "1.2.3.foo-00009", "docker-prop", "istag-prop",
null, docker, "1.2.3.foo-00009", "docker-prop", "istag-prop",
null, s2i, "1.2.3", "docker-prop", "istag-prop",
null, docker, "1.2.3", "docker-prop", "istag-prop",
null, s2i, "1.2.3.foo-00009", "docker-prop-8", "istag-prop",
null, docker, "1.2.3.foo-00009", "docker-prop-8", "istag-prop",
null, s2i, "1.2.3", "docker-prop-8", "istag-prop",
null, docker, "1.2.3", "docker-prop-8", "istag-prop",
openshift, null, "1.2.3.redhat-00009", "redhat-docker-prop", "redhat-istag-prop",
openshift, null, "1.2.3.fuse-00009", "redhat-docker-prop", "redhat-istag-prop",
openshift, null, "1.2.3.foo-00009", "docker-prop", "istag-prop",
openshift, null, "1.2.3", "docker-prop", "istag-prop"
openshift, null, "1.2.3.foo-00009", "docker-prop-8", "istag-prop",
openshift, null, "1.2.3", "docker-prop-8", "istag-prop"
};

for (int i = 0; i < data.length; i += 5) {
GeneratorContext ctx = new GeneratorContext.Builder()
.project(project)
.config(new ProcessorConfig())
.logger(logger)
.runtimeMode((RuntimeMode) data[i])
.strategy((OpenShiftBuildStrategy) data[i + 1])
.build();

final String version = (String) data[i + 2];
new Expectations() {{
plugin.getVersion(); result = version;
}};

prepareExpectionJava8((String) data[i + 2], (RuntimeMode) data[i], (OpenShiftBuildStrategy) data[i + 1]);
FromSelector selector = new FromSelector.Default(ctx, "test");

assertEquals(data[i + 3], selector.getFrom());
Expand All @@ -98,4 +85,64 @@ public void simple() {
}
}

}
@Test
public void simpleJava11() {
final Object[] data = new Object[] {
openshift, docker, "1.2.3.foo-00009", "docker-prop-11",
openshift, docker, "1.2.3", "docker-prop-11",
null, s2i, "1.2.3.foo-00009", "docker-prop-11",
null, docker, "1.2.3.foo-00009", "docker-prop-11",
openshift, null, "1.2.3.foo-00009", "docker-prop-11",
openshift, null, "1.2.3", "docker-prop-11",
};

for (int i = 0; i < data.length; i += 4) {
prepareExpectionJava11((String) data[i + 2], (RuntimeMode) data[i], (OpenShiftBuildStrategy) data[i + 1]);
FromSelector selector = new FromSelector.Default(ctx, "test");

assertEquals(data[i + 3], selector.getFrom());
}
}

private Expectations prepareExpectionJava8(final String version, final RuntimeMode mode, final OpenShiftBuildStrategy strategy) {
return prepareExpectation(version, mode, strategy, String.valueOf(8));
}

private Expectations prepareExpectionJava11(final String version, final RuntimeMode mode, final OpenShiftBuildStrategy strategy) {
return prepareExpectation(version, mode, strategy, String.valueOf(11));
}

private Expectations prepareExpectation(final String version, final RuntimeMode mode, final OpenShiftBuildStrategy strategy, final String javaVersion) {
return new Expectations() {{
ctx.getProject(); result = project;

Xpp3Dom child = new Xpp3Dom("release");
child.setValue(javaVersion);
Xpp3Dom dom = new Xpp3Dom("configuration");
dom.addChild(child);

Plugin plugin1 = new Plugin();
plugin1.setArtifactId("maven-compiler-plugin");
plugin1.setGroupId("org.apache.maven.plugins");
plugin1.setConfiguration(dom);

Plugin plugin2 = new Plugin();
plugin2.setArtifactId("fabric8-maven-plugin");
plugin2.setGroupId("io.fabric8");
plugin2.setVersion(version);

List<Plugin> plugins = new ArrayList<>();
plugins.add(plugin1);
plugins.add(plugin2);

project.getBuildPlugins();
result = plugins;

project.getProperties();
result = new Properties();

ctx.getRuntimeMode();result = mode;
ctx.getStrategy(); result = strategy;
}};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
# permissions and limitations under the License.
#

test.upstream.docker=docker-prop
test.8.upstream.docker=docker-prop-8
test.11.upstream.docker=docker-prop-11
test.upstream.s2i=s2i-prop
test.upstream.istag=istag-prop
test.redhat.docker=redhat-docker-prop
Expand Down
Loading

0 comments on commit d4c586a

Please sign in to comment.