From e285ed81e39788ee7d38e081680d7ec4f5d3ffea Mon Sep 17 00:00:00 2001 From: Ole Christian Kvernberg <42737566+olekvernberg@users.noreply.github.com> Date: Thu, 14 Sep 2023 13:45:14 +0200 Subject: [PATCH 1/2] Unleash next (#894) Unleash next --- .deploy/nais-dev.yaml | 3 +- .deploy/nais-prod.yaml | 3 +- pom.xml | 12 +-- .../api/FeatureToggleController.kt" | 1 + .../config/FeatureToggleConfig.kt" | 81 +++---------------- .../featuretoggle/FeatureToggleService.kt" | 12 ++- src/main/resources/application.yml | 6 +- ...FeatureToggleControllerIntegrationTest.kt" | 55 ------------- .../api/FeatureToggleControllerTest.kt" | 42 ---------- src/test/resources/application-local.yml | 11 +-- 10 files changed, 38 insertions(+), 188 deletions(-) delete mode 100644 "src/test/kotlin/no/nav/familie/ef/s\303\270knad/api/FeatureToggleControllerIntegrationTest.kt" delete mode 100644 "src/test/kotlin/no/nav/familie/ef/s\303\270knad/api/FeatureToggleControllerTest.kt" diff --git a/.deploy/nais-dev.yaml b/.deploy/nais-dev.yaml index 3ad2f1101..0556d5015 100644 --- a/.deploy/nais-dev.yaml +++ b/.deploy/nais-dev.yaml @@ -47,13 +47,14 @@ spec: rules: - application: familie-ef-mottak external: - - host: unleash.nais.io - host: familie-integrasjoner.dev-fss-pub.nais.io - host: pdl-api.dev-fss-pub.nais.io - host: familie-ef-mottak.dev-fss-pub.nais.io + - host: teamfamilie-unleash-api.nav.cloud.nais.io envFrom: - secret: familie - secret: ef-soknad-api + - secret: familie-ef-soknad-api-unleash-api-token env: - name: SPRING_PROFILES_ACTIVE value: dev \ No newline at end of file diff --git a/.deploy/nais-prod.yaml b/.deploy/nais-prod.yaml index c7d4d0d0b..153a9d77b 100644 --- a/.deploy/nais-prod.yaml +++ b/.deploy/nais-prod.yaml @@ -47,13 +47,14 @@ spec: rules: - application: familie-ef-mottak external: - - host: unleash.nais.io - host: familie-integrasjoner.prod-fss-pub.nais.io - host: pdl-api.prod-fss-pub.nais.io - host: familie-ef-mottak.prod-fss-pub.nais.io + - host: teamfamilie-unleash-api.nav.cloud.nais.io envFrom: - secret: familie - secret: ef-soknad-api + - secret: familie-ef-soknad-api-unleash-api-token env: - name: SPRING_PROFILES_ACTIVE value: prod \ No newline at end of file diff --git a/pom.xml b/pom.xml index b717a2651..78fe25d63 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,7 @@ 4.9.1 2.15.2 - 2.20230210162649_a258d57-SPRING_BOOT_3 + 2.20230905101454_06fa3d7 3.0_20230509152247_36d24db no.nav.familie.ef.søknad.ApplicationKt @@ -111,11 +111,6 @@ io.micrometer micrometer-registry-prometheus - - io.getunleash - unleash-client-java - 8.3.0 - @@ -128,6 +123,11 @@ http-client ${felles.version} + + no.nav.familie.felles + unleash + ${felles.version} + no.nav.security token-validation-spring diff --git "a/src/main/kotlin/no/nav/familie/ef/s\303\270knad/api/FeatureToggleController.kt" "b/src/main/kotlin/no/nav/familie/ef/s\303\270knad/api/FeatureToggleController.kt" index cc0dbb0db..20568bfb2 100644 --- "a/src/main/kotlin/no/nav/familie/ef/s\303\270knad/api/FeatureToggleController.kt" +++ "b/src/main/kotlin/no/nav/familie/ef/s\303\270knad/api/FeatureToggleController.kt" @@ -17,6 +17,7 @@ class FeatureToggleController(private val featureToggleService: FeatureToggleSer val funksjonsbrytere = listOf( "familie.ef.soknad.feilsituasjon", "familie.ef.soknad.nynorsk", + "test.environment", ) @GetMapping diff --git "a/src/main/kotlin/no/nav/familie/ef/s\303\270knad/config/FeatureToggleConfig.kt" "b/src/main/kotlin/no/nav/familie/ef/s\303\270knad/config/FeatureToggleConfig.kt" index 19231b8ac..97218736f 100644 --- "a/src/main/kotlin/no/nav/familie/ef/s\303\270knad/config/FeatureToggleConfig.kt" +++ "b/src/main/kotlin/no/nav/familie/ef/s\303\270knad/config/FeatureToggleConfig.kt" @@ -1,81 +1,26 @@ package no.nav.familie.ef.søknad.config -import io.getunleash.DefaultUnleash -import io.getunleash.UnleashContext -import io.getunleash.UnleashContextProvider -import io.getunleash.util.UnleashConfig +import io.getunleash.strategy.Strategy import no.nav.familie.ef.søknad.featuretoggle.ByEnvironmentStrategy -import no.nav.familie.ef.søknad.featuretoggle.FeatureToggleService -import org.slf4j.Logger -import org.slf4j.LoggerFactory -import org.springframework.boot.context.properties.ConfigurationProperties +import no.nav.familie.unleash.DefaultUnleashService +import org.springframework.beans.factory.annotation.Value import org.springframework.context.annotation.Bean -import java.net.URI +import org.springframework.context.annotation.Configuration -@ConfigurationProperties("funksjonsbrytere") +@Configuration class FeatureToggleConfig( - private val enabled: Boolean, - val unleash: Unleash, + @Value("\${UNLEASH_SERVER_API_URL}") private val apiUrl: String, + @Value("\${UNLEASH_SERVER_API_TOKEN}") private val apiToken: String, + @Value("\${NAIS_APP_NAME}") private val appName: String, ) { - data class Unleash( - val uri: URI, - val environment: String, - val applicationName: String, - ) - - private val log: Logger = LoggerFactory.getLogger(this::class.java) - @Bean - fun featureToggle(): FeatureToggleService = - if (enabled) { - lagUnleashFeatureToggleService() - } else { - log.warn( - "Funksjonsbryter-funksjonalitet er skrudd AV. " + - "Gir standardoppførsel for alle funksjonsbrytere, dvs 'false'", - ) - lagDummyFeatureToggleService() - } - - private fun lagUnleashFeatureToggleService(): FeatureToggleService { - val unleash = DefaultUnleash( - UnleashConfig.builder() - .appName(unleash.applicationName) - .unleashAPI(unleash.uri) - .unleashContextProvider(lagUnleashContextProvider()) - .build(), - ByEnvironmentStrategy(), - ) - - return object : FeatureToggleService { - override fun isEnabled(toggleId: String, defaultValue: Boolean): Boolean { - return unleash.isEnabled(toggleId, defaultValue) - } - } + fun strategies(): List { + return listOf(ByEnvironmentStrategy()) } - private fun lagUnleashContextProvider(): UnleashContextProvider { - return UnleashContextProvider { - UnleashContext.builder() - // .userId("a user") // Må legges til en gang i fremtiden - .environment(unleash.environment) - .appName(unleash.applicationName) - .build() - } - } - - private fun lagDummyFeatureToggleService(): FeatureToggleService { - return object : FeatureToggleService { - override fun isEnabled(toggleId: String, defaultValue: Boolean): Boolean { - if (unleash.environment == "local") { - return when (toggleId) { - "familie.ef.soknad.feilsituasjon" -> false - else -> true - } - } - return defaultValue - } - } + @Bean + fun defaultUnleashService(strategies: List): DefaultUnleashService { + return DefaultUnleashService(apiUrl, apiToken, appName, strategies) } } diff --git "a/src/main/kotlin/no/nav/familie/ef/s\303\270knad/featuretoggle/FeatureToggleService.kt" "b/src/main/kotlin/no/nav/familie/ef/s\303\270knad/featuretoggle/FeatureToggleService.kt" index 25336c8b4..02fe26d33 100644 --- "a/src/main/kotlin/no/nav/familie/ef/s\303\270knad/featuretoggle/FeatureToggleService.kt" +++ "b/src/main/kotlin/no/nav/familie/ef/s\303\270knad/featuretoggle/FeatureToggleService.kt" @@ -1,10 +1,16 @@ package no.nav.familie.ef.søknad.featuretoggle -interface FeatureToggleService { +import no.nav.familie.unleash.DefaultUnleashService +import org.springframework.stereotype.Service + +@Service +class FeatureToggleService(val defaultUnleashService: DefaultUnleashService) { fun isEnabled(toggleId: String): Boolean { - return isEnabled(toggleId, false) + return defaultUnleashService.isEnabled(toggleId) } - fun isEnabled(toggleId: String, defaultValue: Boolean): Boolean + fun isEnabled(toggleId: String, defaultValue: Boolean): Boolean { + return defaultUnleashService.isEnabled(toggleId, defaultValue) + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index aa2b61aa1..d92a5816f 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -31,12 +31,8 @@ health: indicator: detailed: true -funksjonsbrytere: +unleash: enabled: true - unleash: - uri: https://unleash.nais.io/api/ - environment: ${NAIS_CLUSTER_NAME:} - applicationName: ${NAIS_APP_NAME:} management: endpoint: diff --git "a/src/test/kotlin/no/nav/familie/ef/s\303\270knad/api/FeatureToggleControllerIntegrationTest.kt" "b/src/test/kotlin/no/nav/familie/ef/s\303\270knad/api/FeatureToggleControllerIntegrationTest.kt" deleted file mode 100644 index ae9babb5f..000000000 --- "a/src/test/kotlin/no/nav/familie/ef/s\303\270knad/api/FeatureToggleControllerIntegrationTest.kt" +++ /dev/null @@ -1,55 +0,0 @@ -package no.nav.familie.ef.søknad.api - -import io.getunleash.DefaultUnleash -import io.getunleash.UnleashContext -import io.getunleash.UnleashContextProvider -import io.getunleash.util.UnleashConfig -import no.nav.familie.ef.søknad.featuretoggle.ByEnvironmentStrategy -import no.nav.familie.ef.søknad.featuretoggle.FeatureToggleService -import org.assertj.core.api.Assertions -import org.junit.jupiter.api.Test -import kotlin.test.Ignore - -@Ignore( - "Krever at unleash kjører lokalt på port 4242, og at en funksjonsbryter " + - "'test' er satt opp med byEnvironmentStrategy og miljø=local", -) -internal class FeatureToggleControllerIntegrationTest { - - val unleashUrl = "http://localhost:4242/api" - - private val unleashContextProvider = UnleashContextProvider { - UnleashContext.builder() - .environment("local") - .appName("app") - .build() - } - - private val unleashService = object : FeatureToggleService { - - val unleash = DefaultUnleash( - UnleashConfig.builder() - .appName("app") - .unleashAPI(unleashUrl) - .unleashContextProvider(unleashContextProvider) - .build(), - ByEnvironmentStrategy(), - ) - - override fun isEnabled(toggleId: String, defaultValue: Boolean): Boolean { - return unleash.isEnabled(toggleId, defaultValue) - } - } - - private val featureToggleController = FeatureToggleController(unleashService) - - @Test - fun `skal funksjonsbryte på miljø`() { - Assertions.assertThat(featureToggleController.sjekkFunksjonsbryter("test")).isTrue() - } - - @Test - fun `skal funksjonsbryte på miljø for ikke-definert bryter`() { - Assertions.assertThat(featureToggleController.sjekkFunksjonsbryter("Ukjent", true)).isTrue() - } -} diff --git "a/src/test/kotlin/no/nav/familie/ef/s\303\270knad/api/FeatureToggleControllerTest.kt" "b/src/test/kotlin/no/nav/familie/ef/s\303\270knad/api/FeatureToggleControllerTest.kt" deleted file mode 100644 index 966bed985..000000000 --- "a/src/test/kotlin/no/nav/familie/ef/s\303\270knad/api/FeatureToggleControllerTest.kt" +++ /dev/null @@ -1,42 +0,0 @@ -package no.nav.familie.ef.søknad.api - -import io.getunleash.FakeUnleash -import no.nav.familie.ef.søknad.featuretoggle.FeatureToggleService -import org.assertj.core.api.Assertions -import org.junit.jupiter.api.Test - -internal class FeatureToggleControllerTest { - private val fakeUnleash = FakeUnleash() - - private val fakeUnleashService = object : FeatureToggleService { - override fun isEnabled(toggleId: String, defaultValue: Boolean): Boolean { - return fakeUnleash.isEnabled(toggleId, defaultValue) - } - } - - private val featureToggleController = FeatureToggleController(fakeUnleashService) - - @Test - fun `skal svare true for påskrudde funksjonsbrytere`() { - fakeUnleash.enable("bryter1") - - Assertions.assertThat(featureToggleController.sjekkFunksjonsbryter("bryter1", null)).isTrue() - Assertions.assertThat(featureToggleController.sjekkFunksjonsbryter("bryter2", null)).isFalse() - } - - @Test - fun `skal gi standardverdi for manglende funksjonsbrytere`() { - fakeUnleash.enable("bryter1") - - Assertions.assertThat(featureToggleController.sjekkFunksjonsbryter("bryter1", false)).isTrue() - Assertions.assertThat(featureToggleController.sjekkFunksjonsbryter("bryter2", true)).isTrue() - } - - @Test - fun `skal funksjonsbryte på miljø`() { - fakeUnleash.enable("bryter1") - - Assertions.assertThat(featureToggleController.sjekkFunksjonsbryter("bryter1", false)).isTrue() - Assertions.assertThat(featureToggleController.sjekkFunksjonsbryter("bryter2", true)).isTrue() - } -} diff --git a/src/test/resources/application-local.yml b/src/test/resources/application-local.yml index dc1291208..d9a54d144 100644 --- a/src/test/resources/application-local.yml +++ b/src/test/resources/application-local.yml @@ -14,13 +14,6 @@ familie: mottak: uri: http://localhost:8092/api -funksjonsbrytere: - enabled: false - unleash: - uri: http://localhost:4242/api - environment: local - applicationName: familie-ef-soknad-api - cors: allowed_origins: - "http://localhost:3000" @@ -51,3 +44,7 @@ TOKEN_X_PRIVATE_JWK: '{ "kid" : "mock-oauth2-server-key", "n" : "" }' + +UNLEASH_SERVER_API_URL: http://localhost:4242/api +UNLEASH_SERVER_API_TOKEN: token +NAIS_APP_NAME: familie-ef-mottak From c0c9f7dff86bc002ea01e5d9dce4914fd80a7fef Mon Sep 17 00:00:00 2001 From: Ole Christian Kvernberg <42737566+olekvernberg@users.noreply.github.com> Date: Thu, 14 Sep 2023 14:05:27 +0200 Subject: [PATCH 2/2] Fjerner toggle brukt til testing i prod (#895) --- .../nav/familie/ef/s\303\270knad/api/FeatureToggleController.kt" | 1 - 1 file changed, 1 deletion(-) diff --git "a/src/main/kotlin/no/nav/familie/ef/s\303\270knad/api/FeatureToggleController.kt" "b/src/main/kotlin/no/nav/familie/ef/s\303\270knad/api/FeatureToggleController.kt" index 20568bfb2..cc0dbb0db 100644 --- "a/src/main/kotlin/no/nav/familie/ef/s\303\270knad/api/FeatureToggleController.kt" +++ "b/src/main/kotlin/no/nav/familie/ef/s\303\270knad/api/FeatureToggleController.kt" @@ -17,7 +17,6 @@ class FeatureToggleController(private val featureToggleService: FeatureToggleSer val funksjonsbrytere = listOf( "familie.ef.soknad.feilsituasjon", "familie.ef.soknad.nynorsk", - "test.environment", ) @GetMapping