From a8d832c28155641a65188a9eb51dbf3e01eb8ca2 Mon Sep 17 00:00:00 2001 From: Jose Date: Thu, 24 Feb 2022 14:46:13 +0100 Subject: [PATCH] Support Startup Probes for Kubernetes, Openshift and Knative The kubelet uses startup probes to know when a container application has started. If such a probe is configured, it disables liveness and readiness checks until it succeeds, making sure those probes don't interfere with the application startup. This can be used to adopt liveness checks on slow starting containers, avoiding them getting killed by the kubelet before they are up and running. ```yaml startupProbe: httpGet: path: /healthz port: liveness-port failureThreshold: 30 periodSeconds: 10 ``` More context in [kubernetes.io/startup-probes](https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/). Fix https://github.com/dekorateio/dekorate/issues/854 --- .../annotation/KnativeApplication.java | 7 +++ .../annotation/KubernetesApplication.java | 7 +++ .../annotation/OpenshiftApplication.java | 7 +++ assets/design.md | 3 +- .../AbstractKubernetesManifestGenerator.java | 6 +++ .../kubernetes/adapter/ContainerAdapter.java | 5 ++ .../dekorate/kubernetes/annotation/Base.java | 7 +++ .../kubernetes/annotation/Container.java | 7 +++ .../decorator/AddStartupProbeDecorator.java | 46 +++++++++++++++++++ ...emoveProbesFromInitContainerDecorator.java | 2 +- docs/documentation/design.md | 3 +- .../main/java/io/dekorate/example/Main.java | 8 +++- .../dekorate/example/KnativeExampleTest.java | 38 +++++++++++++++ examples/vertx-on-kubernetes-example/pom.xml | 13 +++++- .../src/main/resources/application.properties | 8 +++- .../java/io/dekorate/example/VertxTest.java | 43 ++++++++++++++++- examples/vertx-on-openshift-example/pom.xml | 13 +++++- .../src/main/resources/application.properties | 8 +++- .../java/io/dekorate/example/VertxTest.java | 43 ++++++++++++++++- 19 files changed, 264 insertions(+), 10 deletions(-) create mode 100644 core/src/main/java/io/dekorate/kubernetes/decorator/AddStartupProbeDecorator.java diff --git a/annotations/knative-annotations/src/main/java/io/dekorate/knative/annotation/KnativeApplication.java b/annotations/knative-annotations/src/main/java/io/dekorate/knative/annotation/KnativeApplication.java index 650b46918..2ef979566 100644 --- a/annotations/knative-annotations/src/main/java/io/dekorate/knative/annotation/KnativeApplication.java +++ b/annotations/knative-annotations/src/main/java/io/dekorate/knative/annotation/KnativeApplication.java @@ -234,6 +234,13 @@ */ Probe readinessProbe() default @Probe(); + /** + * The startup probe. + * + * @return The probe. + */ + Probe startupProbe() default @Probe(); + /** * The resources that the application container requires. */ diff --git a/annotations/kubernetes-annotations/src/main/java/io/dekorate/kubernetes/annotation/KubernetesApplication.java b/annotations/kubernetes-annotations/src/main/java/io/dekorate/kubernetes/annotation/KubernetesApplication.java index 8fa0bc4ba..a53a1f2bd 100644 --- a/annotations/kubernetes-annotations/src/main/java/io/dekorate/kubernetes/annotation/KubernetesApplication.java +++ b/annotations/kubernetes-annotations/src/main/java/io/dekorate/kubernetes/annotation/KubernetesApplication.java @@ -230,6 +230,13 @@ */ Probe readinessProbe() default @Probe(); + /** + * The startup probe. + * + * @return The probe. + */ + Probe startupProbe() default @Probe(); + /** * The resources that the application container requires. */ diff --git a/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/annotation/OpenshiftApplication.java b/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/annotation/OpenshiftApplication.java index 86b0f4f87..c5b6fd410 100644 --- a/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/annotation/OpenshiftApplication.java +++ b/annotations/openshift-annotations/src/main/java/io/dekorate/openshift/annotation/OpenshiftApplication.java @@ -232,6 +232,13 @@ */ Probe readinessProbe() default @Probe(); + /** + * The startup probe. + * + * @return The probe. + */ + Probe startupProbe() default @Probe(); + /** * The resources that the application container requires. */ diff --git a/assets/design.md b/assets/design.md index 72951f125..89e270fa4 100644 --- a/assets/design.md +++ b/assets/design.md @@ -134,9 +134,10 @@ The `decorator` looks pretty similar to the `configurator` the only difference b |-------------------------------|----------------|--------------------------------------------------------| | AddSecretVolume | PodSpec | Add a secret volume to all pod specs. | | AddService | KubernetesList | Add a service to the list. | -| AddLivenessProbe | Container | Add a liveness probe to all containers. | | AddEnvVar | Container | Add a environment variable to the container. | +| AddLivenessProbe | Container | Add a liveness probe to all containers. | | AddReadinessProbe | Container | Add a readiness probe to all containers. | +| AddStartupProbe | Container | Add a startup probe to all containers. | | AddConfigMapVolume | PodSpec | Add a configmap volume to the pod spec. | | AddEnvToComponent | ComponentSpec | Add environment variable to component. | | AddAzureDiskVolume | PodSpec | Add an Azure disk volume to the pod spec. | diff --git a/core/src/main/java/io/dekorate/AbstractKubernetesManifestGenerator.java b/core/src/main/java/io/dekorate/AbstractKubernetesManifestGenerator.java index 26d6e31f2..e56773478 100644 --- a/core/src/main/java/io/dekorate/AbstractKubernetesManifestGenerator.java +++ b/core/src/main/java/io/dekorate/AbstractKubernetesManifestGenerator.java @@ -45,6 +45,7 @@ import io.dekorate.kubernetes.decorator.AddReadinessProbeDecorator; import io.dekorate.kubernetes.decorator.AddSecretVolumeDecorator; import io.dekorate.kubernetes.decorator.AddSidecarDecorator; +import io.dekorate.kubernetes.decorator.AddStartupProbeDecorator; import io.dekorate.kubernetes.decorator.AddToMatchingLabelsDecorator; import io.dekorate.kubernetes.decorator.AddToSelectorDecorator; import io.dekorate.kubernetes.decorator.ApplyArgsDecorator; @@ -186,6 +187,11 @@ protected void addDecorators(String group, C config) { new AddReadinessProbeDecorator(config.getName(), config.getName(), config.getReadinessProbe())); } + if (Probes.isConfigured(config.getStartupProbe())) { + resourceRegistry.decorate(group, + new AddStartupProbeDecorator(config.getName(), config.getName(), config.getStartupProbe())); + } + //Container resources if (config.getLimitResources() != null) { if (Strings.isNotNullOrEmpty(config.getLimitResources().getCpu())) { diff --git a/core/src/main/java/io/dekorate/kubernetes/adapter/ContainerAdapter.java b/core/src/main/java/io/dekorate/kubernetes/adapter/ContainerAdapter.java index 9b673345a..6f5e02766 100644 --- a/core/src/main/java/io/dekorate/kubernetes/adapter/ContainerAdapter.java +++ b/core/src/main/java/io/dekorate/kubernetes/adapter/ContainerAdapter.java @@ -23,6 +23,7 @@ import io.dekorate.kubernetes.decorator.AddMountDecorator; import io.dekorate.kubernetes.decorator.AddPortDecorator; import io.dekorate.kubernetes.decorator.AddReadinessProbeDecorator; +import io.dekorate.kubernetes.decorator.AddStartupProbeDecorator; import io.dekorate.kubernetes.decorator.ApplyImagePullPolicyDecorator; import io.dekorate.kubernetes.decorator.ApplyLimitsCpuDecorator; import io.dekorate.kubernetes.decorator.ApplyLimitsMemoryDecorator; @@ -68,6 +69,10 @@ public static void applyContainerToBuilder(ContainerBuilder builder, io.dekorate builder.accept(new AddReadinessProbeDecorator(name, container.getReadinessProbe())); } + if (Probes.isConfigured(container.getStartupProbe())) { + builder.accept(new AddStartupProbeDecorator(name, container.getStartupProbe())); + } + // Container resources if (container.getLimitResources() != null && Strings.isNotNullOrEmpty(container.getLimitResources().getCpu())) { builder.accept(new ApplyLimitsCpuDecorator(name, container.getLimitResources().getCpu())); diff --git a/core/src/main/java/io/dekorate/kubernetes/annotation/Base.java b/core/src/main/java/io/dekorate/kubernetes/annotation/Base.java index 040eec82a..4f1dc7b04 100644 --- a/core/src/main/java/io/dekorate/kubernetes/annotation/Base.java +++ b/core/src/main/java/io/dekorate/kubernetes/annotation/Base.java @@ -188,6 +188,13 @@ */ Probe readinessProbe() default @Probe(); + /** + * The startup probe. + * + * @return The probe. + */ + Probe startupProbe() default @Probe(); + /** * The resources that the application container requires. */ diff --git a/core/src/main/java/io/dekorate/kubernetes/annotation/Container.java b/core/src/main/java/io/dekorate/kubernetes/annotation/Container.java index 4a4b652b2..39e1a3ae2 100644 --- a/core/src/main/java/io/dekorate/kubernetes/annotation/Container.java +++ b/core/src/main/java/io/dekorate/kubernetes/annotation/Container.java @@ -92,6 +92,13 @@ */ Probe readinessProbe() default @Probe(); + /** + * The startup probe. + * + * @return The probe. + */ + Probe startupProbe() default @Probe(); + /** * The resources that the application container requires. */ diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/AddStartupProbeDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/AddStartupProbeDecorator.java new file mode 100644 index 000000000..c0b125fb8 --- /dev/null +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/AddStartupProbeDecorator.java @@ -0,0 +1,46 @@ +/** + * Copyright 2018 The original authors. + * + * Licensed 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.dekorate.kubernetes.decorator; + +import io.dekorate.doc.Description; +import io.dekorate.kubernetes.config.Probe; +import io.fabric8.kubernetes.api.model.ContainerFluent; + +@Description("Add a startup probe to all containers.") +public class AddStartupProbeDecorator extends AbstractAddProbeDecorator { + + public AddStartupProbeDecorator(String containerName, Probe probe) { + super(containerName, probe); + } + + public AddStartupProbeDecorator(String deploymentName, String containerName, Probe probe) { + super(deploymentName, containerName, probe); + } + + @Override + protected void doCreateProbe(ContainerFluent container, Actions actions) { + container.withNewStartupProbe() + .withExec(actions.execAction) + .withHttpGet(actions.httpGetAction) + .withTcpSocket(actions.tcpSocketAction) + .withInitialDelaySeconds(probe.getInitialDelaySeconds()) + .withPeriodSeconds(probe.getPeriodSeconds()) + .withTimeoutSeconds(probe.getTimeoutSeconds()) + .withSuccessThreshold(probe.getSuccessThreshold()) + .withFailureThreshold(probe.getFailureThreshold()) + .endStartupProbe(); + } +} diff --git a/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveProbesFromInitContainerDecorator.java b/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveProbesFromInitContainerDecorator.java index 98410c818..9e672198b 100644 --- a/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveProbesFromInitContainerDecorator.java +++ b/core/src/main/java/io/dekorate/kubernetes/decorator/RemoveProbesFromInitContainerDecorator.java @@ -37,6 +37,6 @@ public void andThenVisit(PodSpecFluent podSpec, ObjectMeta resourceMeta) { @Override public Class[] after() { return new Class[] { ResourceProvidingDecorator.class, AddInitContainerDecorator.class, AddLivenessProbeDecorator.class, - AddReadinessProbeDecorator.class }; + AddReadinessProbeDecorator.class, AddStartupProbeDecorator.class }; } } diff --git a/docs/documentation/design.md b/docs/documentation/design.md index a46fa69ba..d5d1a0dab 100644 --- a/docs/documentation/design.md +++ b/docs/documentation/design.md @@ -142,9 +142,10 @@ The `decorator` looks pretty similar to the `configurator` the only difference b |-------------------------------|----------------|--------------------------------------------------------| | AddSecretVolume | PodSpec | Add a secret volume to all pod specs. | | AddService | KubernetesList | Add a service to the list. | -| AddLivenessProbe | Container | Add a liveness probe to all containers. | | AddEnvVar | Container | Add a environment variable to the container. | | AddReadinessProbe | Container | Add a readiness probe to all containers. | +| AddLivenessProbe | Container | Add a liveness probe to all containers. | +| AddStartupProbe | Container | Add a startup probe to all containers. | | AddConfigMapVolume | PodSpec | Add a configmap volume to the pod spec. | | AddEnvToComponent | ComponentSpec | Add environment variable to component. | | AddAzureDiskVolume | PodSpec | Add an Azure disk volume to the pod spec. | diff --git a/examples/knative-example/src/main/java/io/dekorate/example/Main.java b/examples/knative-example/src/main/java/io/dekorate/example/Main.java index e02089295..340201f3d 100644 --- a/examples/knative-example/src/main/java/io/dekorate/example/Main.java +++ b/examples/knative-example/src/main/java/io/dekorate/example/Main.java @@ -16,10 +16,16 @@ package io.dekorate.example; import io.dekorate.knative.annotation.KnativeApplication; +import io.dekorate.kubernetes.annotation.Probe; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -@KnativeApplication(minScale = 1, maxScale = 5, scaleToZeroEnabled = false) +@KnativeApplication(minScale = 1, maxScale = 5, scaleToZeroEnabled = false, + readinessProbe = @Probe(httpActionPath = "/readiness", periodSeconds = 30, timeoutSeconds = 10), + livenessProbe = @Probe(httpActionPath = "/liveness", periodSeconds = 31, timeoutSeconds = 11), + startupProbe = @Probe(httpActionPath = "/startup", periodSeconds = 32, timeoutSeconds = 12) +) @SpringBootApplication public class Main { diff --git a/examples/knative-example/src/test/java/io/dekorate/example/KnativeExampleTest.java b/examples/knative-example/src/test/java/io/dekorate/example/KnativeExampleTest.java index c518bae52..0712d8790 100644 --- a/examples/knative-example/src/test/java/io/dekorate/example/KnativeExampleTest.java +++ b/examples/knative-example/src/test/java/io/dekorate/example/KnativeExampleTest.java @@ -18,8 +18,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Optional; +import java.util.function.Function; import org.junit.jupiter.api.Test; @@ -29,6 +31,7 @@ import io.fabric8.kubernetes.api.model.Container; import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.api.model.KubernetesList; +import io.fabric8.kubernetes.api.model.Probe; class KnativeExampleTest { @@ -53,6 +56,41 @@ public void shouldContainServiceWithPortNamedHttp1() { assertEquals("false", configMap.getData().get("enable-scale-to-zero")); } + @Test + public void shouldContainProbes() { + KubernetesList list = Serialization.unmarshalAsList(KnativeExampleTest.class.getClassLoader().getResourceAsStream("META-INF/dekorate/knative.yml")); + Service service = findFirst(list, Service.class).orElseThrow(() -> new IllegalStateException("No knative service found!")); + + assertReadinessProbe(service, "/readiness", 30, 10); + assertLivenessProbe(service, "/liveness", 31, 11); + assertStartupProbe(service, "/startup", 32, 12); + } + + private static void assertReadinessProbe(Service service, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(service, Container::getReadinessProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertLivenessProbe(Service service, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(service, Container::getLivenessProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertStartupProbe(Service service, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(service, Container::getStartupProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertProbe(Service service, + Function probeFunction, + String actionPath, int periodSeconds, int timeoutSeconds) { + + assertTrue(service.getSpec().getTemplate().getSpec().getContainers().stream() + .map(probeFunction) + .anyMatch(probe -> actionPath.equals(probe.getHttpGet().getPath()) + && periodSeconds == probe.getPeriodSeconds() && timeoutSeconds == probe.getTimeoutSeconds())); + } + Optional findFirst(KubernetesList list, Class t) { return (Optional) list.getItems().stream() .filter(i -> t.isInstance(i)) diff --git a/examples/vertx-on-kubernetes-example/pom.xml b/examples/vertx-on-kubernetes-example/pom.xml index ca62bc4b4..da7315bb4 100644 --- a/examples/vertx-on-kubernetes-example/pom.xml +++ b/examples/vertx-on-kubernetes-example/pom.xml @@ -30,7 +30,8 @@ limitations under the License. 3.8.2 2.12.3 - 3.3 + 3.8.0 + 3.0.0-M3 @@ -75,6 +76,16 @@ limitations under the License. ${java.version} + + org.apache.maven.plugins + maven-surefire-plugin + ${version.maven-surefire-plugin} + true + + false + false + + diff --git a/examples/vertx-on-kubernetes-example/src/main/resources/application.properties b/examples/vertx-on-kubernetes-example/src/main/resources/application.properties index ed3ec18b0..d46cac1ab 100644 --- a/examples/vertx-on-kubernetes-example/src/main/resources/application.properties +++ b/examples/vertx-on-kubernetes-example/src/main/resources/application.properties @@ -1,5 +1,11 @@ dekorate.kubernetes.ports[0].name=http dekorate.kubernetes.ports[0].containerPort=8080 -dekorate.kubernetes.readinessProbe.httpActionPath=/ +dekorate.kubernetes.readinessProbe.httpActionPath=/readiness dekorate.kubernetes.readinessProbe.periodSeconds=30 dekorate.kubernetes.readinessProbe.timeoutSeconds=10 +dekorate.kubernetes.livenessProbe.httpActionPath=/liveness +dekorate.kubernetes.livenessProbe.periodSeconds=31 +dekorate.kubernetes.livenessProbe.timeoutSeconds=11 +dekorate.kubernetes.startupProbe.httpActionPath=/startup +dekorate.kubernetes.startupProbe.periodSeconds=32 +dekorate.kubernetes.startupProbe.timeoutSeconds=12 diff --git a/examples/vertx-on-kubernetes-example/src/test/java/io/dekorate/example/VertxTest.java b/examples/vertx-on-kubernetes-example/src/test/java/io/dekorate/example/VertxTest.java index f551959f3..2eff0ef8d 100644 --- a/examples/vertx-on-kubernetes-example/src/test/java/io/dekorate/example/VertxTest.java +++ b/examples/vertx-on-kubernetes-example/src/test/java/io/dekorate/example/VertxTest.java @@ -18,11 +18,16 @@ package io.dekorate.example; import java.util.Optional; +import java.util.function.Function; + import org.junit.jupiter.api.Test; +import io.fabric8.kubernetes.api.model.Container; import io.fabric8.kubernetes.api.model.KubernetesList; import io.fabric8.kubernetes.api.model.HasMetadata; +import io.fabric8.kubernetes.api.model.Probe; import io.fabric8.kubernetes.api.model.Service; +import io.fabric8.kubernetes.api.model.apps.Deployment; import static org.junit.jupiter.api.Assertions.assertTrue; import static io.dekorate.testing.KubernetesResources.*; @@ -30,10 +35,46 @@ class VertxTest { @Test - public void shouldContainService() throws Exception { + public void shouldContainService() { KubernetesList list = loadGenerated("kubernetes"); Optional service = findFirst(list, Service.class); assertTrue(service.isPresent()); } + + @Test + public void shouldContainProbes() { + KubernetesList list = loadGenerated("kubernetes"); + Optional deployment = findFirst(list, Deployment.class); + assertTrue(deployment.isPresent(), "Deployment not found!"); + + assertReadinessProbe(deployment.get(), "/readiness", 30, 10); + assertLivenessProbe(deployment.get(), "/liveness", 31, 11); + assertStartupProbe(deployment.get(), "/startup", 32, 12); + } + + private static void assertReadinessProbe(Deployment deployment, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(deployment, Container::getReadinessProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertLivenessProbe(Deployment deployment, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(deployment, Container::getLivenessProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertStartupProbe(Deployment deployment, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(deployment, Container::getStartupProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertProbe(Deployment deployment, + Function probeFunction, + String actionPath, int periodSeconds, int timeoutSeconds) { + + assertTrue(deployment.getSpec().getTemplate().getSpec().getContainers().stream() + .map(probeFunction) + .anyMatch(probe -> actionPath.equals(probe.getHttpGet().getPath()) + && periodSeconds == probe.getPeriodSeconds() && timeoutSeconds == probe.getTimeoutSeconds())); + } } diff --git a/examples/vertx-on-openshift-example/pom.xml b/examples/vertx-on-openshift-example/pom.xml index 61a14366c..6b043eb06 100644 --- a/examples/vertx-on-openshift-example/pom.xml +++ b/examples/vertx-on-openshift-example/pom.xml @@ -30,7 +30,8 @@ limitations under the License. 2.12.3 3.8.2 - 3.3 + 3.8.0 + 3.0.0-M3 @@ -74,6 +75,16 @@ limitations under the License. ${java.version} + + org.apache.maven.plugins + maven-surefire-plugin + ${version.maven-surefire-plugin} + true + + false + false + + diff --git a/examples/vertx-on-openshift-example/src/main/resources/application.properties b/examples/vertx-on-openshift-example/src/main/resources/application.properties index f55e23f33..3516afb38 100644 --- a/examples/vertx-on-openshift-example/src/main/resources/application.properties +++ b/examples/vertx-on-openshift-example/src/main/resources/application.properties @@ -1,5 +1,11 @@ dekorate.openshift.ports[0].name=http dekorate.openshift.ports[0].containerPort=8080 -dekorate.openshift.readinessProbe.httpActionPath=/ +dekorate.openshift.readinessProbe.httpActionPath=/readiness dekorate.openshift.readinessProbe.periodSeconds=30 dekorate.openshift.readinessProbe.timeoutSeconds=10 +dekorate.openshift.livenessProbe.httpActionPath=/liveness +dekorate.openshift.livenessProbe.periodSeconds=31 +dekorate.openshift.livenessProbe.timeoutSeconds=11 +dekorate.openshift.startupProbe.httpActionPath=/startup +dekorate.openshift.startupProbe.periodSeconds=32 +dekorate.openshift.startupProbe.timeoutSeconds=12 diff --git a/examples/vertx-on-openshift-example/src/test/java/io/dekorate/example/VertxTest.java b/examples/vertx-on-openshift-example/src/test/java/io/dekorate/example/VertxTest.java index ed230141c..fe6895c31 100644 --- a/examples/vertx-on-openshift-example/src/test/java/io/dekorate/example/VertxTest.java +++ b/examples/vertx-on-openshift-example/src/test/java/io/dekorate/example/VertxTest.java @@ -18,11 +18,16 @@ package io.dekorate.example; import java.util.Optional; +import java.util.function.Function; + import org.junit.jupiter.api.Test; +import io.fabric8.kubernetes.api.model.Container; import io.fabric8.kubernetes.api.model.KubernetesList; import io.fabric8.kubernetes.api.model.HasMetadata; +import io.fabric8.kubernetes.api.model.Probe; import io.fabric8.kubernetes.api.model.Service; +import io.fabric8.openshift.api.model.DeploymentConfig; import static org.junit.jupiter.api.Assertions.assertTrue; import static io.dekorate.testing.KubernetesResources.*; @@ -30,10 +35,46 @@ class VertxTest { @Test - public void shouldContainService() throws Exception { + public void shouldContainService() { KubernetesList list = loadGenerated("openshift"); Optional service = findFirst(list, Service.class); assertTrue(service.isPresent()); } + + @Test + public void shouldContainProbes() { + KubernetesList list = loadGenerated("openshift"); + Optional deployment = findFirst(list, DeploymentConfig.class); + assertTrue(deployment.isPresent(), "DeploymentConfig not found!"); + + assertReadinessProbe(deployment.get(), "/readiness", 30, 10); + assertLivenessProbe(deployment.get(), "/liveness", 31, 11); + assertStartupProbe(deployment.get(), "/startup", 32, 12); + } + + private static void assertReadinessProbe(DeploymentConfig deployment, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(deployment, Container::getReadinessProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertLivenessProbe(DeploymentConfig deployment, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(deployment, Container::getLivenessProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertStartupProbe(DeploymentConfig deployment, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(deployment, Container::getStartupProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertProbe(DeploymentConfig deployment, + Function probeFunction, + String actionPath, int periodSeconds, int timeoutSeconds) { + + assertTrue(deployment.getSpec().getTemplate().getSpec().getContainers().stream() + .map(probeFunction) + .anyMatch(probe -> actionPath.equals(probe.getHttpGet().getPath()) + && periodSeconds == probe.getPeriodSeconds() && timeoutSeconds == probe.getTimeoutSeconds())); + } }