Skip to content

Commit

Permalink
Support Startup Probes for Kubernetes, Openshift and Knative
Browse files Browse the repository at this point in the history
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 dekorateio#854
  • Loading branch information
Sgitario authored and iocanel committed Feb 28, 2022
1 parent f4e0030 commit a8d832c
Show file tree
Hide file tree
Showing 19 changed files with 264 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
3 changes: 2 additions & 1 deletion assets/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public void andThenVisit(PodSpecFluent<?> podSpec, ObjectMeta resourceMeta) {
@Override
public Class<? extends Decorator>[] after() {
return new Class[] { ResourceProvidingDecorator.class, AddInitContainerDecorator.class, AddLivenessProbeDecorator.class,
AddReadinessProbeDecorator.class };
AddReadinessProbeDecorator.class, AddStartupProbeDecorator.class };
}
}
3 changes: 2 additions & 1 deletion docs/documentation/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {

Expand All @@ -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<Container, Probe> 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()));
}

<T extends HasMetadata> Optional<T> findFirst(KubernetesList list, Class<T> t) {
return (Optional<T>) list.getItems().stream()
.filter(i -> t.isInstance(i))
Expand Down
13 changes: 12 additions & 1 deletion examples/vertx-on-kubernetes-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ limitations under the License.

<version.vertx>3.8.2</version.vertx>
<version.jackson>2.12.3</version.jackson>
<version.maven-compiler-plugin>3.3</version.maven-compiler-plugin>
<version.maven-compiler-plugin>3.8.0</version.maven-compiler-plugin>
<version.maven-surefire-plugin>3.0.0-M3</version.maven-surefire-plugin>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -75,6 +76,16 @@ limitations under the License.
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.maven-surefire-plugin}</version>
<inherited>true</inherited>
<configuration>
<trimStackTrace>false</trimStackTrace>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,63 @@
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.*;

class VertxTest {

@Test
public void shouldContainService() throws Exception {
public void shouldContainService() {
KubernetesList list = loadGenerated("kubernetes");
Optional<Service> service = findFirst(list, Service.class);
assertTrue(service.isPresent());
}

@Test
public void shouldContainProbes() {
KubernetesList list = loadGenerated("kubernetes");
Optional<Deployment> 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<Container, Probe> 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()));
}
}

13 changes: 12 additions & 1 deletion examples/vertx-on-openshift-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ limitations under the License.

<version.jackson>2.12.3</version.jackson>
<version.vertx>3.8.2</version.vertx>
<version.maven-compiler-plugin>3.3</version.maven-compiler-plugin>
<version.maven-compiler-plugin>3.8.0</version.maven-compiler-plugin>
<version.maven-surefire-plugin>3.0.0-M3</version.maven-surefire-plugin>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -74,6 +75,16 @@ limitations under the License.
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.maven-surefire-plugin}</version>
<inherited>true</inherited>
<configuration>
<trimStackTrace>false</trimStackTrace>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit a8d832c

Please sign in to comment.