30

I have my own helm chart and I'm trying to perform split without using the _helpers.tpl in one line

my values.yaml file content:

deployment:
    domain: my.domain

I need to split domain name in my template file: my.domain

I have tried to perform this by using the following syntax:

name regex (.*)\.{{ (split .Values.deployment.domain ".")._0 }}\.{{ (split .Values.deployment.domain ".")._1 }}

or

{{- $split := .Values.deployment.domain "." . }}
name regex (.*)\.{{ first split }}\.{{ second split }}

But nothing worked

I'm trying to get the following results in my template file:

name regex (.*)\.my\.domain
Community
  • 1
  • 1
dsaydon
  • 4,421
  • 6
  • 48
  • 52

2 Answers2

56

Helm uses the sprig library to provide lots of data manipulation functions, have a look at their docs for strings. You can use the {{ split }} function to do what you want.

$parts := split "." .Values.deployment.domain
$parts._0
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
  • 34
    Thank you @alex Pliutau ,you helped me to figured it out that I had a syntax issue. Here is my final results that worked: ```{{ (split "." .Values.deployment.domain)._0 }}``` – dsaydon Dec 06 '18 at 09:56
  • 1
    How can i get last element after split? – Utkarsh Saraf Jun 23 '22 at 15:30
  • 6
    You can use `splitList` to create an array, then use `last` to retrieve the last element in the list. i.e. `{{ (splitList "." "http://www.foo.bar.com") | last | quote }}` – meappy Aug 05 '22 at 06:35
0

I needed the filename of a path, so the last element. There are 2 solutions:

Values.yaml

config:
  files:
  - includes/config/xxx/conf/db.yml
  - includes/config/xxx/conf/application.properties
  - includes/config/xxx/conf/custom.properties

Configmap.yaml

kind: ConfigMap
apiVersion: v1
metadata:
  name: xxx
  namespace: {{ .Values.namespace }}
data: 
  {{- range $file := .Values.config.files }} #$file = includes/config/xxx/conf/db.yml

  #Solution 1
  {{- $splitpath := $file | split "/" }} #$splitpath = map[_0:includes _1:config _2:leasone _3:conf _4:db.yml]
  {{- $indexlast := sub (len $splitpath) 1 }} #$indexlast = %!s(int64=4)
  {{- $index := printf "_%d" $indexlast }} #$index = _4
  {{- $filename := index $splitpath $index }} #$filename = db.yml

  #Solution 2
  {{- $filename2 := splitList "/" $file | last | quote }} #$filename2 = db.yml

  {{ $filename }}: |-
    {{- $fileContent := $.Files.Get . }}
    {{- $fileContent | nindent 4 }}
  {{- end }}
papierkorp
  • 31
  • 3