spring-cloud / spring-cloud-kubernetes

Kubernetes integration with Spring Cloud Discovery Client, Configuration, etc...
Apache License 2.0
3.44k stars 1.03k forks source link

Change the configuration to use property 'spring.config.import' for mounting configmaps and secrets #1673

Closed filmor732 closed 1 month ago

filmor732 commented 1 month ago

Hello everyone, I need some help with the migration of Spring Cloud Kubernetes configuration using spring.cloud.kubernetes.config.paths and spring.cloud.kubernetes.config.paths to spring.cloud.import.

This request is similar to problem https://github.com/spring-cloud/spring-cloud-kubernetes/issues/1461 but different in small details, so I haven't managed to apply this to my case yet.

My current implementation reads the configmap and secrets by mounting them into the pod by running the Spring Cloud Kubernetes application and Spring Cloud Kubernetes reads them from the filesystem - as it is described here: https://docs.spring.io/spring-cloud-kubernetes/reference/property-source-config/configmap-propertysource.html

I use the following dependencies: `

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

`

I use the following bootstrap.properties: ` spring.application.name=exampleName spring.application.version=exampleVersion spring.cloud.kubernetes.discovery.enabled=false spring.cloud.kubernetes.config.enabled=true spring.cloud.kubernetes.config.enableApi=false spring.cloud.kubernetes.config.fail-fast=true spring.cloud.kubernetes.config.paths=/etc/configmap

spring.cloud.kubernetes.secrets.enabled=true spring.cloud.kubernetes.secrets.enableApi=false spring.cloud.kubernetes.secrets.fail-fast=true spring.cloud.kubernetes.secrets.paths=/etc/secrets management.health.kubernetes.enabled=false management.info.kubernetes.enabled=false kubernetes.manifests.enabled=false ` Issue: With this configuration I get this warning in my logs: path support is deprecated and will be removed in a future release. Please use spring.config.import

According to the documentation, I should change the configuration using spring.config.import instead of spring.cloud.kubernetes.config.paths and spring.cloud.kubernetes.config.paths properties. Now I would like to make this change to spring.config.import and continue using volume mount but I can't get any further at this point. I am wondering at this point how I can bring this configuration together with Spring Cloud Kubernetes.

Questions: Which dependencies do I need if I change the configuration to spring.config.import? Can I still use the dependencies mentioned above? And which properties do I need to set in the bootstrap.properties? Do I even need a dependency on Spring Cloud Kubernetes here? Or do I just set the configuration via Spring Boot for mounting configmaps and secrets to my application?

wind57 commented 1 month ago

What did you try btw? is there a sample on github with things that you tried may be?

https://github.com/spring-cloud/spring-cloud-kubernetes/issues/1664 here you can find a discussion where a user provided a repository with an example, so may be you can "bounce" of that one.

For example take a look at that sample's application.yaml here: https://github.com/AngelJava78/kubernetes-configuration-watcher/blob/main/src/main/resources/application.yml :

#spring:
#  config:
#    import: "configtree:/etc/secrets/db-secret/"

You can see how the volumes are mounted here : https://github.com/AngelJava78/kubernetes-configuration-watcher/blob/main/Kubernetes/deployment.yaml

filmor732 commented 1 month ago

Thank you for the quick reply and the example @wind57 ! I will go through it and try to implement it.

According to your question there is no example that I have tried to recreate. I have tried to read through the raised issues and to read and understand the documentation referred to. For example here: https://github.com/spring-cloud/spring-cloud-kubernetes/issues/1239 which refers to this documentation: https://docs.spring.io/spring-cloud-kubernetes/docs/3.0.0-RC3/reference/html/#kubernetes-propertysource-implementations according to which I have to include this configuration: spring.config.import=kubernetes: Another example is here: https://github.com/spring-cloud/spring-cloud-kubernetes/issues/1461 which refers to the Spring Boot documentation https://docs.spring.io/spring-boot/docs/3.1.4/reference/htmlsingle/#features.external-config.files.importing.

Without referring to a code example, I tried to apply the hints described in the documentation. I tried to exchange the properties spring.cloud.kubernetes.config.paths and spring.cloud.kubernetes.config.paths with the property spring.config.import as seen below:

bootstrap.properties: spring.application.name=exampleName spring.application.version=exampleVersion spring.cloud.kubernetes.discovery.enabled=false spring.cloud.kubernetes.config.enabled=true spring.cloud.kubernetes.config.enableApi=false spring.cloud.kubernetes.config.fail-fast=true spring.cloud.kubernetes.secrets.enabled=true spring.cloud.kubernetes.secrets.enableApi=false spring.cloud.kubernetes.secrets.fail-fast=true management.health.kubernetes.enabled=false management.info.kubernetes.enabled=false kubernetes.manifests.enabled=false removed this two properties: spring.cloud.kubernetes.config.paths=/etc/configmap spring.cloud.kubernetes.secrets.paths=/etc/secrets added this property: spring.config.import=optional:configtree:/etc/configmap/,optional:configtree:/etc/secrets/ OR spring.config.import=optional:file:/etc/configmap,optional:file:/etc/secrets dependencies: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> <version>4.1.3</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-kubernetes-fabric8-config</artifactId> <version>3.1.2</version> </dependency>

Is it even an option to replace the property like I did? Or do I have to switch my implementation to Spring Cloud Kubernetes Watcher to use volume mount like in your example?

wind57 commented 1 month ago

watcher is only for when you watch watching enabled, like to track a change in a configmap and react to that, like a context refresh, so that changes are picked-up.

I mean, suppose you mount a configmap as volume and have a property my.property=1, that is wired and used by your app. Now you change that property to my.property=2, how will your app know to refresh so that it can use this new one? This is where the watcher is used...

Otherwise, yes, look at those samples and create a sample of your own and then we can take a look at it and figure it out.

wind57 commented 1 month ago

@ryanjbaxter can you please add the feedback tag here also? thank you

filmor732 commented 1 month ago

Hi all, the hint to look here https://github.com/AngelJava78/kubernetes-configuration-watcher/blob/main/src/main/resources/application.yml#L26-L32 was very useful!! Thank you for that @wind57. I have made a similar configuration as in this example and also looked into this documentation to understand the correct indication of the paths: https://docs.spring.io/spring-boot/reference/features/external-config.html

Summarizing, there was an error in the configuration I specified above. After some trial and error, I have now found the correct configuration for my case:

I use the following properties in my bootstrap.properties: spring.application.name=exampleName spring.application.version=exampleVersion spring.config.import=optional:/etc/configmap/application.properties,optional:configtree:/etc/secrets/

This specification can also be used: spring.config.additional-location=optional:/etc/configmap/application.properties,optional:configtree:/etc/secrets/

The following dependency is used: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> <version>4.1.3</version>

wind57 commented 1 month ago

Cool :) if your issue was fixed, you can close this one