-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(actuator): adding operator health indicator (#132)
- Loading branch information
Showing
5 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...rc/test/java/io/javaoperatorsdk/operator/springboot/starter/test/HealthIndicatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\"}}"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...src/main/java/io/javaoperatorsdk/operator/springboot/starter/OperatorHealthIndicator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...javaoperatorsdk/operator/springboot/starter/OperatorHealthIndicatorAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |