From 4e3c7679e844d6265827d3690370c0847e7236f5 Mon Sep 17 00:00:00 2001 From: Thomas Johansen Date: Tue, 10 Dec 2024 07:05:35 +0100 Subject: [PATCH] La til read-only health indicator --- .editorconfig | 5 ++ apps/bekreftelse-api/nais/nais-prod.yaml | 4 -- .../kotlin/no/nav/paw/health/model/Health.kt | 53 +++++++++++++------ .../repository/HealthIndicatorRepository.kt | 7 ++- .../nav/paw/health/route/HealthRoutesTest.kt | 8 +-- 5 files changed, 50 insertions(+), 27 deletions(-) diff --git a/.editorconfig b/.editorconfig index 4b87f3d9..c5814dc6 100644 --- a/.editorconfig +++ b/.editorconfig @@ -19,3 +19,8 @@ ij_kotlin_imports_layout = *,java.**,javax.**,kotlin.**,^ ij_kotlin_name_count_to_use_star_import = 9999 ij_kotlin_name_count_to_use_star_import_for_members = 9999 ij_kotlin_packages_to_use_import_on_demand = java.util.*,kotlinx.android.synthetic.*,io.ktor.* + +[{*.yml,*.yaml}] +indent_size = 2 +tab_width = 2 +ij_continuation_indent_size = 4 diff --git a/apps/bekreftelse-api/nais/nais-prod.yaml b/apps/bekreftelse-api/nais/nais-prod.yaml index 9cbb5c15..af9dcdca 100644 --- a/apps/bekreftelse-api/nais/nais-prod.yaml +++ b/apps/bekreftelse-api/nais/nais-prod.yaml @@ -65,10 +65,6 @@ spec: accessPolicy: inbound: rules: - - application: tokenx-token-generator - namespace: aura - - application: azure-token-generator - namespace: aura - application: arbeidssokerregistrering-for-veileder namespace: paw - application: arbeidssoekerregisteret-for-personbruker diff --git a/lib/error-handling/src/main/kotlin/no/nav/paw/health/model/Health.kt b/lib/error-handling/src/main/kotlin/no/nav/paw/health/model/Health.kt index 2c365684..bf398cce 100644 --- a/lib/error-handling/src/main/kotlin/no/nav/paw/health/model/Health.kt +++ b/lib/error-handling/src/main/kotlin/no/nav/paw/health/model/Health.kt @@ -2,35 +2,47 @@ package no.nav.paw.health.model import java.util.concurrent.atomic.AtomicReference -enum class HealthStatus(val value: String, val priority: Int) { - UNKNOWN("UNKNOWN", 10), - HEALTHY("HEALTHY", 5), - UNHEALTHY("UNHEALTHY", 1), +enum class HealthStatus(val value: String) { + UNKNOWN("UNKNOWN"), + HEALTHY("HEALTHY"), + UNHEALTHY("UNHEALTHY"), } -open class HealthIndicator(initialStatus: HealthStatus) { +interface HealthIndicator { + fun getStatus(): HealthStatus +} + +interface MutableHealthIndicator : HealthIndicator { + fun setUnknown(); + + fun setHealthy(); + + fun setUnhealthy(); +} + +open class DefaultHealthIndicator(initialStatus: HealthStatus) : MutableHealthIndicator { private val status = AtomicReference(initialStatus) - fun setUnknown() { + override fun setUnknown() { status.set(HealthStatus.UNKNOWN) } - fun setHealthy() { + override fun setHealthy() { status.set(HealthStatus.HEALTHY) } - fun setUnhealthy() { + override fun setUnhealthy() { status.set(HealthStatus.UNHEALTHY) } - fun getStatus(): HealthStatus { + override fun getStatus(): HealthStatus { return status.get() } override fun equals(other: Any?): Boolean { if (this === other) return true - other as HealthIndicator + if (other !is DefaultHealthIndicator) return false return status == other.status } @@ -41,9 +53,20 @@ open class HealthIndicator(initialStatus: HealthStatus) { typealias HealthIndicatorList = MutableList -fun HealthIndicatorList.getAggregatedStatus(): HealthStatus = - map(HealthIndicator::getStatus) - .minByOrNull(HealthStatus::priority) ?: HealthStatus.UNKNOWN +fun HealthIndicatorList.getAggregatedStatus(): HealthStatus { + return if (this.isEmpty()) { + HealthStatus.HEALTHY // Default til healthy + } else if (this.all { it.getStatus() == HealthStatus.HEALTHY }) { + HealthStatus.HEALTHY + } else if (this.any { it.getStatus() == HealthStatus.UNHEALTHY }) { + HealthStatus.UNHEALTHY + } else { + HealthStatus.UNKNOWN + } +} + +class ReadinessHealthIndicator(initialStatus: HealthStatus = HealthStatus.UNKNOWN) : + DefaultHealthIndicator(initialStatus) -class ReadinessHealthIndicator(initialStatus: HealthStatus = HealthStatus.UNKNOWN) : HealthIndicator(initialStatus) -class LivenessHealthIndicator(initialStatus: HealthStatus = HealthStatus.HEALTHY) : HealthIndicator(initialStatus) \ No newline at end of file +class LivenessHealthIndicator(initialStatus: HealthStatus = HealthStatus.HEALTHY) : + DefaultHealthIndicator(initialStatus) \ No newline at end of file diff --git a/lib/error-handling/src/main/kotlin/no/nav/paw/health/repository/HealthIndicatorRepository.kt b/lib/error-handling/src/main/kotlin/no/nav/paw/health/repository/HealthIndicatorRepository.kt index 85b418cd..2c5540c6 100644 --- a/lib/error-handling/src/main/kotlin/no/nav/paw/health/repository/HealthIndicatorRepository.kt +++ b/lib/error-handling/src/main/kotlin/no/nav/paw/health/repository/HealthIndicatorRepository.kt @@ -1,20 +1,19 @@ package no.nav.paw.health.repository +import no.nav.paw.health.model.HealthIndicator import no.nav.paw.health.model.HealthIndicatorList -import no.nav.paw.health.model.LivenessHealthIndicator -import no.nav.paw.health.model.ReadinessHealthIndicator class HealthIndicatorRepository { private val readinessIndicators: HealthIndicatorList = mutableListOf() private val livenessIndicators: HealthIndicatorList = mutableListOf() - fun addReadinessIndicator(healthIndicator: ReadinessHealthIndicator): ReadinessHealthIndicator { + fun addReadinessIndicator(healthIndicator: T): T { readinessIndicators.add(healthIndicator) return healthIndicator } - fun addLivenessIndicator(healthIndicator: LivenessHealthIndicator): LivenessHealthIndicator { + fun addLivenessIndicator(healthIndicator: T): T { livenessIndicators.add(healthIndicator) return healthIndicator } diff --git a/lib/error-handling/src/test/kotlin/no/nav/paw/health/route/HealthRoutesTest.kt b/lib/error-handling/src/test/kotlin/no/nav/paw/health/route/HealthRoutesTest.kt index 9d80fa75..b52ee494 100644 --- a/lib/error-handling/src/test/kotlin/no/nav/paw/health/route/HealthRoutesTest.kt +++ b/lib/error-handling/src/test/kotlin/no/nav/paw/health/route/HealthRoutesTest.kt @@ -45,10 +45,10 @@ class HealthRoutesTest : FreeSpec({ val livenessResponse1 = client.get("/internal/isAlive") val readinessResponse1 = client.get("/internal/isReady") - livenessResponse1.status shouldBe HttpStatusCode.ServiceUnavailable - livenessResponse1.body() shouldBe HealthStatus.UNKNOWN.value - readinessResponse1.status shouldBe HttpStatusCode.ServiceUnavailable - readinessResponse1.body() shouldBe HealthStatus.UNKNOWN.value + livenessResponse1.status shouldBe HttpStatusCode.OK + livenessResponse1.body() shouldBe HealthStatus.HEALTHY.value + readinessResponse1.status shouldBe HttpStatusCode.OK + readinessResponse1.body() shouldBe HealthStatus.HEALTHY.value val liveness1 = healthIndicatorRepository.addLivenessIndicator(LivenessHealthIndicator()) val liveness2 = healthIndicatorRepository.addLivenessIndicator(LivenessHealthIndicator())