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

9/13/2018

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?

-- codependent
fabric8
fabric8-maven-plugin
kubernetes
spring-boot

2 Answers

9/13/2018

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.

-- Ryan Dawson
Source: StackOverflow

9/14/2018

Another way that also worked was explicitly defining it in the JAVA_OPTIONS parameters:

spec:
  template:
    spec:
      containers:
      - env:
        - name: JAVA_OPTIONS
          value: "-Dspring.profiles.active=${ENVIRONMENT}"
        - name: ENVIRONMENT
          valueFrom:
              configMapKeyRef:
                  name: global-configmap
                  key: ENVIRONMENT
-- codependent
Source: StackOverflow