Skip to content

Commit

Permalink
Felles Unleash-pakke for kobling mot Unleash Next (#721)
Browse files Browse the repository at this point in the history
* 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
3 people authored Aug 25, 2023
1 parent 6b28bd8 commit 3bcaf53
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<module>util</module>
<module>kafka</module>
<module>valutakurs-klient</module>
<module>unleash</module>
</modules>

<dependencyManagement>
Expand Down
19 changes: 19 additions & 0 deletions unleash/README.md
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
82 changes: 82 additions & 0 deletions unleash/pom.xml
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>
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 unleash/src/main/kotlin/no/nav/familie/unleash/UnleashConfig.kt
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
}

0 comments on commit 3bcaf53

Please sign in to comment.