Search code examples
openshift

openshift 3.11 - oc process - how to pass env variables?


I am looking to pass environment variable in my openshift oc process command.

I see option for param file, but nothing for env variable.

Question: Are params = environment variables? I mean can i set env variables using this option? I tried this but I did not get any env variable set after the deployment was done.

I am going through below document. https://docs.openshift.com/container-platform/3.11/dev_guide/templates.html

Only way I am achieving setting up my env variables is like below

oc process -f helloworld.yaml | oc create -f -
curl http://servertofetchenvironmentvariables:5005/env/dev/helloworld | oc set env dc/helloworld -

This ends up two deployments. Any lead on resolving this and making it as one command will be helpful. I have to use template to create my application.


Solution

  • Question: Are params = environment variables?

    No, parameters only apply to your template file. Your template file contains placeholders such as "${MY_PASSWORD}", which are then replaced when using oc process.

    I mean can i set env variables using this option?

    You can, but you would need to edit your template file to include all these environment variables and the relevant placeholder.

    Only way I am achieving setting up my env variables is like below

    That should definitely work, as you would then update the created DeploymentConfig (dc/helloworld in your case) with your new environment variables.

    A good alternative could be to populate your environment variables using a ConfigMap (so having them totally separate from your Deployment) using envFrom like so:

    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: k8s.gcr.io/busybox
          command: [ "/bin/sh", "-c", "env" ]
          envFrom:
          - configMapRef:
              name: special-config
      restartPolicy: Never
    

    This would also decouple your configuration from your Deployment and you could store / change your environment variables in your ConfigMap.

    Source: https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#configure-all-key-value-pairs-in-a-configmap-as-container-environment-variables