ssc327
ssc327

Reputation: 710

Use O/S Env Var for OpenShift Deployment Environment Configuration

I would like to provide an environment variable to an OpenShift pod and within that variable reference other environment variables defined in the container.

For example, I define an environment variable called JAVA_CMD_LINE in OpenShift and set it to:

$HEAP_SETTING -Djavax.net.ssl.trustStore=/var/.keystore/cacerts -jar abc.jar

Where $HEAP_SETTING is set to -XMX=1G when the container starts.

In my container, there is a startup script that looks like:

java $JAVA_CMD_LINE

What I would expect is that then the container runs, the following is executed:

java -XMX=1G -Djavax.net.ssl.trustStore=/var/.keystore/cacerts -jar abc.jar

But instead what I see is:

java '$HEAP_SETTING' -Djavax.net.ssl.trustStore=/var/.keystore/cacerts -jar abc.jar

How do I provide the variable?

Update: Adding details from the YML file.

spec:
  containers:
    - env:
        - name: OPENSHIFT_ENABLE_OAUTH
          value: 'true'
        - name: OPENSHIFT_ENABLE_REDIRECT_PROMPT
          value: 'true'
        - name: KUBERNETES_MASTER
          value: 'https://kubernetes.default:443'
        - name: KUBERNETES_TRUST_CERTIFICATES
          value: 'true'
        - name: JAVA_CMD_LINE
          value: >-
            -Djavax.net.ssl.trustStore=/var/cert/.keystore/cacerts
            -Dfile.encoding=UTF8
            $HEAP_SETTING

Update 2 - The error that I see:

+ exec java -Djavax.net.ssl.trustStore=/var/jenk-cert/.keystore/cacerts -Djavax.net.ssl.trustStorePassword=changeit -Dfile.encoding=UTF8 '$(HEAP_SETTING)' -Duser.home=/var/lib/jenkins -Djavamelody.application-name=JENKINS -jar /usr/lib/jenkins/jenkins.war
Picked up JAVA_TOOL_OPTIONS: -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Dsun.zip.disableMemoryMapping=true
Error: Could not find or load main class $(HEAP_SETTING)

Upvotes: 1

Views: 1899

Answers (1)

Graham Dumpleton
Graham Dumpleton

Reputation: 58523

Try using:

spec:
  containers:
    - env:
        - name: OPENSHIFT_ENABLE_OAUTH
          value: 'true'
        - name: OPENSHIFT_ENABLE_REDIRECT_PROMPT
          value: 'true'
        - name: KUBERNETES_MASTER
          value: 'https://kubernetes.default:443'
        - name: KUBERNETES_TRUST_CERTIFICATES
          value: 'true'
        - name: JAVA_CMD_LINE
          value: >-
            -Djavax.net.ssl.trustStore=/var/cert/.keystore/cacerts
            -Dfile.encoding=UTF8
            $(HEAP_SETTING)

Any time you are setting the value of an environment variable, if you need to compose the value from other environment variables that are already being set, you can use $(<VARNAME>) in the value.

IOW, use $(HEAP_SETTING) and not just $HEAP_SETTING.


UPDATE 1

Actually this will not work. This is because HEAP_SETTING is not in the set of environment variables you are setting via the deployment config, so it will pass the literal value $(HEAP_SETTING). This can't be used where the environment variable you are trying to use is populated by startup code in the image.

Upvotes: 3

Related Questions