Skip to content

Commit

Permalink
Support gRPC probes for Kubernetes, Openshift and Knative
Browse files Browse the repository at this point in the history
See feature request description in dekorateio#857
  • Loading branch information
Sgitario committed Feb 28, 2022
1 parent 08ac818 commit a8b0e92
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@
*/
String tcpSocketAction() default "";

/**
* The gRPC port to use for the probe (the format is "port").
* If the health endpoint is configured on a non-default service, you must also specify the service (the format
* is "port:service").
*
* @return The string representation of the gRPC probe.
*/
String grpcAction() default "";

/**
* The amount of time to wait in seconds before starting to probe.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.dekorate.utils.Strings;
import io.fabric8.kubernetes.api.model.ContainerFluent;
import io.fabric8.kubernetes.api.model.ExecAction;
import io.fabric8.kubernetes.api.model.GRPCAction;
import io.fabric8.kubernetes.api.model.HTTPGetAction;
import io.fabric8.kubernetes.api.model.IntOrString;
import io.fabric8.kubernetes.api.model.TCPSocketAction;
Expand All @@ -31,7 +32,6 @@
* Base class for any kind of {@link Decorator} that acts on probes.
*/
public abstract class AbstractAddProbeDecorator extends ApplicationContainerDecorator<ContainerFluent<?>> {

protected final Probe probe;

abstract protected void doCreateProbe(ContainerFluent<?> container, Actions actions);
Expand All @@ -54,13 +54,14 @@ public void andThenVisit(ContainerFluent<?> container) {

final ExecAction execAction = execAction(probe);
final TCPSocketAction tcpSocketAction = tcpSocketAction(probe);
final GRPCAction grpcAction = grpcAction(probe);
final boolean defaultToHttpGetAction = (execAction == null) && (tcpSocketAction == null);
final HTTPGetAction httpGetAction = defaultToHttpGetAction ? httpGetAction(probe, container) : null;
if (defaultToHttpGetAction && (httpGetAction == null)) {
return;
}

doCreateProbe(container, new Actions(execAction, tcpSocketAction, httpGetAction));
doCreateProbe(container, new Actions(execAction, tcpSocketAction, httpGetAction, grpcAction));
}

private ExecAction execAction(Probe probe) {
Expand Down Expand Up @@ -93,15 +94,40 @@ private TCPSocketAction tcpSocketAction(Probe probe) {
return new TCPSocketAction(parts[0], new IntOrString(parts[1]));
}

private GRPCAction grpcAction(Probe probe) {
String grpcActionExpression = probe.getGrpcAction();
if (Strings.isNullOrEmpty(grpcActionExpression)) {
return null;
}

try {
GRPCAction grpcAction;
if (grpcActionExpression.contains(":")) {
// both port and service is provided
String[] parts = grpcActionExpression.split(":");
grpcAction = new GRPCAction(Integer.valueOf(parts[0]), parts[1]);
} else {
grpcAction = new GRPCAction(Integer.valueOf(grpcActionExpression), null);
}

return grpcAction;
} catch (NumberFormatException ex) {
throw new RuntimeException("Wrong port format set in the gRPC probe. Got: " + grpcActionExpression, ex);
}
}

protected static class Actions {
protected final ExecAction execAction;
protected final TCPSocketAction tcpSocketAction;
protected final HTTPGetAction httpGetAction;
protected final GRPCAction grpcAction;

protected Actions(ExecAction execAction, TCPSocketAction tcpSocketAction, HTTPGetAction httpGetAction) {
protected Actions(ExecAction execAction, TCPSocketAction tcpSocketAction,
HTTPGetAction httpGetAction, GRPCAction grpcAction) {
this.execAction = execAction;
this.tcpSocketAction = tcpSocketAction;
this.httpGetAction = httpGetAction;
this.grpcAction = grpcAction;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ protected void doCreateProbe(ContainerFluent<?> container, Actions actions) {
.withExec(actions.execAction)
.withHttpGet(actions.httpGetAction)
.withTcpSocket(actions.tcpSocketAction)
.withGrpc(actions.grpcAction)
.withInitialDelaySeconds(probe.getInitialDelaySeconds())
.withPeriodSeconds(probe.getPeriodSeconds())
.withTimeoutSeconds(probe.getTimeoutSeconds())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected void doCreateProbe(ContainerFluent<?> container, Actions actions) {
.withExec(actions.execAction)
.withHttpGet(actions.httpGetAction)
.withTcpSocket(actions.tcpSocketAction)
.withGrpc(actions.grpcAction)
.withInitialDelaySeconds(probe.getInitialDelaySeconds())
.withPeriodSeconds(probe.getPeriodSeconds())
.withTimeoutSeconds(probe.getTimeoutSeconds())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected void doCreateProbe(ContainerFluent<?> container, Actions actions) {
.withExec(actions.execAction)
.withHttpGet(actions.httpGetAction)
.withTcpSocket(actions.tcpSocketAction)
.withGrpc(actions.grpcAction)
.withInitialDelaySeconds(probe.getInitialDelaySeconds())
.withPeriodSeconds(probe.getPeriodSeconds())
.withTimeoutSeconds(probe.getTimeoutSeconds())
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/io/dekorate/utils/Probes.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static boolean isConfigured(Probe probe) {
return probe != null
&& (Strings.isNotNullOrEmpty(probe.getHttpActionPath())
|| Strings.isNotNullOrEmpty(probe.getExecAction())
|| Strings.isNotNullOrEmpty(probe.getTcpSocketAction()));
|| Strings.isNotNullOrEmpty(probe.getTcpSocketAction())
|| Strings.isNotNullOrEmpty(probe.getGrpcAction()));
}
}
58 changes: 58 additions & 0 deletions tests/feat-857-kubernetes-grpc-probes/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>dekorate-tests</artifactId>
<groupId>io.dekorate</groupId>
<version>2.9-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>

<groupId>io.dekorate</groupId>
<version>2.9-SNAPSHOT</version>
<artifactId>feat-857-kubernetes-grpc-probes</artifactId>
<name>Dekorate :: Tests :: gRPC Probes :: Kubernetes</name>

<dependencies>
<dependency>
<groupId>io.dekorate</groupId>
<artifactId>kubernetes-annotations</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.dekorate</groupId>
<artifactId>dekorate-spring-boot</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${version.spring-boot}</version>
</dependency>

<!-- Testing -->
<dependency>
<groupId>io.dekorate</groupId>
<artifactId>kubernetes-junit</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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.example;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {

@RequestMapping("/")
public String hello() {
return "Hello world";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {

public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Cover simple port mapping
dekorate.kubernetes.readinessProbe.grpcAction=8000
# Cover port-service mapping
dekorate.kubernetes.livenessProbe.grpcAction=8001:service
dekorate.kubernetes.startupProbe.grpcAction=8002
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* 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.example;

import static io.dekorate.testing.KubernetesResources.findFirst;
import static io.dekorate.testing.KubernetesResources.loadGenerated;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Optional;
import java.util.function.Function;

import org.junit.jupiter.api.Test;

import io.dekorate.utils.Strings;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.Probe;
import io.fabric8.kubernetes.api.model.apps.Deployment;

class GRPCProbesExampleTest {

@Test
public void shouldContainProbes() {
KubernetesList list = loadGenerated("kubernetes");
Optional<Deployment> deployment = findFirst(list, Deployment.class);
assertTrue(deployment.isPresent(), "Deployment not found!");

assertReadinessProbe(deployment.get(), 8000, null);
assertLivenessProbe(deployment.get(), 8001, "service");
assertStartupProbe(deployment.get(), 8002, null);
}

private static void assertReadinessProbe(Deployment deployment, int port, String service) {
assertProbe(deployment, Container::getReadinessProbe, port, service);
}

private static void assertLivenessProbe(Deployment deployment, int port, String service) {
assertProbe(deployment, Container::getLivenessProbe, port, service);
}

private static void assertStartupProbe(Deployment deployment, int port, String service) {
assertProbe(deployment, Container::getStartupProbe, port, service);
}

private static void assertProbe(Deployment deployment, Function<Container, Probe> probeFunction, int port, String service) {

assertTrue(deployment.getSpec().getTemplate().getSpec().getContainers().stream()
.map(probeFunction)
.anyMatch(probe -> Strings.equals(service, probe.getGrpc().getService())
&& port == probe.getGrpc().getPort()));
}
}
1 change: 1 addition & 0 deletions tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<module>feat-kubernetes-probes</module>
<module>feat-openshift-probes</module>
<module>feat-knative-probes</module>
<module>feat-857-kubernetes-grpc-probes</module>
</modules>

</project>

0 comments on commit a8b0e92

Please sign in to comment.