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

feat: add minimal health check #461

Merged
merged 1 commit into from
Dec 16, 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -99,6 +100,9 @@ void setup(BuildProducer<IndexDependencyBuildItem> indexDependency,
} else {
additionalBeans.produce(AdditionalBeanBuildItem.unremovableOf(NoOpMetricsProvider.class));
}

// register health check
additionalBeans.produce(AdditionalBeanBuildItem.unremovableOf(OperatorHealthCheck.class));
}

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