Skip to content

Commit

Permalink
Merge pull request #65 from FINTLabs/feat/add-readiness-liveness-probes
Browse files Browse the repository at this point in the history
FLA-514 Add liveness and readiness probes
  • Loading branch information
murillio4 authored Oct 20, 2024
2 parents 24fff02 + 36fce08 commit abee1dd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
18 changes: 18 additions & 0 deletions charts/flaiserator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/no/fintlabs/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,21 @@ val baseModule = module {
}
single<HttpHandler> {
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.")
}
}
)
}
}
Expand Down
39 changes: 39 additions & 0 deletions src/test/integration/kotlin/no/fintlabs/HttpTests.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
}
}

0 comments on commit abee1dd

Please sign in to comment.