2

I am attempting to load properties from a K8s configmap into my Spring Boot service using Spring Cloud Kubernetes Fabric8, as described in the Spring Cloud docs here.

My configmap for my service looks like this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: foo-my-service
  namespace: foo
  labels:
    helm.sh/chart: my-service-1.0.0-SNAPSHOT
    app.kubernetes.io/name: my-service
    app.kubernetes.io/instance: foo
    app.kubernetes.io/version: "1.0.0-SNAPSHOT"
    app.kubernetes.io/managed-by: Helm
data:
  spring.datasource.url: jdbc:postgresql://foo-postgresql/mydb

And my Spring Boot application.yaml looks like this:

spring:
  application:
    name: foo-my-service
  cloud:
    kubernetes:
      secrets:
        enable-api: true

I'm using Fabric8 to communicate with k8s. This dependency is brought in as org.springframework.cloud:spring-cloud-starter-kubernetes-fabric8-config:jar:3.0.1 in my Maven pom.

I'm using Spring Boot version 3.0.3 and Spring Cloud 2022.0.1.

What I expect to happen based on the Spring Cloud documentation is that my spring.datasource.url property will be set from the value in the configmap.

What's actually happening is that the property is not detected, and the Spring Boot application fails to start when it tries to use the property to run a Flyway migration.

│ Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.                                                                                                                                       │
│ Reason: Failed to determine suitable jdbc url                                                                                                                                                                                                            │

If I downgrade my Spring Boot version to 2.x and Spring Cloud to the corresponding version, then this works (my application starts up and the spring.datasource.url property is set to the value in the configmap).

How do I get this to work in Spring Boot 3?

Mark
  • 4,970
  • 5
  • 42
  • 66
  • 3
    You need to set `spring.config.import=kubernetes:`, see https://docs.spring.io/spring-cloud-kubernetes/docs/current/reference/html/#kubernetes-propertysource-implementations Otherwise you need to enable bootstrap as documented – spencergibb Mar 07 '23 at 23:27
  • Yup, that's what I was missing. Also, note the trailing colon – Mark Mar 08 '23 at 00:37

2 Answers2

0

As Spencer commented, you need to enable "config-data" support in application.yaml (see documentation here).

The thing to note is that, while we indeed added this support, at the moment it is a bit limited compared to bootstrap. For example, you can't read multiple secrets, for that you will need to still enable bootstrap.

Eugene
  • 117,005
  • 15
  • 201
  • 306
0

I faced this issue too. To solve this, I needed to insert 2 dependencies into my pom.xml.

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-kubernetes-fabric8-config</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>

The tip is the artifact spring-cloud-starter-bootstrap, as mentioned in documentation:

If you would like to load Kubernetes PropertySources during the bootstrap phase like it worked prior to the 3.0.x release you can either add spring-cloud-starter-bootstrap to your application’s classpath or set spring.cloud.bootstrap.enabled=true as an environment variable.

Fábio Almeida
  • 276
  • 3
  • 11