Skip to content

Commit ee9f1ec

Browse files
committed
implement the PostgreSQLInstanceReadinessCheck
1 parent 3db4900 commit ee9f1ec

4 files changed

Lines changed: 147 additions & 69 deletions

File tree

src/main/java/it/aboutbits/postgresql/MinioInstanceReadinessCheck.java

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package it.aboutbits.postgresql;
2+
3+
import io.fabric8.kubernetes.client.KubernetesClient;
4+
import it.aboutbits.postgresql.core.PostgreSQLContextFactory;
5+
import it.aboutbits.postgresql.crd.connection.ClusterConnection;
6+
import lombok.RequiredArgsConstructor;
7+
import org.eclipse.microprofile.health.HealthCheck;
8+
import org.eclipse.microprofile.health.HealthCheckResponse;
9+
import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
10+
import org.eclipse.microprofile.health.Readiness;
11+
import org.jspecify.annotations.NullMarked;
12+
13+
/**
14+
* MicroProfile readiness health check that verifies connectivity to all
15+
* configured PostgreSQL instances. Each instance is probed with a lightweight
16+
* operation, and the aggregated status is exposed.
17+
*/
18+
@NullMarked
19+
@Readiness
20+
@RequiredArgsConstructor
21+
public class PostgreSQLInstanceReadinessCheck implements HealthCheck {
22+
private final PostgreSQLContextFactory postgreSQLContextFactory;
23+
24+
private final KubernetesClient kubernetesClient;
25+
26+
@Override
27+
public HealthCheckResponse call() {
28+
var builder = HealthCheckResponse.builder().name("PostgreSQL Instances");
29+
30+
var connections = kubernetesClient.resources(ClusterConnection.class).list().getItems();
31+
32+
boolean allUp = connections.stream()
33+
.allMatch(connection -> checkInstance(
34+
connection,
35+
builder
36+
));
37+
38+
return builder.status(allUp).build();
39+
}
40+
41+
private boolean checkInstance(
42+
ClusterConnection clusterConnection,
43+
HealthCheckResponseBuilder builder
44+
) {
45+
var name = clusterConnection.getMetadata().getName();
46+
47+
try (var dsl = postgreSQLContextFactory.getDSLContext(clusterConnection)) {
48+
var version = dsl.fetchSingle("select version()").into(String.class);
49+
50+
builder.withData(
51+
name,
52+
"UP (%s)".formatted(version)
53+
);
54+
55+
return true;
56+
} catch (Exception _) {
57+
builder.withData(
58+
name,
59+
"DOWN"
60+
);
61+
62+
return false;
63+
}
64+
}
65+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package it.aboutbits.postgresql;
2+
3+
import io.fabric8.kubernetes.client.KubernetesClient;
4+
import io.quarkus.test.junit.QuarkusTest;
5+
import it.aboutbits.postgresql._support.testdata.persisted.Given;
6+
import it.aboutbits.postgresql.crd.connection.ClusterConnection;
7+
import jakarta.inject.Inject;
8+
import org.eclipse.microprofile.health.HealthCheckResponse;
9+
import org.eclipse.microprofile.health.Readiness;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
@QuarkusTest
16+
class PostgreSQLInstanceReadinessCheckTest {
17+
@Inject
18+
Given given;
19+
20+
@Inject
21+
@Readiness
22+
PostgreSQLInstanceReadinessCheck readinessCheck;
23+
24+
@Inject
25+
KubernetesClient kubernetesClient;
26+
27+
@BeforeEach
28+
void cleanUp() {
29+
kubernetesClient.resources(ClusterConnection.class).delete();
30+
}
31+
32+
@Test
33+
void call_whenAllConnectionsUp_shouldReturnUp() {
34+
given.one()
35+
.clusterConnection()
36+
.withName("test-db")
37+
.returnFirst();
38+
39+
var response = readinessCheck.call();
40+
41+
assertThat(response.getStatus()).isEqualTo(
42+
HealthCheckResponse.Status.UP
43+
);
44+
45+
assertThat(response.getData())
46+
.isPresent()
47+
.get()
48+
.satisfies(data -> {
49+
assertThat(data).containsKey("test-db");
50+
assertThat(data.get("test-db").toString()).startsWith("UP (PostgreSQL");
51+
});
52+
}
53+
54+
@Test
55+
void call_whenSomeConnectionsDown_shouldReturnDown() {
56+
given.one()
57+
.clusterConnection()
58+
.withName("db-1")
59+
.returnFirst();
60+
61+
given.one()
62+
.clusterConnection()
63+
.withName("db-2")
64+
.withHost("non-existent-host")
65+
.returnFirst();
66+
67+
var response = readinessCheck.call();
68+
69+
assertThat(response.getStatus()).isEqualTo(
70+
HealthCheckResponse.Status.DOWN
71+
);
72+
73+
assertThat(response.getData())
74+
.isPresent()
75+
.get()
76+
.satisfies(data -> {
77+
assertThat(data).containsKey("db-1");
78+
assertThat(data.get("db-1").toString()).startsWith("UP (PostgreSQL");
79+
assertThat(data).containsEntry("db-2", "DOWN");
80+
});
81+
}
82+
}

src/test/java/it/aboutbits/postgresql/crd/role/RoleReconcilerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.junit.jupiter.params.provider.Arguments;
2121
import org.junit.jupiter.params.provider.MethodSource;
2222

23-
import java.sql.SQLException;
2423
import java.time.OffsetDateTime;
2524
import java.time.ZoneOffset;
2625
import java.time.temporal.ChronoUnit;

0 commit comments

Comments
 (0)