From ef6c2c729742d5efb35e4488c071c052926ec315 Mon Sep 17 00:00:00 2001 From: Mattis Bratland Date: Sun, 20 Oct 2024 05:27:25 +0200 Subject: [PATCH] feat: add liveness and readiness probes to deployment - Added liveness and readiness probes to monitor the health and readiness of the application - Added endpoint for /health and /ready --- charts/flaiserator/templates/deployment.yaml | 18 +++++++++ src/main/kotlin/no/fintlabs/Application.kt | 13 +++++++ .../kotlin/no/fintlabs/HttpTests.kt | 39 +++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/charts/flaiserator/templates/deployment.yaml b/charts/flaiserator/templates/deployment.yaml index 4ba744d..5d1e053 100644 --- a/charts/flaiserator/templates/deployment.yaml +++ b/charts/flaiserator/templates/deployment.yaml @@ -48,6 +48,24 @@ spec: env: {{- toYaml . | nindent 12 }} {{- end }} + + livenessProbe: + httpGet: + path: /health + port: 8080 + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 3 + + readinessProbe: + httpGet: + path: /ready + port: 8080 + initialDelaySeconds: 5 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 3 {{- with .Values.volumes }} volumes: {{- toYaml . | nindent 8 }} diff --git a/src/main/kotlin/no/fintlabs/Application.kt b/src/main/kotlin/no/fintlabs/Application.kt index 3139be5..fc71840 100644 --- a/src/main/kotlin/no/fintlabs/Application.kt +++ b/src/main/kotlin/no/fintlabs/Application.kt @@ -82,8 +82,21 @@ val baseModule = module { } single { val prometheusRegistry: PrometheusMeterRegistry = get() + val operator: Operator = get() routes( "/metrics" bind Method.GET to { Response(Status.OK).body(prometheusRegistry.scrape()) }, + "/health" bind Method.GET to { + when (operator.runtimeInfo.isStarted) { + true -> Response(Status.OK).body("Operator is healthy.") + false -> Response(Status.SERVICE_UNAVAILABLE).body("Operator is not healthy.") + } + }, + "/ready" bind Method.GET to { + when (operator.runtimeInfo.isStarted) { + true -> Response(Status.OK).body("Operator is ready.") + false -> Response(Status.SERVICE_UNAVAILABLE).body("Operator is not ready.") + } + } ) } } diff --git a/src/test/integration/kotlin/no/fintlabs/HttpTests.kt b/src/test/integration/kotlin/no/fintlabs/HttpTests.kt index 66abc7f..01c8270 100644 --- a/src/test/integration/kotlin/no/fintlabs/HttpTests.kt +++ b/src/test/integration/kotlin/no/fintlabs/HttpTests.kt @@ -1,6 +1,8 @@ package no.fintlabs +import no.fintlabs.extensions.KubernetesOperatorContext import no.fintlabs.operator.Utils.createKoinTestExtension +import no.fintlabs.operator.Utils.createKubernetesOperatorExtension import org.http4k.core.HttpHandler import org.http4k.core.Method import org.http4k.core.Request @@ -22,8 +24,45 @@ class HttpTests : KoinTest { assertContains(response.bodyString(), "jvm_memory_used_bytes") } + @Test + fun `should return health ok`() { + val client: HttpHandler by inject() + + val response = client(Request(Method.GET, "/health")) + assertEquals(Status.OK, response.status) + } + + @Test + fun `should return health fail`(context: KubernetesOperatorContext) { + val client: HttpHandler by inject() + + context.operator.stop() + val response = client(Request(Method.GET, "/health")) + assertEquals(Status.SERVICE_UNAVAILABLE, response.status) + } + + @Test + fun `should return readiness ok`() { + val client: HttpHandler by inject() + + val response = client(Request(Method.GET, "/ready")) + assertEquals(Status.OK, response.status) + } + + @Test + fun `should return readiness fail`(context: KubernetesOperatorContext) { + val client: HttpHandler by inject() + + context.operator.stop() + val response = client(Request(Method.GET, "/ready")) + assertEquals(Status.SERVICE_UNAVAILABLE, response.status) + } + companion object { @RegisterExtension val koinTestExtension = createKoinTestExtension() + + @RegisterExtension + val kubernetesOperatorExtension = createKubernetesOperatorExtension() } } \ No newline at end of file