Skip to content

Commit

Permalink
IS-2756: Bytte til Aiven redis cache (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
geir-waagboe authored Nov 8, 2024
1 parent 674458d commit 0073e56
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 124 deletions.
30 changes: 0 additions & 30 deletions .github/workflows/redis.yaml

This file was deleted.

7 changes: 3 additions & 4 deletions .nais/naiserator-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ spec:
claims:
extra:
- "NAVident"
envFrom:
- secret: syfoperson-redis-password
redis:
- instance: cache
access: readwrite
observability:
autoInstrumentation:
enabled: true
Expand All @@ -76,8 +77,6 @@ spec:
value: "dev-fss.pdl.pdl-api"
- name: PDL_URL
value: "https://pdl-api.dev-fss-pub.nais.io/graphql"
- name: REDIS_HOST
value: "syfoperson-redis.teamsykefravr.svc.cluster.local"
- name: SKJERMEDEPERSONERPIP_CLIENT_ID
value: "dev-gcp.nom.skjermede-personer-pip"
- name: SKJERMEDEPERSONERPIP_URL
Expand Down
7 changes: 3 additions & 4 deletions .nais/naiserator-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ spec:
claims:
extra:
- "NAVident"
envFrom:
- secret: syfoperson-redis-password
redis:
- instance: cache
access: readwrite
observability:
autoInstrumentation:
enabled: true
Expand All @@ -77,8 +78,6 @@ spec:
value: "prod-fss.pdl.pdl-api"
- name: PDL_URL
value: "https://pdl-api.prod-fss-pub.nais.io/graphql"
- name: REDIS_HOST
value: "syfoperson-redis.teamsykefravr.svc.cluster.local"
- name: SKJERMEDEPERSONERPIP_CLIENT_ID
value: "prod-gcp.nom.skjermede-personer-pip"
- name: SKJERMEDEPERSONERPIP_URL
Expand Down
33 changes: 0 additions & 33 deletions .nais/redis/redis-config.yaml

This file was deleted.

35 changes: 0 additions & 35 deletions .nais/redis/redisexporter.yaml

This file was deleted.

19 changes: 19 additions & 0 deletions src/main/kotlin/no/nav/syfo/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,33 @@ import io.ktor.server.netty.*
import no.nav.syfo.application.ApplicationState
import no.nav.syfo.application.Environment
import no.nav.syfo.application.api.apiModule
import no.nav.syfo.application.cache.RedisStore
import no.nav.syfo.client.wellknown.getWellKnown
import org.slf4j.LoggerFactory
import redis.clients.jedis.DefaultJedisClientConfig
import redis.clients.jedis.HostAndPort
import redis.clients.jedis.JedisPool
import redis.clients.jedis.JedisPoolConfig
import java.util.concurrent.TimeUnit

const val applicationPort = 8080

fun main() {
val applicationState = ApplicationState()
val environment = Environment()
val redisConfig = environment.redisConfig
val cache = RedisStore(
JedisPool(
JedisPoolConfig(),
HostAndPort(redisConfig.host, redisConfig.port),
DefaultJedisClientConfig.builder()
.ssl(redisConfig.ssl)
.user(redisConfig.redisUsername)
.password(redisConfig.redisPassword)
.database(redisConfig.redisDB)
.build()
)
)

val server = embeddedServer(
Netty,
Expand All @@ -37,6 +55,7 @@ fun main() {
applicationState = applicationState,
environment = environment,
wellKnownInternalAzureAD = wellKnownInternalAzureAD,
redisStore = cache,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package no.nav.syfo.application

import no.nav.syfo.application.cache.RedisConfig
import java.net.URI

data class Environment(
val azureAppClientId: String = getEnvVar("AZURE_APP_CLIENT_ID"),
val azureAppClientSecret: String = getEnvVar("AZURE_APP_CLIENT_SECRET"),
Expand All @@ -20,10 +23,12 @@ data class Environment(

val kodeverkClientId: String = getEnvVar("KODEVERK_CLIENT_ID"),
val kodeverkUrl: String = getEnvVar("KODEVERK_URL"),

val redisHost: String = getEnvVar("REDIS_HOST"),
val redisPort: Int = getEnvVar("REDIS_PORT", "6379").toInt(),
val redisSecret: String = getEnvVar("REDIS_PASSWORD"),
val redisConfig: RedisConfig = RedisConfig(
redisUri = URI(getEnvVar("REDIS_URI_CACHE")),
redisDB = 23, // se https://github.com/navikt/istilgangskontroll/blob/master/README.md
redisUsername = getEnvVar("REDIS_USERNAME_CACHE"),
redisPassword = getEnvVar("REDIS_PASSWORD_CACHE"),
),
)

fun getEnvVar(
Expand Down
11 changes: 1 addition & 10 deletions src/main/kotlin/no/nav/syfo/application/api/ApiModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import no.nav.syfo.client.veiledertilgang.VeilederTilgangskontrollClient
import no.nav.syfo.client.wellknown.WellKnown
import no.nav.syfo.person.api.registrerPersonApi
import no.nav.syfo.person.skjermingskode.SkjermingskodeService
import redis.clients.jedis.*

fun Application.apiModule(
applicationState: ApplicationState,
environment: Environment,
wellKnownInternalAzureAD: WellKnown,
redisStore: RedisStore,
) {
installMetrics()
installCallId()
Expand All @@ -37,15 +37,6 @@ fun Application.apiModule(
),
)
installStatusPages()
val redisStore = RedisStore(
jedisPool = JedisPool(
JedisPoolConfig(),
environment.redisHost,
environment.redisPort,
Protocol.DEFAULT_TIMEOUT,
environment.redisSecret
)
)

val azureAdClient = AzureAdClient(
azureAppClientId = environment.azureAppClientId,
Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/no/nav/syfo/application/cache/RedisConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package no.nav.syfo.application.cache

import java.net.URI

class RedisConfig(
val redisUri: URI,
val redisDB: Int,
val redisUsername: String,
val redisPassword: String,
val ssl: Boolean = true
) {
val host: String = redisUri.host
val port: Int = redisUri.port
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package no.nav.syfo.testhelper

import io.ktor.server.netty.*
import no.nav.syfo.application.ApplicationState
import no.nav.syfo.application.cache.RedisStore
import no.nav.syfo.testhelper.mock.*

class ExternalMockEnvironment {
Expand Down Expand Up @@ -32,11 +33,12 @@ class ExternalMockEnvironment {
)

val redisServer = testRedis(
port = environment.redisPort,
secret = environment.redisSecret,
port = environment.redisConfig.redisUri.port,
secret = environment.redisConfig.redisPassword,
)

val wellKnownInternalAzureAD = wellKnownInternalAzureAD()
lateinit var redisCache: RedisStore
}

fun ExternalMockEnvironment.startExternalMocks() {
Expand Down
20 changes: 20 additions & 0 deletions src/test/kotlin/no/nav/syfo/testhelper/TestApiModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,33 @@ package no.nav.syfo.testhelper

import io.ktor.server.application.*
import no.nav.syfo.application.api.apiModule
import no.nav.syfo.application.cache.RedisStore
import redis.clients.jedis.DefaultJedisClientConfig
import redis.clients.jedis.HostAndPort
import redis.clients.jedis.JedisPool
import redis.clients.jedis.JedisPoolConfig

fun Application.testApiModule(
externalMockEnvironment: ExternalMockEnvironment,
) {
val redisConfig = externalMockEnvironment.environment.redisConfig
val cache = RedisStore(
JedisPool(
JedisPoolConfig(),
HostAndPort(redisConfig.host, redisConfig.port),
DefaultJedisClientConfig.builder()
.ssl(redisConfig.ssl)
.password(redisConfig.redisPassword)
.build()
)
)

externalMockEnvironment.redisCache = cache

this.apiModule(
applicationState = externalMockEnvironment.applicationState,
environment = externalMockEnvironment.environment,
wellKnownInternalAzureAD = externalMockEnvironment.wellKnownInternalAzureAD,
redisStore = externalMockEnvironment.redisCache,
)
}
11 changes: 9 additions & 2 deletions src/test/kotlin/no/nav/syfo/testhelper/TestEnvironment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package no.nav.syfo.testhelper

import no.nav.syfo.application.ApplicationState
import no.nav.syfo.application.Environment
import no.nav.syfo.application.cache.RedisConfig
import java.net.ServerSocket
import java.net.URI

fun testEnvironment(
azureOpenIdTokenEndpoint: String = "azureTokenEndpoint",
Expand All @@ -26,8 +28,13 @@ fun testEnvironment(
istilgangskontrollUrl = istilgangskontrollUrl,
kodeverkClientId = "dev-gcp.team-rocket.kodeverk-api",
kodeverkUrl = kodeverkUrl,
redisHost = "localhost",
redisSecret = "password",
redisConfig = RedisConfig(
redisUri = URI("http://localhost:6379"),
redisDB = 0,
redisUsername = "redisUser",
redisPassword = "redisPassword",
ssl = false,
),
)

fun testAppState() = ApplicationState(
Expand Down

0 comments on commit 0073e56

Please sign in to comment.