Skip to content

Commit

Permalink
feat(fabric8io#601): Adds webapp healthckech (fabric8io#1456)
Browse files Browse the repository at this point in the history
  • Loading branch information
lordofthejars committed Jan 16, 2019
1 parent 31f21b3 commit 802861b
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ We use semantic versioning in some slight variation until our feature set has st

After this we will switch probably to real [Semantic Versioning 2.0.0](http://semver.org/)

### 4.0.0-M3-SNAPSHOT

* Feature 601: Adds healthcheck for webapp

### 4.0.0-M2 (2018-12-14)
* Fix 10: Make VolumeConfiguration more flexible
* Fix 1326: Fixes overridding of Selector Label by the project enrichers entries.
Expand Down
47 changes: 46 additions & 1 deletion doc/src/main/asciidoc/inc/_enricher.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,52 @@ items:
By default the enricher inspects the images' BuildConfiguration and add the annotations if the port 9779 is listed.
You can force the plugin to add annotations by setting enricher's config ```prometheusPort```

[[f8-healthcheck-webapp]]
==== f8-healthcheck-webapp

This enricher adds kubernetes readiness and liveness probes with WebApp. This requires that you have `maven-war-plugin` set.

The enricher will use the following settings by default:

- port = `8080`
- scheme = `HTTP`
- path = ``
- initialReadinessDelay = 10
- initialLivenessDelay = 180

If `path` attribute is not set (default value) then this enricher is disabled.

These values can be configured by the enricher in the `fabric8-maven-plugin` configuration as shown below:

[source,xml]
----
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>fmp</id>
<goals>
<goal>resource</goal>
<goal>helm</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<enricher>
<config>
<f8-healthcheck-webapp>
<path>/</path>
</f8-healthcheck-webapp>
</config>
</enricher>
</configuration>
...
</plugin>
----

[[f8-healthcheck-spring-boot]]
==== f8-healthcheck-spring-boot

Expand Down Expand Up @@ -429,7 +475,6 @@ These values can be configured by the enricher in the `fabric8-maven-plugin` con
</configuration>
</plugin>


[[f8-healthcheck-vertx]]
==== f8-healthcheck-vertx

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Copyright 2016 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.fabric8.maven.enricher.fabric8;

import io.fabric8.kubernetes.api.model.Probe;
import io.fabric8.kubernetes.api.model.ProbeBuilder;
import io.fabric8.maven.core.util.Configs;
import io.fabric8.maven.enricher.api.MavenEnricherContext;
import org.apache.commons.lang3.StringUtils;

public class WebAppHealthCheckEnricher extends AbstractHealthCheckEnricher {

public WebAppHealthCheckEnricher(MavenEnricherContext buildContext) {
super(buildContext, "f8-healthcheck-webapp");
}

// Available configuration keys
private enum Config implements Configs.Key {

scheme {{
d = "HTTP";
}},
port {{
d = "8080";
}},
path {{
d = "";
}},
initialReadinessDelay {{
d = "10";
}},
initialLivenessDelay {{
d = "180";
}};

protected String d;

public String def() {
return d;
}
}

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

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

private boolean isApplicable() {
return getContext()
.hasPlugin("org.apache.maven.plugins", "maven-war-plugin") &&
StringUtils.isNotEmpty(Configs.asString(getConfig(Config.path)));
}

private Probe getProbe(boolean readiness) {
if (!isApplicable()) {
return null;
}

Integer port = getPort();
String scheme = getScheme().toUpperCase();
String path = getPath();

int delay = readiness ? getInitialReadinessDelay() : getInitialLivenessDelay();

return new ProbeBuilder().
withNewHttpGet().withNewPort(port).withPath(path).withScheme(scheme).endHttpGet().
withInitialDelaySeconds(delay).build();

}

private int getInitialReadinessDelay() {
return Configs.asInt(getConfig(Config.initialReadinessDelay));
}

private int getInitialLivenessDelay() {
return Configs.asInt(getConfig(Config.initialLivenessDelay));
}

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

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

protected String getPath() {
return Configs.asString(getConfig(Config.path));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ io.fabric8.maven.enricher.fabric8.WildFlySwarmHealthCheckEnricher
io.fabric8.maven.enricher.fabric8.ThorntailV2HealthCheckEnricher
io.fabric8.maven.enricher.fabric8.KarafHealthCheckEnricher
io.fabric8.maven.enricher.fabric8.VertxHealthCheckEnricher
io.fabric8.maven.enricher.fabric8.WebAppHealthCheckEnricher
io.fabric8.maven.enricher.fabric8.DockerHealthCheckEnricher,510

# Other enrichers
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* Copyright 2016 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.fabric8.maven.enricher.fabric8;

import io.fabric8.kubernetes.api.model.Probe;
import io.fabric8.maven.core.model.Configuration;
import io.fabric8.maven.enricher.api.MavenEnricherContext;
import io.fabric8.maven.enricher.api.util.MavenConfigurationExtractor;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import mockit.Expectations;
import mockit.Mocked;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class WebAppHealthCeckEnricherTest {

@Mocked
private MavenEnricherContext context;

private void setupExpectations(Map<String, Object> config) {
new Expectations() {{
context.hasPlugin("org.apache.maven.plugins", "maven-war-plugin");
result = true;

Configuration.Builder configBuilder = new Configuration.Builder();
configBuilder.pluginConfigLookup(getProjectLookup(config));
}};
}

@Test
public void noEnrichmentIfNoPath() {

// given

WebAppHealthCheckEnricher enricher = new WebAppHealthCheckEnricher(context);
setupExpectations(new HashMap<>());

// when

Probe probeLiveness = enricher.getLivenessProbe();
Probe probeReadiness = enricher.getReadinessProbe();

// then
assertThat(probeLiveness).isNull();
assertThat(probeReadiness).isNull();
}

@Test
public void enrichmentWithDefaultsIfPath() {

// given

final Map<String, Object> config = createFakeConfig(
"<path>/health</path>");
setupExpectations(config);

WebAppHealthCheckEnricher enricher = new WebAppHealthCheckEnricher(context);

// when

Probe probeLiveness = enricher.getLivenessProbe();
Probe probeReadiness = enricher.getReadinessProbe();

// then
assertThat(probeLiveness).isNull();
assertThat(probeReadiness).isNull();
}

private BiFunction<String, String, Optional<Map<String, Object>>> getProjectLookup(Map<String, Object> config) {
return (s,i) -> {
assertThat(s).isEqualTo("maven");
assertThat(i).isEqualTo("io.fabric8:fabric8-maven-plugin");
return Optional.ofNullable(config);
};
}

private Map<String, Object> createFakeConfig(String config) {

String content = "<configuration><enricher><config><f8-healthcheck-webapp>"
+ config
+ "</f8-healthcheck-webapp></config></enricher></configuration>";
Xpp3Dom dom;
try {
dom = Xpp3DomBuilder.build(new StringReader(content));
} catch (Exception e) {
throw new RuntimeException(e);
}

return MavenConfigurationExtractor.extract(dom);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
- f8-healthcheck-karaf
- f8-healthcheck-vertx
- f8-healthcheck-docker
- f8-healthcheck-webapp
- f8-prometheus
# Dependencies shouldn't be enriched anymore, therefor it's last in the list
- fmp-dependency
Expand Down

0 comments on commit 802861b

Please sign in to comment.