forked from fabric8io/fabric8-maven-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fabric8io#601): Adds webapp healthckech (fabric8io#1456)
- Loading branch information
1 parent
31f21b3
commit 802861b
Showing
6 changed files
with
273 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...er/fabric8/src/main/java/io/fabric8/maven/enricher/fabric8/WebAppHealthCheckEnricher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...fabric8/src/test/java/io/fabric8/maven/enricher/fabric8/WebAppHealthCeckEnricherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters