Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support gRPC probes for Kubernetes, Openshift and Knative #882

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion assets/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ Each option can be:
They can be referenced by index inside brackets as in java for example: `dekorate.kubernetes.labels[0]`.

## Complex object
Each property of the complex object can be specified, by expanding the the property key.
Each property of the complex object can be specified, by expanding the property key.
For example lets assume the object `Probe` that looks like:

| Property | Type | Description | Default Value |
|-----------------------|--------|-------------|---------------|
| http-action-path | String | | |
| exec-action | String | | |
| tcp-socket-action | String | | |
| grpc-action | String | | |
| initial-delay-seconds | int | | 0 |
| period-seconds | int | | 30 |
| timeout-seconds | int | | 10 |
Expand Down Expand Up @@ -146,6 +147,7 @@ The section below describes all the available subtypes.
| http-action-path | String | The http path to use for the probe For this to work, the container port also needs to be set Assuming the container port has been set (as per above comment), if execAction or tcpSocketAction are not set, an http probe will be used automatically even if no path is set (which will result in the root path being used) | |
| exec-action | String | The command to use for the probe. | |
| tcp-socket-action | String | The tcp socket to use for the probe (the format is host:port). | |
| grpc-action | String | The gRPC port to use for the probe (the format is either port or port:service). | |
| initial-delay-seconds | int | The amount of time to wait in seconds before starting to probe. | 0 |
| period-seconds | int | The period in which the action should be called. | 30 |
| timeout-seconds | int | The amount of time to wait for each action. | 10 |
Expand Down
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()));
}
}
4 changes: 3 additions & 1 deletion docs/configuration-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ Each option can be:
They can be referenced by index inside brackets as in java for example: `dekorate.kubernetes.labels[0]`.

## Complex object
Each property of the complex object can be specified, by expanding the the property key.
Each property of the complex object can be specified, by expanding the property key.
For example lets assume the object `Probe` that looks like:

| Property | Type | Description | Default Value |
|-----------------------|--------|-------------|---------------|
| http-action-path | String | | |
| exec-action | String | | |
| tcp-socket-action | String | | |
| grpc-action | String | | |
| initial-delay-seconds | int | | 0 |
| period-seconds | int | | 30 |
| timeout-seconds | int | | 10 |
Expand Down Expand Up @@ -169,6 +170,7 @@ The section below describes all the available subtypes.
| http-action-path | String | The http path to use for the probe For this to work, the container port also needs to be set Assuming the container port has been set (as per above comment), if execAction or tcpSocketAction are not set, an http probe will be used automatically even if no path is set (which will result in the root path being used) | |
| exec-action | String | The command to use for the probe. | |
| tcp-socket-action | String | The tcp socket to use for the probe (the format is host:port). | |
| grpc-action | String | The gRPC port to use for the probe (the format is either port or port:service). | |
| initial-delay-seconds | int | The amount of time to wait in seconds before starting to probe. | 0 |
| period-seconds | int | The period in which the action should be called. | 30 |
| timeout-seconds | int | The amount of time to wait for each action. | 10 |
Expand Down
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>