<p>In my Kubernetes cluster, I have a ConfigMap object containing the address of my Postgres pod. It was created with the following YAML:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-configmap
data:
  database_url: postgres-service
</code></pre>
<p>Now I reference this value in one of my Deployment's configuration:</p>
<pre><code>env:
 - name: DB_ADDRESS
   valueFrom:
     configMapKeyRef:
       name: postgres-configmap
       key: database_url
</code></pre>
<p>This deployment is a Spring Boot application that intends to communicate with the database. Thus it reads the database's URL from the DB_ADDRESS environment variable. (ignore the default values, those are used only during development)</p>
<pre><code>datasource:
    url: ${DB_ADDRESS:jdbc:postgresql://localhost:5432/users}
    username:  ${POSTGRES_USER:postgres}
    password:  ${POSTGRES_PASSWORD:mysecretpassword}
</code></pre>
<p>So, according to the logs, the problem is that the address has to have the <code>jdbc:postgresql://</code> prefix. Either in the ConfigMap's YAML or in the application.yml I would need to concatenate the prefix protocol string with the variable. Any idea how to do it in yml or suggestion of some other workaround?</p>

Howard Fring
<p>Kubernetes environment variable declarations can <a href="https://kubernetes.io/docs/tasks/inject-data-application/define-interdependent-environment-variables/" rel="nofollow noreferrer">embed the values of other environment variables</a>.  This is the only string manipulation that Kubernetes supports, and it pretty much only works in <code>env:</code> blocks.</p>
<p>For this setup, once you've retrieved the database hostname from the ConfigMap, you can then embed it into a more complete <code>SPRING_DATASOURCE_URL</code> environment variable:</p>
<pre class="lang-yaml prettyprint-override"><code>env:
 - name: DB_ADDRESS
   valueFrom:
     configMapKeyRef:
       name: postgres-configmap
       key: database_url
 - name: SPRING_DATASOURCE_URL
   value: 'jdbc:postgresql://$(DB_ADDRESS):5432/users'
</code></pre>
<p>You might similarly parameterize the port (though it will almost always be the standard port 5432) and database name.  Avoid putting these settings in a Spring profile YAML file, where you'll have to rebuild your application if any of the deploy-time settings change.</p>
