0

I am setting an environment variable in a docker container whose value I receive at run time.

  env:
    - name: POD_INFO
      value: {{ Values.resolved_when_the_container_starts }}

I want to use this value inside an XML file. Is there a way I can do this ?

Something like

<property>
   <name>pod.info</name>
   <value>this place should pick up the value from that environment variable</value>
</property>
The_Lost_Avatar
  • 992
  • 5
  • 15
  • 35
  • 1
    Is this actually a Kubernetes question? Helm? Where does the XML file come from? (Could you use a Helm template to create it in a Kubernetes ConfigMap object?) – David Maze Apr 06 '20 at 21:54
  • it seems like this is helm, so the XML is inside the container? – paltaa Apr 07 '20 at 02:53
  • I wanted to give as much context as possible. As such I put in the kubernetes tag. @DavidMaze. CM is not an option here, since I am getting POD_INFO when the container starts up. Think of it as a concatenated string between POD_HOST(which is a kubernetes standard POD_HOST ) and then some code that creates a namespace and service . So POD_INFO would be POD_HOST+Namespace+service name. And namespace is randomly generated name. As such I cant hardcode it into a configmap. – The_Lost_Avatar Apr 07 '20 at 03:51
  • @paltaa , Yes, its a helm template. So the XML is inside the container. – The_Lost_Avatar Apr 07 '20 at 03:55

2 Answers2

0

Neither Kubernetes nor Helm provide a way to template ConfigMaps or any other files. What you're probably looking for is init container, which starts before your container and which can execute any custom script you want. Then, you can modify the configuration file with your environment variable.

mario
  • 9,858
  • 1
  • 26
  • 42
Rafał Leszko
  • 4,939
  • 10
  • 19
0

You could use envsubst and call it in your entrypoint.sh script. You will have to define your variable first and then substitute them with envsubst.

kminehart explaines very well how envsubst works in one of the github cases.

# mytemplate.tmpl  
apiVersion: extensions/v1beta1  
kind: Deployment  
# ...  
architecture: ${GOOS}  
GOOS=amd64 envsubst < mytemplate.tmpl > mydeployment.yaml  
# mydeployment.yaml  
apiVersion: extensions/v1beta1  
kind: Deployment  
# ...  
architecture: amd64  

Another way of env substition is sed. Here is a good article that shows how it works.

sed -i -g "s/[target_expression]/[replace_expression/g" <file> 
acid_fuji
  • 6,287
  • 7
  • 22