<p>my current setup involves Helm charts,Kubernetes</p>
<p>I have a requirement where i have to replace a property in configMap.yaml file with an environment variable declared in the deployment.yaml file</p>
<p>here is a section my configMap.yaml which declares a property file</p>
<pre><code>data:

  rest.properties: |

    map.dirs=/data/maps

    catalog.dir=/data/catalog

    work.dir=/data/tmp

    map.file.extension={{ .Values.rest.mapFileExtension }}

    unload.time=1

    max.flow.threads=10

    max.map.threads=50

    trace.level=ERROR

    run.mode={{ .Values.runMode }}

    {{- if eq .Values.cache.redis.location &quot;external&quot; }}

    redis.host={{ .Values.cache.redis.host }}

    {{- else if eq .Values.cache.redis.location &quot;internal&quot; }}

    redis.host=localhost

    {{- end }}

    redis.port={{ .Values.cache.redis.port }}

    redis.stem={{ .Values.cache.redis.stem }}

    redis.database={{ .Values.cache.redis.database }}

    redis.logfile=redis.log

    redis.loglevel=notice

    exec.log.dir=/data/logs

    exec.log.file.count=5

    exec.log.file.size=100

    exec.log.level=all

    synchronous.timeout=300

    {{- if .Values.global.linkIntegration.enabled }}

    authentication.enabled=false

    authentication.server=https://{{ .Release.Name }}-product-design-server:443

    config.dir=/opt/runtime/config

    {{- end }}

    {{- if .Values.keycloak.enabled }}

    authentication.keycloak.enabled={{ .Values.keycloak.enabled }}

    authentication.keycloak.serverUrl={{ .Values.keycloak.serverUrl }}

    authentication.keycloak.realmId={{ .Values.keycloak.realmId }}

    authentication.keycloak.clientId={{ .Values.keycloak.clientId }}

    authentication.keycloak.clientSecret=${HIP_KEYCLOAK_CLIENT_SECRET}

    {{- end }}
</code></pre>
<p>i need to replace ${HIP_KEYCLOAK_CLIENT_SECRET} which is defined in deployment.yaml file as shown below</p>
<pre><code>containers:
        - name: {{ .Chart.Name }}
          image: &quot;{{ .Values.global.hImageRegistry }}/{{ include &quot;image.runtime.repo&quot; . }}:{{ .Values.image.tag }}&quot;
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            {{- if .Values.keycloak.enabled }}
            - name: HIP_KEYCLOAK_CLIENT_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.keycloak.secret }}
                  key: clientSecret
            {{ end }}
</code></pre>
<p>the idea is to have the property file in the deployed pod under /opt/runtime/rest.properties</p>
<p>here is my complete deployment.yaml file</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include &quot;lnk-service.fullname&quot; . }}
  labels:
    app.kubernetes.io/name: {{ include &quot;lnk-service.name&quot; . }}
    helm.sh/chart: {{ include &quot;lnk-service.chart&quot; . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include &quot;lnk-service.name&quot; . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include &quot;lnk-service.name&quot; . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      {{- if .Values.global.hImagePullSecret }}
      imagePullSecrets:
        - name: {{ .Values.global.hImagePullSecret }}
      {{- end }}
      securityContext:
        runAsUser: 998
        runAsGroup: 997
        fsGroup: 997
      volumes:
        - name: configuration
          configMap: 
            name: {{ include &quot;lnk-service.fullname&quot; . }}-server-config
        - name: core-configuration
          configMap: 
            name: {{ include &quot;lnk-service.fullname&quot; . }}-server-core-config
        - name: hch-configuration
          configMap:
            name: {{ include &quot;lnk-service.fullname&quot; . }}-hch-config
        - name: data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            {{- if .Values.global.linkIntegration.enabled }}
            claimName: lnk-shared-px
            {{- else }}
            claimName: {{ include &quot;pvc.name&quot; . }}
            {{- end }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        - name: hch-data
          {{- if .Values.global.linkIntegration.enabled }}
          persistentVolumeClaim: 
            claimName: {{ include &quot;unicapvc.fullname&quot; . }}
          {{- else }}
          emptyDir: {}
          {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: &quot;{{ .Values.global.hImageRegistry }}/{{ include &quot;image.runtime.repo&quot; . }}:{{ .Values.image.tag }}&quot;
          #command: ['/bin/sh']
          #args: ['-c', 'echo $HIP_KEYCLOAK_CLIENT_SECRET']
          #command: [ &quot;/bin/sh&quot;, &quot;-c&quot;, &quot;export&quot; ]
          #command: [ &quot;/bin/sh&quot;, &quot;-ce&quot;, &quot;export&quot; ]
          command: [ &quot;/bin/sh&quot;, &quot;-c&quot;, &quot;export --;trap : TERM INT; sleep infinity &amp; wait&quot; ]
          #command: ['sh', '-c', 'sed -i &quot;s/REPLACEME/$HIP_KEYCLOAK_CLIENT_SECRET/g&quot; /opt/runtime/rest.properties']
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: &quot;HIP_CLOUD_LICENSE_SERVER_URL&quot;
              value: {{ include &quot;license.url&quot; . | quote }}
            - name: &quot;HIP_CLOUD_LICENSE_SERVER_ID&quot;
              value: {{ include &quot;license.id&quot; . | quote }}
            {{- if .Values.keycloak.enabled }}
            - name: HIP_KEYCLOAK_CLIENT_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.keycloak.secret }}
                  key: clientSecret
            {{ end }}
          envFrom:
            - configMapRef:
                name: {{ include &quot;lnk-service.fullname&quot; . }}-server-env
            {{- if .Values.rest.extraEnvConfigMap }}
            - configMapRef:
                name: {{ .Values.rest.extraEnvConfigMap }}
            {{- end }}
            {{- if .Values.rest.extraEnvSecret }}
            - secretRef:
                name: {{ .Values.rest.extraEnvSecret }}
            {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.image.port }}
              protocol: TCP
            - name: https
              containerPort: 8443
              protocol: TCP
          volumeMounts:
            - name: configuration
              mountPath: /opt/runtime/rest.properties
              subPath: rest.properties
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
</code></pre>
<p>i have tried init containers and replacing the string in rest.properties which works however it involves creating volumes with emptyDir.</p>
<p>can someone kindly help me if there is any simpler way to do this</p>

<p>confd will give you the solution, you can tell it to look at the configmap and  change all the environment variables that is expected by the file to the env values that had been set.</p>
