-2

I have a project/namespace in Openshift 3.9. It has one Mysql db pod, the credentials details are stored as secrets.

enter image description here

While deploying my Springboot application, in Openshift UI, I change the Deployment Configuration to map the dynamic variables from my APP to Mysql secrets.

enter image description here

Now I want to automate this step, and how do I mention/pass these secrets while using the oc new-app cmd. I am using below command, but not sure on how to pass/map variables to secrets.

oc new-app redhat-openjdk18-openshift~https://github.com/xyzasdkjasnda/openshift-mysql

Somewhere I saw a workaround like below,

oc new-app redhat-openjdk18-openshift~https://github.com/xyzasdkjasnda/openshift-mysql | jq '.items[] | select(.kind == "DeploymentConfig") | .spec.template.spec.containers[0].env += [{"name":"db_name","valueFrom":{"secretKeyRef":{"key":"database-name","name":"mysql"}}},{"name":"db_username","valueFrom":{"secretKeyRef":{"key":"database-user","name":"mysql"}}},{"name":"db_password","valueFrom":{"secretKeyRef":{"key":"database-password","name":"mysql"}}}]' | \ oc apply --filename -

Ernesto U
  • 786
  • 3
  • 14
John Seen
  • 701
  • 4
  • 15
  • 31

1 Answers1

0

While oc new-app seems convenient, it usually is preferred to explicitly create a DeploymentConfig (or Deployment) file and check it into a code repo; use oc create -f <deployment-filename> to create the actual object in OpenShift.

See Example #3 here for how to populate environment variables with values from Secrets - that is what the "workaround" does.

Alternatively, you can still use oc new-app to create a new application, including building it from source code, which will create the DeploymentConfig you can then edit to add the environment variables from Secrets configuration.

gears
  • 690
  • 3
  • 6
  • Ok, let's say I am using a `dc.json` for deployment config, and I have added few variables like `${APP_NAME}` in `dc.json`. How do we pass these variable in `oc create -f dc.json` – John Seen Dec 03 '19 at 20:24
  • I have used the following approach: define env vars with the values to be replaced (e.g. `export APP_NAME=my-app`); pre-process the yaml file (json could work, too) that has the placeholders (e.g. `${APP_NAME}`) by `grep`-ing for what is in between `${` and `}` delimiters and `eval`-uating it as an environment variable; search-n-replace via `sed` the placeholders with the evaluated env var value; pass the result to `oc apply`. (Sorry, it probably sounds cryptic). Some solutions like [helm](https://github.com/helm/helm) allow similar use and substitution of placeholder values and more. – gears Dec 03 '19 at 21:18