-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Felles Unleash-pakke for kobling mot Unleash Next (#721)
* Ny felles modul med Unleash-konfigurasjon * Renamet implementasjoner av UnleashService og byttet Component -> Configuration * Lagt til enkel README * Lagt til destroy på UnleashService som stenger ned unleashklienten (#722) * Bruker UnleashConfig logger --------- Co-authored-by: Tor Nærland <[email protected]> Co-authored-by: Johan Blomgren <[email protected]>
- Loading branch information
1 parent
6b28bd8
commit 3bcaf53
Showing
5 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Unleash | ||
|
||
I hver enkelt applikasjon man skal bruke feature-toggles, blir man nødt til å legge inn `@ComponentScan("no.nav.familie.unleash", ...)` i App-configen. Deretter kan man ta ibruk servicen `UnleashService` som tilbyr `isEnabled()`-metoden. | ||
|
||
Hvor vidt man går mot unleash eller ikke kan styres med app-properties: | ||
``` | ||
// Default | ||
unleash: | ||
enabled: true | ||
// Dersom man ønsker at alle toggles skal gi isEnabled = false eller default verdi der det er definert | ||
unleash: | ||
enabled: false | ||
``` | ||
|
||
Pakka krever følgende miljøvariabler: | ||
* UNLEASH_SERVER_API_URL (se [Unleash-doc](https://docs.nais.io/addons/unleash-next/)) | ||
* UNLEASH_SERVER_API_TOKEN (se [Unleash-doc](https://docs.nais.io/addons/unleash-next/)) | ||
* NAIS_APP_NAME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>no.nav.familie.felles</groupId> | ||
<artifactId>felles</artifactId> | ||
<version>${revision}${sha1}${changelist}</version> | ||
</parent> | ||
|
||
<artifactId>unleash</artifactId> | ||
<version>${revision}${sha1}${changelist}</version> | ||
<name>Felles - Unleash</name> | ||
<packaging>jar</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<artifactId>kotlin-reflect</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.getunleash</groupId> | ||
<artifactId>unleash-client-java</artifactId> | ||
<version>8.2.1</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<sourceDirectory>src/main/kotlin</sourceDirectory> | ||
<testSourceDirectory>src/test/kotlin</testSourceDirectory> | ||
|
||
<plugins> | ||
<plugin> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<artifactId>kotlin-maven-plugin</artifactId> | ||
<version>${kotlin.version}</version> | ||
<executions> | ||
<execution> | ||
<id>compile</id> | ||
<phase>compile</phase> | ||
<goals> | ||
<goal>compile</goal> | ||
</goals> | ||
</execution> | ||
<execution> | ||
<id>test-compile</id> | ||
<phase>test-compile</phase> | ||
<goals> | ||
<goal>test-compile</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>compile</id> | ||
<phase>compile</phase> | ||
<goals> | ||
<goal>compile</goal> | ||
</goals> | ||
</execution> | ||
<execution> | ||
<id>testCompile</id> | ||
<phase>test-compile</phase> | ||
<goals> | ||
<goal>testCompile</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
43 changes: 43 additions & 0 deletions
43
unleash/src/main/kotlin/no/nav/familie/unleash/DefaultUnleashService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package no.nav.familie.unleash | ||
|
||
import io.getunleash.DefaultUnleash | ||
import io.getunleash.UnleashContext | ||
import io.getunleash.UnleashContextProvider | ||
import io.getunleash.util.UnleashConfig | ||
|
||
class DefaultUnleashService( | ||
val apiUrl: String, | ||
val apiToken: String, | ||
val appName: String | ||
) : UnleashService { | ||
|
||
private val defaultUnleash: DefaultUnleash | ||
|
||
init { | ||
|
||
defaultUnleash = DefaultUnleash( | ||
UnleashConfig.builder() | ||
.appName(appName) | ||
.unleashAPI("$apiUrl/api") | ||
.apiKey(apiToken) | ||
.unleashContextProvider(lagUnleashContextProvider()).build() | ||
) | ||
} | ||
|
||
private fun lagUnleashContextProvider(): UnleashContextProvider { | ||
return UnleashContextProvider { | ||
UnleashContext.builder() | ||
.appName(appName) | ||
.build() | ||
} | ||
} | ||
|
||
override fun isEnabled(toggleId: String, defaultValue: Boolean): Boolean { | ||
return defaultUnleash.isEnabled(toggleId, defaultValue) | ||
} | ||
|
||
override fun destroy() { | ||
// Spring trigger denne ved shutdown. Gjøres for å unngå at unleash fortsetter å gjøre kall ut | ||
defaultUnleash.shutdown() | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
unleash/src/main/kotlin/no/nav/familie/unleash/UnleashConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package no.nav.familie.unleash | ||
|
||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.DisposableBean | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
@EnableConfigurationProperties(UnleashProperties::class) | ||
open class UnleashConfig( | ||
private val featureToggleProperties: UnleashProperties, | ||
@Value("\${UNLEASH_SERVER_API_URL}") val apiUrl: String, | ||
@Value("\${UNLEASH_SERVER_API_TOKEN}") val apiToken: String, | ||
@Value("\${NAIS_APP_NAME}") val appName: String | ||
) { | ||
|
||
@Bean | ||
open fun unleashNext(): UnleashService = | ||
if (featureToggleProperties.enabled) { | ||
DefaultUnleashService(apiUrl = apiUrl, apiToken = apiToken, appName = appName) | ||
} else { | ||
logger.warn( | ||
"Funksjonsbryter-funksjonalitet er skrudd AV. " + | ||
"isEnabled gir 'false' med mindre man har oppgitt en annen default verdi." | ||
) | ||
lagDummyUnleashService() | ||
} | ||
|
||
private fun lagDummyUnleashService(): UnleashService { | ||
return object : UnleashService { | ||
override fun isEnabled(toggleId: String, defaultValue: Boolean): Boolean { | ||
return System.getenv(toggleId).run { toBoolean() } || defaultValue | ||
} | ||
|
||
override fun destroy() { | ||
// Dummy featureToggleService trenger ikke destroy, då den ikke har en unleash å lukke | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
|
||
private val logger = LoggerFactory.getLogger(UnleashConfig::class.java) | ||
} | ||
} | ||
|
||
@ConfigurationProperties("unleash") | ||
class UnleashProperties( | ||
val enabled: Boolean = true | ||
) | ||
|
||
interface UnleashService : DisposableBean { | ||
|
||
fun isEnabled(toggleId: String): Boolean { | ||
return isEnabled(toggleId, false) | ||
} | ||
|
||
fun isEnabled(toggleId: String, defaultValue: Boolean): Boolean | ||
} |