OpenShift CLI: Import environment from a secret

A quick howto to import environment from a secret using the CLI.

The YAML way

Create a secret.yaml file

apiVersion: v1
kind: Secret
metadata:
  name: test-secret
data:
  username: bXktYXBw
  password: Mzk1MjgkdmRnN0pi

and then use valueFrom: secretKeyRef to get the value.

apiVersion: v1
kind: Pod
metadata:
  name: secret-envars-test
spec:
  containers:
  - name: envars-test-container
    image: nginx
    env:
    - name: SECRET_USERNAME
      valueFrom:
        secretKeyRef:
          name: test-secret
          key: username
    - name: SECRET_PASSWORD
      valueFrom:
        secretKeyRef:
          name: test-secret
          key: password

The CLI way

# Create a secret
$ oc create secret generic test-secret --from-literal=username='my-app' --from-literal=password='39528$vdg7Jb'

$ oc get deploymentconfig 
NAME        REVISION   DESIRED   CURRENT   TRIGGERED BY
myapp       1          1         1         config,image(myapp:latest)

# Import environment from a secret to your deploymentconfig
$ oc set env --from=secret/test-secret dc/myapp

# Check if the env is imported
$ oc exec -it myapp-1-g7m44 -- env | egrep 'USERNAME|PASSWORD'
PASSWORD=39528$vdg7Jb
USERNAME=my-app

I hope that helps.