You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add support for volume mounted directories where the filename becomes
the property key and the file contents becomes the value.
Support is provided via a dedicated `VolumeMountDirectoryPropertySource`
class which can either be used directly, or via a "volumemount:/..."
`spring.config.import` location.
Closesgh-19990
Co-authored-by: Phillip Webb <[email protected]>
When running applications on a cloud platform (such as Kubernetes) you often need to read config values that the platform supplies.
686
+
It's not uncommon to use environment variables for such purposes, but this can have drawbacks, especially if the value is supposed to be kept secret.
687
+
688
+
As an alternative to environment variables, many cloud platforms now allow you to map configuration into mounted data volumes.
689
+
For example, Kubernetes can volume mount both https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#populate-a-volume-with-data-stored-in-a-configmap[`ConfigMaps`] and https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-files-from-a-pod[`Secrets`].
690
+
691
+
There are two common volume mount patterns that can be use:
692
+
693
+
. A single file contains a complete set of properties (usually written as YAML).
694
+
. Multiple files are written to a directory with the filename becoming the '`key`' and the contents becoming the '`value`'.
695
+
696
+
For the first case, you can import the YAML or Properties file directly using `spring.config.import` as described <<boot-features-external-config-files-importing,above>>.
697
+
For the second case, you need to use the `volumemount:` prefix so that Spring Boot knows it needs to expose all the files as properties.
698
+
699
+
As an example, let's imagine that Kubernetes has mounted the following volume:
700
+
701
+
[source,indent=0]
702
+
----
703
+
etc/
704
+
config/
705
+
myapp/
706
+
username
707
+
password
708
+
----
709
+
710
+
The contents of the `username` file would be a config value, and the contents of `password` would be a secret.
711
+
712
+
To import these properties, you can add the following to your `application.properties` file:
713
+
714
+
[source,properties,indent=0]
715
+
----
716
+
spring.config.import=volumemount:/etc/config
717
+
----
718
+
719
+
You can then access or inject `myapp.username` and `myapp.password` properties from the `Environment` in the usual way.
720
+
721
+
TIP: Volume mounted values can be bound to both string `String` and `byte[]` types depending on the contents expected.
The values in `application.properties` and `application.yml` are filtered through the existing `Environment` when they are used, so you can refer back to previously defined values (for example, from System properties).
0 commit comments