Skip to content

Commit

Permalink
feat(actuator): adding operator health indicator (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
bullshit authored Mar 12, 2024
1 parent 95099de commit 0e5e2b4
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
10 changes: 10 additions & 0 deletions starter-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.javaoperatorsdk</groupId>
<artifactId>operator-framework</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.javaoperatorsdk.operator.springboot.starter.test;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {
"management.endpoint.health.enabled=true", "management.endpoint.health.show-details=always"})
@EnableMockOperator(crdPaths = "classpath:crd.yml")
class HealthIndicatorTest {

@Autowired
private TestRestTemplate restTemplate;

@Test
void testOperatorHealthIndicator() {
ResponseEntity<String> entity =
this.restTemplate.getForEntity("/actuator/health", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
assertThat(entity.getBody())
.contains(
"\"operator\":{\"status\":\"UP\",\"details\":{\"customservicereconciler\":\"OK\"}}");
}

}
6 changes: 5 additions & 1 deletion starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.javaoperatorsdk.operator.springboot.starter;

import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import io.javaoperatorsdk.operator.Operator;

@Component
public class OperatorHealthIndicator extends AbstractHealthIndicator {

private final Operator operator;

public OperatorHealthIndicator(final Operator operator) {
super("OperatorSDK health check failed");
Assert.notNull(operator, "OperatorSDK Operator not initialized");
this.operator = operator;
}

@Override
protected void doHealthCheck(Health.Builder builder) {
final var runtimeInfo = operator.getRuntimeInfo();
if (runtimeInfo.isStarted()) {
final boolean[] healthy = {true};
runtimeInfo.getRegisteredControllers().forEach(rc -> {
final var name = rc.getConfiguration().getName();
final var unhealthy = rc.getControllerHealthInfo().unhealthyEventSources();
if (unhealthy.isEmpty()) {
builder.withDetail(name, "OK");
} else {
healthy[0] = false;
builder.withDetail(name, "unhealthy: " + String.join(", ", unhealthy.keySet()));
}
});
if (healthy[0]) {
builder.up();
} else {
builder.down();
}
} else {
builder.unknown();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.javaoperatorsdk.operator.springboot.starter;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import io.javaoperatorsdk.operator.Operator;

@Configuration(proxyBeanMethods = false)
@ConditionalOnBean(Operator.class)
@ComponentScan("io.javaoperatorsdk.operator.springboot.starter")
public class OperatorHealthIndicatorAutoConfiguration {

@Bean
public OperatorHealthIndicator createOperatorHealthIndicator(Operator operator) {
return new OperatorHealthIndicator(operator);
}

}

0 comments on commit 0e5e2b4

Please sign in to comment.