forked from eclipse-jkube/jkube
-
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 (jkube-kit) : Add HelidonHealthCheckEnricher to add Kubernetes h…
…ealth checks for Helidon applications (eclipse-jkube#1713) + Add HelidonHealthCheckEnricher to automatically add liveness, readiness and start probes based on already existing microprofile health check enricher Signed-off-by: Rohan Kumar <rohaan@redhat.com>
- Loading branch information
1 parent
355a982
commit 1720ee0
Showing
11 changed files
with
295 additions
and
0 deletions.
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
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
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
39 changes: 39 additions & 0 deletions
39
...-helidon/src/main/java/org/eclipse/jkube/helidon/enricher/HelidonHealthCheckEnricher.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,39 @@ | ||
/** | ||
* Copyright (c) 2019 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.helidon.enricher; | ||
|
||
import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext; | ||
import org.eclipse.jkube.microprofile.enricher.AbstractMicroprofileHealthCheckEnricher; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.eclipse.jkube.helidon.HelidonUtils.hasHelidonHealthDependency; | ||
import static org.eclipse.jkube.kit.common.Configs.asInteger; | ||
|
||
public class HelidonHealthCheckEnricher extends AbstractMicroprofileHealthCheckEnricher { | ||
private static final String DEFAULT_HELIDON_PORT = "8080"; | ||
public HelidonHealthCheckEnricher(JKubeEnricherContext buildContext) { | ||
super(buildContext, "jkube-healthcheck-helidon"); | ||
} | ||
|
||
@Override | ||
protected boolean shouldAddProbe() { | ||
return hasHelidonHealthDependency(getContext().getProject()); | ||
} | ||
|
||
@Override | ||
protected int getPort() { | ||
return asInteger(Optional.ofNullable(getPortFromConfiguration()).orElse(DEFAULT_HELIDON_PORT)); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
jkube-kit/jkube-kit-helidon/src/main/resources/META-INF/jkube/enricher-default
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 @@ | ||
org.eclipse.jkube.helidon.enricher.HelidonHealthCheckEnricher |
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
217 changes: 217 additions & 0 deletions
217
...idon/src/test/java/org/eclipse/jkube/helidon/enricher/HelidonHealthCheckEnricherTest.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,217 @@ | ||
/** | ||
* Copyright (c) 2019 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.helidon.enricher; | ||
|
||
import io.fabric8.kubernetes.api.model.Container; | ||
import io.fabric8.kubernetes.api.model.KubernetesListBuilder; | ||
import io.fabric8.kubernetes.api.model.Probe; | ||
import io.fabric8.kubernetes.api.model.apps.Deployment; | ||
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; | ||
import org.assertj.core.api.AssertionsForInterfaceTypes; | ||
import org.eclipse.jkube.helidon.HelidonUtils; | ||
import org.eclipse.jkube.kit.common.Dependency; | ||
import org.eclipse.jkube.kit.common.JavaProject; | ||
import org.eclipse.jkube.kit.config.resource.PlatformMode; | ||
import org.eclipse.jkube.kit.config.resource.ProcessorConfig; | ||
import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
|
||
import java.io.File; | ||
import java.util.Collections; | ||
import java.util.Properties; | ||
import java.util.function.Supplier; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.mockito.Answers.RETURNS_DEEP_STUBS; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class HelidonHealthCheckEnricherTest { | ||
private JKubeEnricherContext context; | ||
private JavaProject javaProject; | ||
private Properties properties; | ||
private KubernetesListBuilder klb; | ||
|
||
@BeforeEach | ||
void setup() { | ||
properties = new Properties(); | ||
ProcessorConfig processorConfig = new ProcessorConfig(); | ||
klb = new KubernetesListBuilder(); | ||
klb.addToItems(new DeploymentBuilder() | ||
.editOrNewSpec() | ||
.editOrNewTemplate() | ||
.editOrNewMetadata() | ||
.withName("template-name") | ||
.endMetadata() | ||
.editOrNewSpec() | ||
.addNewContainer() | ||
.withImage("container/image") | ||
.endContainer() | ||
.endSpec() | ||
.endTemplate() | ||
.endSpec() | ||
.build()); | ||
|
||
context = mock(JKubeEnricherContext.class, RETURNS_DEEP_STUBS); | ||
javaProject = mock(JavaProject.class, RETURNS_DEEP_STUBS); | ||
when(context.getProject()).thenReturn(javaProject); | ||
when(context.getProperties()).thenReturn(properties); | ||
when(context.getConfiguration().getProcessorConfig()).thenReturn(processorConfig); | ||
when(javaProject.getProperties()).thenReturn(properties); | ||
when(javaProject.getBaseDirectory()).thenReturn(new File("/tmp/ignore")); | ||
when(javaProject.getOutputDirectory()).thenReturn(new File("/tmp/ignore")); | ||
} | ||
|
||
@Test | ||
void create_withNoMicroprofileDependency_shouldNotAddProbes() { | ||
// Given | ||
HelidonHealthCheckEnricher helidonHealthCheckEnricher = new HelidonHealthCheckEnricher(context); | ||
|
||
// When | ||
helidonHealthCheckEnricher.create(PlatformMode.kubernetes, klb); | ||
|
||
// Then | ||
assertNoProbesAdded(); | ||
} | ||
|
||
@Test | ||
void create_withMicroprofileDependencyAndMicroprofileFeatureDisabled_shouldNotAddProbes() { | ||
// Given | ||
withMicroprofileDependency("5.0"); | ||
HelidonHealthCheckEnricher helidonHealthCheckEnricher = new HelidonHealthCheckEnricher(context); | ||
|
||
// When | ||
helidonHealthCheckEnricher.create(PlatformMode.kubernetes, klb); | ||
|
||
// Then | ||
assertNoProbesAdded(); | ||
} | ||
|
||
@Test | ||
void create_withMicroprofileDependencyAndMicroprofileFeatureEnabled_shouldAddProbes() { | ||
try (MockedStatic<HelidonUtils> mockStatic = Mockito.mockStatic(HelidonUtils.class)) { | ||
// Given | ||
mockStatic.when(() -> HelidonUtils.hasHelidonHealthDependency(javaProject)).thenReturn(true); | ||
withMicroprofileDependency("5.0"); | ||
HelidonHealthCheckEnricher helidonHealthCheckEnricher = new HelidonHealthCheckEnricher(context); | ||
// When | ||
helidonHealthCheckEnricher.create(PlatformMode.kubernetes, klb); | ||
// Then | ||
assertProbesAdded("HTTP", "/health/live", "HTTP", "/health/ready", "HTTP", "/health/started"); | ||
} | ||
} | ||
|
||
@Test | ||
void create_withLegacyMicroprofileDependencyAndMicroprofileFeatureEnabled_shouldOnlyAddLivenessReadinessProbes() { | ||
try (MockedStatic<HelidonUtils> mockStatic = Mockito.mockStatic(HelidonUtils.class)) { | ||
// Given | ||
mockStatic.when(() -> HelidonUtils.hasHelidonHealthDependency(javaProject)).thenReturn(true); | ||
withMicroprofileDependency("2.2"); | ||
HelidonHealthCheckEnricher helidonHealthCheckEnricher = new HelidonHealthCheckEnricher(context); | ||
// When | ||
helidonHealthCheckEnricher.create(PlatformMode.kubernetes, klb); | ||
// Then | ||
assertProbesAdded("HTTP", "/health/live", "HTTP", "/health/ready", null, null); | ||
assertThat(getFirstContainerFromDeployment()) | ||
.extracting(Container::getStartupProbe) | ||
.isNull(); | ||
} | ||
} | ||
|
||
@Test | ||
void create_withMicroprofileEnabledAndEnricherConfiguration_shouldProbesAsConfigured() { | ||
try (MockedStatic<HelidonUtils> mockStatic = Mockito.mockStatic(HelidonUtils.class)) { | ||
// Given | ||
mockStatic.when(() -> HelidonUtils.hasHelidonHealthDependency(javaProject)).thenReturn(true); | ||
withMicroprofileDependency("5.0"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.scheme", "HTTPS"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.port", "8080"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.livenessPath", "/custom/health/live"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.livenessFailureThreshold", "5"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.livenessSuccessThreshold", "5"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.livenessInitialDelay", "5"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.livenessPeriodSeconds", "5"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.readinessPath", "/custom/health/ready"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.readinessFailureThreshold", "5"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.readinessSuccessThreshold", "5"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.readinessInitialDelay", "5"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.readinessPeriodSeconds", "5"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.startupPath", "/custom/health/startup"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.startupFailureThreshold", "5"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.startupSuccessThreshold", "5"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.startupInitialDelay", "5"); | ||
properties.put("jkube.enricher.jkube-healthcheck-helidon.startupPeriodSeconds", "5"); | ||
HelidonHealthCheckEnricher helidonHealthCheckEnricher = new HelidonHealthCheckEnricher(context); | ||
// When | ||
helidonHealthCheckEnricher.create(PlatformMode.kubernetes, klb); | ||
// Then | ||
assertProbesAdded("HTTPS", "/custom/health/live", "HTTPS", "/custom/health/ready", "HTTPS", "/custom/health/startup"); | ||
assertThat(getFirstContainerFromDeployment()) | ||
.hasFieldOrPropertyWithValue("livenessProbe.failureThreshold", 5) | ||
.hasFieldOrPropertyWithValue("livenessProbe.successThreshold", 5) | ||
.hasFieldOrPropertyWithValue("livenessProbe.initialDelaySeconds", 5) | ||
.hasFieldOrPropertyWithValue("livenessProbe.periodSeconds", 5) | ||
.hasFieldOrPropertyWithValue("livenessProbe.httpGet.port.IntVal", 8080) | ||
.hasFieldOrPropertyWithValue("readinessProbe.failureThreshold", 5) | ||
.hasFieldOrPropertyWithValue("readinessProbe.successThreshold", 5) | ||
.hasFieldOrPropertyWithValue("readinessProbe.initialDelaySeconds", 5) | ||
.hasFieldOrPropertyWithValue("readinessProbe.periodSeconds", 5) | ||
.hasFieldOrPropertyWithValue("readinessProbe.httpGet.port.IntVal", 8080) | ||
.hasFieldOrPropertyWithValue("startupProbe.failureThreshold", 5) | ||
.hasFieldOrPropertyWithValue("startupProbe.successThreshold", 5) | ||
.hasFieldOrPropertyWithValue("startupProbe.initialDelaySeconds", 5) | ||
.hasFieldOrPropertyWithValue("startupProbe.periodSeconds", 5) | ||
.hasFieldOrPropertyWithValue("startupProbe.httpGet.port.IntVal", 8080); | ||
} | ||
} | ||
|
||
private void withMicroprofileDependency(String microProfileVersion) { | ||
when(javaProject.getDependenciesWithTransitive()).thenReturn(Collections.singletonList(Dependency.builder() | ||
.groupId("org.eclipse.microprofile") | ||
.artifactId("microprofile") | ||
.version(microProfileVersion) | ||
.build())); | ||
} | ||
|
||
private void assertProbesAdded(String livenessScheme, String livenessPath, String readyScheme, String readyPath, String startedScheme, String startedPath) { | ||
Container container = getFirstContainerFromDeployment(); | ||
assertProbe(container::getLivenessProbe, livenessScheme, livenessPath); | ||
assertProbe(container::getReadinessProbe, readyScheme, readyPath); | ||
assertProbe(container::getStartupProbe, startedScheme, startedPath); | ||
} | ||
|
||
private void assertNoProbesAdded() { | ||
Container container = getFirstContainerFromDeployment(); | ||
assertThat(container.getLivenessProbe()).isNull(); | ||
assertThat(container.getReadinessProbe()).isNull(); | ||
assertThat(container.getStartupProbe()).isNull(); | ||
} | ||
|
||
private void assertProbe(Supplier<Probe> probeSupplier, String scheme, String path) { | ||
if (scheme != null && path != null) { | ||
assertThat(probeSupplier.get()) | ||
.hasFieldOrPropertyWithValue("httpGet.scheme", scheme) | ||
.hasFieldOrPropertyWithValue("httpGet.path", path); | ||
} | ||
} | ||
|
||
private Container getFirstContainerFromDeployment() { | ||
Deployment deployment = (Deployment) klb.buildFirstItem(); | ||
AssertionsForInterfaceTypes.assertThat(deployment.getSpec().getTemplate().getSpec().getContainers()).hasSize(1); | ||
return deployment.getSpec().getTemplate().getSpec().getContainers().get(0); | ||
} | ||
} |
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