Helm: Can replace string in a file?

Created on 15 May 2018  ·  3Comments  ·  Source: helm/helm

Hi,
I am having an issue when I am trying to deploy a configmap using helm. Issue is explained as follows.
Lets assume I have helm chart which has the folder structure as follows.

├── charts
├── Chart.yaml
├── conf
│   └── tee.xml
├── templates
│   └── ConfigMap.yaml
└── values.yaml

tee.xml is shown below.

<note>
<to>Tove</to>
<from>${name}</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

ConfigMap.yaml is shown below.

apiVersion: v1
kind: ConfigMap
metadata:
  name: mytest-var
xml:
  {{ .Values.from }}
data:
  {{- $file := .Files }}
  {{- $var := .Values.from }}
  {{- range $path, $byte := .Files.Glob "conf/*" }}
  {{- $list := $path | splitList "/"}}
  {{- $length := len $list }}
  {{- $last := add $length -1 }}
  {{ index $list $last }}: |-
    {{- range $file.Lines $path }}
    {{ . }}
    {{- end }}
  {{- end }}

part of the values.yaml is shown below.
from: Shashi

When I run the helm install command, I want to replace the value ${name} which is located in tee.xml file (<from>${name}</from>) from a value which is set in values.yaml. Is this possible using helm and if it is possible can you please help me to do the relevant changes?

questiosupport

Most helpful comment

Hi,

I was able to find the solution to above issue by using "replace". I have attached the relevant helm files below.

ConfigMap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: mytest-var
xml:
  {{ .Values.from }}
data:
  {{- $file := .Files }}
  {{- $var := .Values.from }}
  {{- range $path, $byte := .Files.Glob "conf/*" }}
  {{- $list := $path | splitList "/"}}
  {{- $length := len $list }}
  {{- $last := add $length -1 }}
  {{ index $list $last }}: |-
    {{- range $line := $file.Lines $path }}
    {{ $line | replace "John" $var }}
    {{- end }}
  {{- end }}

tee.xml

<note>
<to>Tove</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

Part of values.yaml
from: Cigra

By running helm install we can create the config map which is replaced the key "John" from the word "Cigra"

Please tell me this is the correct way?

All 3 comments

Hi,

I was able to find the solution to above issue by using "replace". I have attached the relevant helm files below.

ConfigMap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: mytest-var
xml:
  {{ .Values.from }}
data:
  {{- $file := .Files }}
  {{- $var := .Values.from }}
  {{- range $path, $byte := .Files.Glob "conf/*" }}
  {{- $list := $path | splitList "/"}}
  {{- $length := len $list }}
  {{- $last := add $length -1 }}
  {{ index $list $last }}: |-
    {{- range $line := $file.Lines $path }}
    {{ $line | replace "John" $var }}
    {{- end }}
  {{- end }}

tee.xml

<note>
<to>Tove</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

Part of values.yaml
from: Cigra

By running helm install we can create the config map which is replaced the key "John" from the word "Cigra"

Please tell me this is the correct way?

If it works, it works. Using replace to replace every instance of a substring within a given string sounds sensible to me.

I agree -- using replace sounds like a pretty good option in this case.

@KavinduZoysa given this solution works, let me go ahead and close out this issue. Feel free to re-open if this didn't work quite as expected or if you have any other related questions. Thanks!

Was this page helpful?
0 / 5 - 0 ratings