micronautmicronaut-kubernetes

Micronaut app fails to access values from Kubernetes config map


I'm using Micronaut 3.2.3 with the Kubernetes integration to inject configuration values from config maps and secrets.

Dependencies:

    implementation("io.micronaut.kubernetes:micronaut-kubernetes-client")
    implementation("io.micronaut.kubernetes:micronaut-kubernetes-discovery-client")

bootstrap.yml

micronaut:
  application:
    name: ingestor
  config-client:
    enabled: true

kubernetes:
  client:
    config-maps:
      enabled: true
      includes:
        - application
        - ingestor
    secrets:
      enabled: true
      includes:
        - application
        - ingestor

As you can see the application config map includes the kafka.brokers value:

kubectl get configmaps application -o yaml   
  
apiVersion: v1
data:
  application.yml: |
    kafka.brokers: xxx

kubectl get configmaps ingestor -o yaml   
  
apiVersion: v1
data:
  application.yml: |
    prop1: value1

I've added a simple singleton class the check whether the projerty can be injected:

@Singleton
class SomeConfiguration(@Value("\${kafka.brokers}") private val kafkaBrokers: String) {

    init {
        println(kafkaBrokers)
    }

}

Log traces seem to indicate that those config maps are correctly accessed:

15:59:24.206 [OkHttp https://10.222.0.1/...] -  -   DEBUG i.m.k.c.KubernetesConfigurationClient - Adding config map with name application
15:59:24.218 [OkHttp https://10.222.0.1/...] -  -   DEBUG i.m.k.c.KubernetesConfigurationClient - Adding config map with name ingestor

However the application crashes because it couln'd find it:

fun main(args: Array<String>) {
    build()
        .args(*args)
        .eagerInitSingletons(true)
15:59:25.484 [main] -  -   ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [xxx.SomeConfiguration] could not be loaded: Failed to inject value for parameter [kafkaBrokers] of class: xxx.configuration.SomeConfiguration

Message: Failed to inject value for parameter [kafkaBrokers] of class: xxx.configuration.SomeConfiguration

Message: Error resolving property value [${kafka.brokers}]. Property doesn't exist


Solution

  • This problem was kind of tricky. The key was the name of the yml file used in the config maps, both application and ingestor config maps defined it as application.yml:

    apiVersion: v1
    data:
      application.yml: |
    

    They can't overlap, so I changed the ingestor configmap to be:

    kubectl get configmaps ingestor -o yaml   
      
    apiVersion: v1
    data:
      ingestor.yml: |
        prop1: value1
    

    And now the kafka.brokers value is found and injected.