1

I am writing a Helm chart for a bunch of deployments. I am supplying a value which can be:

my_value: "/opt/my-path"or my_value: "/opt/my-path/"

Now I would want to make sure that there is always a single / at the end of the path.

How do I do this using Go templates?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Hedge
  • 16,142
  • 42
  • 141
  • 246

1 Answers1

6

You can trim the suffix / with trimSuffix function, docs here http://masterminds.github.io/sprig/strings.html, and to add / manually at the end. So regardless the original value you will always get a / at the end. example

values.yaml:

path_with_slash: "/my/path/"
path_without_slash: "/my/path"

inside a template file:

{{ $path_with_slash := trimSuffix "/" .Values.path_with_slash }}
{{ $path_without_slash := trimSuffix "/" .Values.path_without_slash }}
path_with_slash: "{{ $path_with_slash }}/"
path_without_slash: "{{ $path_without_slash }}/"

rendered file:

path_with_slash: "/my/path/"
path_without_slash: "/my/path/"
Yuri G.
  • 4,323
  • 1
  • 17
  • 32
  • I ended up doing it in a single line but it works as expected. Thank you `mountPath: {{ .mountPath | default "/var/config/" | trimSuffix "/" }}/{{ base .file }}` – Hedge Feb 17 '20 at 10:22