From 338811f2c65815b57b8dac35e710c4437d1726f7 Mon Sep 17 00:00:00 2001 From: Chris Laprun Date: Fri, 16 Dec 2022 14:23:12 +0100 Subject: [PATCH] feat: add minimal health check Fixes #446 --- .../deployment/OperatorSDKProcessor.java | 4 ++ .../runtime/OperatorHealthCheck.java | 47 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 core/runtime/src/main/java/io/quarkiverse/operatorsdk/runtime/OperatorHealthCheck.java diff --git a/core/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/OperatorSDKProcessor.java b/core/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/OperatorSDKProcessor.java index e5b6f8c57..9e9cb7cd5 100644 --- a/core/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/OperatorSDKProcessor.java +++ b/core/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/OperatorSDKProcessor.java @@ -34,6 +34,7 @@ import io.quarkiverse.operatorsdk.runtime.ConfigurationServiceRecorder; import io.quarkiverse.operatorsdk.runtime.KubernetesClientSerializationCustomizer; import io.quarkiverse.operatorsdk.runtime.NoOpMetricsProvider; +import io.quarkiverse.operatorsdk.runtime.OperatorHealthCheck; import io.quarkiverse.operatorsdk.runtime.OperatorProducer; import io.quarkiverse.operatorsdk.runtime.QuarkusConfigurationService; import io.quarkiverse.operatorsdk.runtime.ResourceInfo; @@ -99,6 +100,9 @@ void setup(BuildProducer indexDependency, } else { additionalBeans.produce(AdditionalBeanBuildItem.unremovableOf(NoOpMetricsProvider.class)); } + + // register health check + additionalBeans.produce(AdditionalBeanBuildItem.unremovableOf(OperatorHealthCheck.class)); } @BuildStep diff --git a/core/runtime/src/main/java/io/quarkiverse/operatorsdk/runtime/OperatorHealthCheck.java b/core/runtime/src/main/java/io/quarkiverse/operatorsdk/runtime/OperatorHealthCheck.java new file mode 100644 index 000000000..a44f33766 --- /dev/null +++ b/core/runtime/src/main/java/io/quarkiverse/operatorsdk/runtime/OperatorHealthCheck.java @@ -0,0 +1,47 @@ +package io.quarkiverse.operatorsdk.runtime; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; + +import org.eclipse.microprofile.health.HealthCheck; +import org.eclipse.microprofile.health.HealthCheckResponse; +import org.eclipse.microprofile.health.Readiness; + +import io.javaoperatorsdk.operator.Operator; + +@Readiness +@ApplicationScoped +public class OperatorHealthCheck implements HealthCheck { + + public static final String HEALTH_CHECK_NAME = "Quarkus Operator SDK health check"; + public static final String OK = "OK"; + @Inject + Operator operator; + + @Override + public HealthCheckResponse call() { + final var runtimeInfo = operator.getRuntimeInfo(); + if (runtimeInfo.isStarted()) { + final var response = HealthCheckResponse.named(HEALTH_CHECK_NAME); + final boolean[] healthy = { true }; + runtimeInfo.getRegisteredControllers().forEach(rc -> { + final var name = rc.getConfiguration().getName(); + final var unhealthy = rc.getControllerHealthInfo().unhealthyEventSources(); + if (unhealthy.isEmpty()) { + response.withData(name, OK); + } else { + healthy[0] = false; + response + .withData(name, "unhealthy: " + String.join(", ", unhealthy.keySet())); + } + }); + if (healthy[0]) { + response.up(); + } else { + response.down(); + } + return response.build(); + } + return HealthCheckResponse.down(HEALTH_CHECK_NAME); + } +}