Answers: <p>Variable interpolation doesn't work when you're referencing variables from the secrets. It only works when you explicitly define variable in the YAML. Like this:</p>
  <pre><code>---
  apiVersion: v1
  kind: Pod
  metadata:
    labels:
      run: pod
    name: pod
  spec:
    containers:
    - image: nginx
      name: pod
      env:
      - name: foo_SERVICE_HOST
        value: test
      - name: foo_SERVICE_PORT
        value: &quot;80&quot;
      - name: foo_SERVICE_URL
        value: https://$(foo_SERVICE_HOST):$(foo_SERVICE_PORT)
  </code></pre>
  <p>In this case environment variable <code>foo_SERVICE_URL</code> will be set to <code>https://test:80</code></p>
  
Answers: CC BY-SA 4.0
Answers: 2024-11-08T10:03:04.753Z
Answers: 2024-11-08T10:03:04.753Z
Answers: Vasilii Angapov
Body: <p>I'm using Helm and kubernetes to deploy a number of apps, i'm trying to inject a number of environmental variables using envFrom pointing to an already defined secret.</p>
  <p>Some of the environmental variables there are referencing other environmental variables that are injected by kubernetes itself, like <code>foo_SERVICE_PORT</code> and <code>foo_SERVICE_HOST</code> variables, so the secret content is something like</p>
  <pre><code>  # Source: external-secret/templates/secret.yaml
    apiVersion: v1
    kind: Secret
    metadata:
      name: backend-secret
    data:
  -   foo_service_url: 'https://${foo_SERVICE_HOST}:${foo_SERVICE_PORT}'
  </code></pre>
  <p>I'm injecting these into the pods by using:</p>
  <pre><code>envFrom:
    - secretRef:
        name: backend-secret
  </code></pre>
  <p>The problem happens when i execute <code>env</code> from inside the pod the variable content is not interpolated, it contains the above string <code>https://${foo_SERVICE_HOST}:${foo_SERVICE_PORT}</code></p>
  <p>How can indicate to the pod that those should be interpolated?</p>
  
Tags: <kubernetes><kubernetes-helm>
Title: How to force shell interpolation in kubernetes environment variables
UserDisplayName: Alexis Delgado
ThreadText: # How to force shell interpolation in kubernetes environment variables
  
  (Asked by: username_0 on 2024-11-06)
  
  I'm using Helm and kubernetes to deploy a number of apps, i'm trying to inject a number of environmental variables using envFrom pointing to an already defined secret.
  
  Some of the environmental variables there are referencing other environmental variables that are injected by kubernetes itself, like `foo_SERVICE_PORT` and `foo_SERVICE_HOST` variables, so the secret content is something like
  
  ```
    # Source: external-secret/templates/secret.yaml
    apiVersion: v1
    kind: Secret
    metadata:
      name: backend-secret
    data:
  -   foo_service_url: 'https://${foo_SERVICE_HOST}:${foo_SERVICE_PORT}'
  
  ```
  
  I'm injecting these into the pods by using:
  
  ```
  envFrom:
    - secretRef:
        name: backend-secret
  
  ```
  
  The problem happens when i execute `env` from inside the pod the variable content is not interpolated, it contains the above string `https://${foo_SERVICE_HOST}:${foo_SERVICE_PORT}`
  
  How can indicate to the pod that those should be interpolated?
  
  **Tags:** kubernetes, kubernetes-helm
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-11-08. Score: 0
  
  Variable interpolation doesn't work when you're referencing variables from the secrets. It only works when you explicitly define variable in the YAML. Like this:
  
  ```
  ---
  apiVersion: v1
  kind: Pod
  metadata:
    labels:
      run: pod
    name: pod
  spec:
    containers:
    - image: nginx
      name: pod
      env:
      - name: foo_SERVICE_HOST
        value: test
      - name: foo_SERVICE_PORT
        value: "80"
      - name: foo_SERVICE_URL
        value: https://$(foo_SERVICE_HOST):$(foo_SERVICE_PORT)
  
  ```
  
  In this case environment variable `foo_SERVICE_URL` will be set to `https://test:80`
  
  
  ---
  
  