Skip to content

Commit

Permalink
Fix for issue eclipse-jkube#283, Add support for WildFly Bootable JAR
Browse files Browse the repository at this point in the history
Signed-off-by: JF Denise <[email protected]>
  • Loading branch information
jfdenise committed Jul 17, 2020
1 parent 8555e05 commit eb00a85
Show file tree
Hide file tree
Showing 20 changed files with 907 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public WildFlyAppSeverHandler(GeneratorContext context) {
@Override
public boolean isApplicable() {
try {
return isNotWildflySwarm() && isNotThorntail() && (isWildFlyWebApp() || hasWildFlyPlugin());
return isNotWildflySwarm() && isNotThorntail() && (isWildFlyWebApp() || hasWildFlyPlugin()) && isNotWildFlyJAR();
} catch (IOException exception) {
throw new IllegalStateException("Unable to scan output directory: ", exception);
}
Expand Down Expand Up @@ -78,6 +78,10 @@ private boolean isNotWildflySwarm() {
private boolean isNotThorntail() {
return !JKubeProjectUtil.hasPlugin(getProject(), "io.thorntail", "thorntail-maven-plugin");
}

private boolean isNotWildFlyJAR() {
return !JKubeProjectUtil.hasPlugin(getProject(), "org.wildfly.plugins", "wildfly-jar-maven-plugin");
}

@Override
public String getFrom() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void testIsWildFly() throws Exception {
"org.wildfly.plugins:wildfly-maven-plugin", true,
"org.wildfly.swarm:wildfly-swarm-plugin", false,
"io.thorntail:thorntail-maven-plugin", false,
"org.wildfly.plugins:wildfly-jar-maven-plugin", false,
};

assertAppServerDescriptorApplicability(descriptorNames);
Expand Down
64 changes: 64 additions & 0 deletions jkube-kit/jkube-kit-wildfly-jar/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2020 Red Hat, Inc.
This program and the accompanying materials are made
available under the terms of the Eclipse Public License 2.0
which is available at:
https://www.eclipse.org/legal/epl-2.0/
SPDX-License-Identifier: EPL-2.0
Contributors:
Red Hat, Inc. - initial API and implementation
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<groupId>org.eclipse.jkube</groupId>
<artifactId>jkube-kit-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>jkube-kit-wildfly-jar</artifactId>
<name>JKube Kit :: WildFly JAR</name>

<dependencies>
<dependency>
<groupId>org.eclipse.jkube</groupId>
<artifactId>jkube-kit-common-maven</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.jkube</groupId>
<artifactId>jkube-kit-generator-java-exec</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.jkube</groupId>
<artifactId>jkube-kit-enricher-specific</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/**
* Copyright (c) 2020 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.wildfly.jar.enricher;

import io.fabric8.kubernetes.api.builder.TypedVisitor;
import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.kubernetes.api.model.Probe;
import io.fabric8.kubernetes.api.model.ProbeBuilder;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.eclipse.jkube.kit.common.Configs;
import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext;
import org.eclipse.jkube.kit.enricher.specific.AbstractHealthCheckEnricher;

import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.Plugin;
import org.eclipse.jkube.kit.common.util.JKubeProjectUtil;
import org.eclipse.jkube.kit.config.resource.PlatformMode;

/**
* Enriches wildfly-jar containers with health checks if the bootable JAR has
* been configured for cloud.
*/
public class WildflyJARHealthCheckEnricher extends AbstractHealthCheckEnricher {

public static final String BOOTABLE_JAR_GROUP_ID = "org.wildfly.plugins";
public static final String BOOTABLE_JAR_ARTIFACT_ID = "wildfly-jar-maven-plugin";

public WildflyJARHealthCheckEnricher(JKubeEnricherContext buildContext) {
super(buildContext, "jkube-healthcheck-wildfly-jar");
}

@AllArgsConstructor
private enum Config implements Configs.Config {

SCHEME("scheme", "HTTP"),
PORT("port", "9990"),
FAILURETHRESHOLD("failureThreshold","3"),
SUCCESSTHRESHOLD("successThreshold","1"),
LIVENESSINITIALDELAY("livenessInitialDelay", "60"),
READINESSINITIALDELAY("readinessInitialDelay", "10"),
READINESSPATH("readinessPath", "/health/ready"),
LIVENESSPATH("livenessPath", "/health/live"),
ENFORCEPROBES("enforceProbes","false");

@Getter
protected String key;
@Getter
protected String defaultValue;
}

@Override
protected Probe getReadinessProbe() {
return discoverWildflyJARHealthCheck(true);
}

@Override
protected Probe getLivenessProbe() {
return discoverWildflyJARHealthCheck(false);
}

@Override
public void create(PlatformMode platformMode, KubernetesListBuilder builder) {
// Add HOSTNAME
if (PlatformMode.kubernetes.equals(platformMode)) {
final List<ContainerBuilder> containerBuilders = new LinkedList<>();
builder.accept(new TypedVisitor<ContainerBuilder>() {
@Override
public void visit(ContainerBuilder containerBuilder) {
containerBuilders.add(containerBuilder);
}
});
for (ContainerBuilder container : containerBuilders) {
container.addToEnv(new EnvVarBuilder()
.withName("HOSTNAME")
.withNewValueFrom()
.withNewFieldRef()
.withFieldPath("metadata.name")
.endFieldRef()
.endValueFrom()
.build());
}
}
super.create(platformMode, builder);
}

private Probe discoverWildflyJARHealthCheck(boolean isReadiness) {

if (isAvailable()) {
Integer port = getPort();
if (port <= 0) {
return null;
}
int initialDelay = isReadiness ? getReadinessInitialDelay() : getLivenessInitialDelay();
// scheme must be in upper case in k8s
String scheme = getScheme().toUpperCase();
String path = isReadiness ? getReadinessPath() : getLivenessPath();

return new ProbeBuilder()
.withNewHttpGet().withNewPort(port).withPath(path).withScheme(scheme).endHttpGet()
.withFailureThreshold(getFailureThreshold())
.withSuccessThreshold(getSuccessThreshold())
.withInitialDelaySeconds(initialDelay).build();
}
return null;
}

private boolean isAvailable() {
if (isProbeEnforced()) {
return true;
}
JavaProject project = ((JKubeEnricherContext) getContext()).getProject();
Plugin plugin = JKubeProjectUtil.getPlugin(project, BOOTABLE_JAR_GROUP_ID, BOOTABLE_JAR_ARTIFACT_ID);
if (plugin == null) {
return false;
}
Map<String, Object> config = plugin.getConfiguration();
return config.containsKey("cloud");
}

protected int getFailureThreshold() {
return Configs.asInteger(getConfig(Config.FAILURETHRESHOLD));
}

protected boolean isProbeEnforced() {
return Configs.asBoolean(getConfig(Config.ENFORCEPROBES));
}

protected int getSuccessThreshold() {
return Configs.asInteger(getConfig(Config.SUCCESSTHRESHOLD));
}

protected String getScheme() {
return Configs.asString(getConfig(Config.SCHEME));
}

protected int getPort() {
return Configs.asInt(getConfig(Config.PORT));
}

protected String getLivenessPath() {
return Configs.asString(getConfig(Config.LIVENESSPATH));
}

protected int getLivenessInitialDelay() {
return Configs.asInt(getConfig(Config.LIVENESSINITIALDELAY));
}

protected String getReadinessPath() {
return Configs.asString(getConfig(Config.READINESSPATH));
}

protected int getReadinessInitialDelay() {
return Configs.asInt(getConfig(Config.READINESSINITIALDELAY));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright (c) 2020 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.wildfly.jar.generator;

import org.eclipse.jkube.generator.api.GeneratorContext;
import org.eclipse.jkube.generator.javaexec.JavaExecGenerator;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
import org.eclipse.jkube.kit.common.util.JKubeProjectUtil;

import java.util.List;
import java.util.Map;
import org.eclipse.jkube.wildfly.jar.enricher.WildflyJARHealthCheckEnricher;

public class WildflyJARGenerator extends JavaExecGenerator {

public WildflyJARGenerator(GeneratorContext context) {
super(context, "wildfly-jar");
}

@Override
public boolean isApplicable(List<ImageConfiguration> configs) {
return shouldAddGeneratedImageConfiguration(configs)
&& JKubeProjectUtil.hasPlugin(getProject(),
WildflyJARHealthCheckEnricher.BOOTABLE_JAR_GROUP_ID, WildflyJARHealthCheckEnricher.BOOTABLE_JAR_ARTIFACT_ID);
}

@Override
protected Map<String, String> getEnv(boolean isPrepackagePhase) {
Map<String, String> ret = super.getEnv(isPrepackagePhase);
// Switch off Prometheus agent until logging issue with WildFly Swarm is resolved
// See:
// - https://github.com/fabric8io/fabric8-maven-plugin/issues/1173
// - https://issues.jboss.org/browse/THORN-1859
ret.put("AB_PROMETHEUS_OFF", "true");
ret.put("AB_OFF", "true");

// In addition, there is no proper fix in Jolokia to detect that the Bootable JAR is started.
// Can be workarounded by setting JAVA_OPTIONS to contain -Djboss.modules.system.pkgs=org.jboss.byteman
ret.put("AB_JOLOKIA_OFF", "true");
return ret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Healthcheck Enrichers
org.eclipse.jkube.wildfly.jar.enricher.WildflyJARHealthCheckEnricher
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# The order of the generators used is defined in the active profile
# (which is the profile "default" by default)
# You can find the default profiles in "profiles-default.yml"

org.eclipse.jkube.wildfly.jar.generator.WildflyJARGenerator
Loading

0 comments on commit eb00a85

Please sign in to comment.