Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify the spring.profiles.active param with a value from an environment variable using fabric8 maven plugin?

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?

like image 506
codependent Avatar asked Sep 13 '18 16:09

codependent


People also ask

How do you define beans for a specific profile?

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.

How do I select a profile in spring boot?

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.

What is Spring_profiles_active?

SPRING_PROFILES_ACTIVE is the environment variable to override/pick Spring profile.


1 Answers

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.

like image 173
Ryan Dawson Avatar answered Nov 15 '22 09:11

Ryan Dawson