Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Initiell test versjon av henting av record metadata basert på traceId
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsmsa committed Feb 21, 2024
1 parent 37983c4 commit 204f25b
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 5 deletions.
70 changes: 70 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Build, push, and deploy

on: [push]
env:
IMAGE: europe-north1-docker.pkg.dev/${{ vars.NAIS_MANAGEMENT_PROJECT_ID }}/paw/${{ github.event.repository.name }}
jobs:
build:
name: Build and push Docker container
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
packages: write
outputs:
image: ${{ steps.docker-build-push.outputs.image }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
with:
java-version: 21
distribution: temurin
cache: gradle
- name: Set version
run: echo "VERSION=$(date +'%y.%m.%d').${{ github.run_number }}-${{ github.run_attempt }}" >> $GITHUB_ENV
- name: Login GAR
uses: nais/login@v0
with:
project_id: ${{ vars.NAIS_MANAGEMENT_PROJECT_ID }}
identity_provider: ${{ secrets.NAIS_WORKLOAD_IDENTITY_PROVIDER }}
team: paw
- name: Build and push image and artifacts with Gradle
id: docker-build-push
working-directory: ./
run: |
echo "image=${{ env.IMAGE }}:${{ env.VERSION }}" >> $GITHUB_OUTPUT
echo -Pversion=${{ env.VERSION }} -Pimage=${{ env.IMAGE }} build test jib
./gradlew -Pversion=${{ env.VERSION }} -Pimage=${{ env.IMAGE }} build test jib
echo "DIGEST=$(cat app/build/jib-image.digest)" >> $GITHUB_ENV
env:
ORG_GRADLE_PROJECT_githubPassword: ${{ secrets.GITHUB_TOKEN }}
- name: Attest and sign
uses: nais/[email protected]
with:
image_ref: ${{ env.IMAGE }}@${{ env.DIGEST }}
deploy-dev:
name: Deploy to dev-gcp
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: nais/deploy/actions/deploy@v2
env:
CLUSTER: dev-gcp
RESOURCE: nais/nais.yaml
VAR: image=${{ needs.build.outputs.image }},kafka=nav-dev,domain=dev.nav.no

deploy-prod:
# if: github.ref == 'refs/heads/main'
if: false
name: Deploy to prod-gcp
needs: [build, deploy-dev]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: nais/deploy/actions/deploy@v2
env:
TEAM: paw
CLUSTER: prod-gcp
RESOURCE: nais/nais.yaml
VAR: image=${{ needs.build.outputs.image }},kafka=nav-prod,domain=nav.no
40 changes: 38 additions & 2 deletions kafka-app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
plugins {
kotlin("jvm") version "1.9.20"
id("com.google.cloud.tools.jib") version "3.4.0"
application
}
val jvmVersion = JavaVersion.VERSION_21
val image: String? by project

val pawUtilsVersion = "24.01.11.9-1"
val pawUtilsVersion = "24.02.06.10-1"
val hopliteVersion = "2.8.0.RC3"

val navCommonModulesVersion = "3.2023.10.23_12.41-bafec3836d28"
val tokenSupportVersion = "3.1.5"

dependencies {
implementation("com.sksamuel.hoplite:hoplite-core:$hopliteVersion")
implementation("no.nav.paw.kafka:kafka:$pawUtilsVersion")
implementation("no.nav.paw.hoplite-config:hoplite-config:$pawUtilsVersion")
implementation("org.apache.kafka:kafka-clients:3.6.1")
implementation("io.confluent:kafka-avro-serializer:7.6.0")
implementation(pawObservability.bundles.ktorNettyOpentelemetryMicrometerPrometheus)

}

application {
Expand All @@ -17,6 +28,31 @@ application {

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
languageVersion = JavaLanguageVersion.of(jvmVersion.majorVersion)
}
}

repositories {
mavenCentral()
maven {
url = uri("https://packages.confluent.io/maven/")
}
mavenNav("paw-arbeidssokerregisteret")
}

fun RepositoryHandler.mavenNav(repo: String): MavenArtifactRepository {
val githubPassword: String by project

return maven {
setUrl("https://maven.pkg.github.com/navikt/$repo")
credentials {
username = "x-access-token"
password = githubPassword
}
}
}

jib {
from.image = "ghcr.io/navikt/baseimages/temurin:${jvmVersion.majorVersion}"
to.image = "${image ?: rootProject.name }:${project.version}"
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,88 @@
package no.nav.paw.arbeidssokerregisteret.debug.app

import io.confluent.kafka.serializers.KafkaAvroDeserializer
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import no.nav.paw.config.hoplite.loadNaisOrLocalConfiguration
import no.nav.paw.config.kafka.KAFKA_CONFIG
import no.nav.paw.config.kafka.KafkaConfig
import no.nav.paw.config.kafka.KafkaFactory
import org.apache.avro.generic.GenericRecord
import org.apache.kafka.clients.consumer.Consumer
import org.apache.kafka.clients.consumer.ConsumerRecord
import org.apache.kafka.common.TopicPartition
import org.apache.kafka.common.serialization.LongDeserializer
import java.time.Duration
import java.time.Instant
import java.util.UUID

const val OPPLYSNINGER_TOPIC = "paw.opplysninger-om-arbeidssoeker-beta-v7"
const val PERIODE_TOPIC = "paw.arbeidssokerperioder-beta-v7"
const val PROFILERING = "paw.arbeidssoker-profilering-beta-v2"

val topics = listOf(OPPLYSNINGER_TOPIC, PERIODE_TOPIC, PROFILERING)

fun main() {
val kafkaConfig = loadNaisOrLocalConfiguration<KafkaConfig>(KAFKA_CONFIG)
val kafkaConfig = loadNaisOrLocalConfiguration<KafkaConfig>("kafka")
val kafkaFactory = KafkaFactory(kafkaConfig)

fun consumer(): Consumer<Long, Any> = kafkaFactory.createConsumer(
groupId = "paw-arbeidssokerregisteret-debug-app-${UUID.randomUUID()}",
clientId = "paw-arbeidssokerregisteret-debug-app-${UUID.randomUUID()}",
keyDeserializer = LongDeserializer::class,
valueDeserializer = KafkaAvroDeserializer::class
)

embeddedServer(Netty, port = 8080) {
routing {
get("/isAlive") {
call.respondText("I'm alive!")
}
get("/isReady") {
call.respondText("I'm ready!")
}
get("api-topics/by-trace-id/{traceId}") {
val traceId: String? = call.parameters["traceId"]
val partition: Int? = call.request.queryParameters["partition"]?.toIntOrNull()
if (traceId == null || partition == null) {
call.respond(HttpStatusCode.BadRequest, "Missing params")
return@get
} else {
val topicPartitions = topics.map { topic -> TopicPartition(topic, partition) }
val records = consumer().use { consumer ->
consumer.assign(topicPartitions)
consumer.seekToBeginning(topicPartitions)
consumer.asSequence()
.filter { record ->
val traceparent = record.headers()
.lastHeader("traceparent")?.value()
?.toString(Charsets.UTF_8)
traceparent?.contains(traceId) == true
}.map { record -> record.timestamp() to (record.value() as GenericRecord) }
.map { (timestamp, generic) ->
listOfNotNull(
"schema" to generic.schema.name,
"timestamp" to Instant.ofEpochMilli(timestamp)
.toString(),
"id" to generic.get("id")?.toString(),
"ref:periodeId" to generic.get("periodeId")
?.toString(),
"ref:opplysningerOmArbeidssokerId" to generic.get("opplysningerOmArbeidssokerId")
?.toString(),
).toMap()
}.toList()
}
call.respond(records)
}
}
}
}.start(wait = true)
}

fun <K, V> Consumer<K, V>.asSequence(): Sequence<ConsumerRecord<K, V>> =
generateSequence {
this.poll(Duration.ofSeconds(2))
}.flatten()
35 changes: 35 additions & 0 deletions nais/nais.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: nais.io/v1alpha1
kind: Application
metadata:
name: ppaw-arbeidssokerregisteret-feilsoeking
namespace: paw
labels:
team: paw
spec:
image: {{ image }}
port: 8080
replicas:
min: 1
max: 1
liveness:
path: /isAlive
initialDelay: 10
readiness:
path: /isReady
initialDelay: 10
kafka:
pool: {{ kafka }}
resources:
limits:
memory: 3048Mi
requests:
memory: 2048Mi
azure:
application:
enabled: true
tenant: nav.no
claims:
groups:
- id: 88b552ab-2cd9-41e1-bd44-3868605d6f6a
ingresses:
- https://paw-arbeidssoekerregisteret-feilsoeking.intern.{{ domain }}
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencyResolutionManagement {
}
versionCatalogs {
create("pawObservability") {
from("no.nav.paw.observability:observability-version-catalog:23.10.25.8-1")
from("no.nav.paw.observability:observability-version-catalog:24.02.20.10-1")
}
}
}

0 comments on commit 204f25b

Please sign in to comment.