I have a K8s config map that defines an ENVIRONMENT parameter.
That value is mounted as an environment variable on the deployment yaml using an excerpt in src/fabric8/deployment.yml:
spec:
template:
spec:
containers:
- env:
- name: "ENVIRONMENT"
valueFrom:
configMapKeyRef:
name: global-configmap
key: ENVIRONMENT
I would like to use that ENVIRONMENT env variable to configure the spring.active.profiles
property.
Is it supported in some way by fabric8 maven plugin? If not, can you suggest some workaround?
Use @Profile on a Bean Let's start simple and look at how we can make a bean belong to a particular profile. We use the @Profile annotation — we are mapping the bean to that particular profile; the annotation simply takes the names of one (or multiple) profiles.
The solution would be to create more property files and add the "profile" name as the suffix and configure Spring Boot to pick the appropriate properties based on the profile. Then, we need to create three application. properties : application-dev.
SPRING_PROFILES_ACTIVE is the environment variable to override/pick Spring profile.
One thing to note first is that the name given to the environment variable injected into the Pod and the key being used from the configmap don't have to match. So you could do:
- env:
- name: SPRING_PROFILES_ACTIVE
valueFrom:
configMapKeyRef:
name: global-configmap
key: ENVIRONMENT
If ENVIRONMENT is a key within the configmap called global-configmap
. If it's not then naturally you want to use whatever the key is that matches the value you're looking for (something like spring.profiles.active might be clearer if possible but from your description it sounds like you have an existing configmap called global-configmap with the key called ENVIRONMENT). I expect you'll need to call the environment variable (the name section) SPRING_PROFILES_ACTIVE because this will match to the property spring.profiles.active by relaxed binding.
Alternatively, you do have other options with the fabric8 maven plugin, which it seems you're using for generation. You could simply set an environment variable directly or set the spring.profiles.active value directly in your property file, which you could mount as a configmap.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With