Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FLA-514 Add liveness and readiness probes #65

Merged
merged 2 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}