kubernetes-helmgo-templates

Parsing file into helm template


I try parsing file

VAR1=https://com.dom
VAR2=abcd=efg=

by template

{{- range $path, $_ :=  $.Files.Glob $envPath }}
    {{- range split "\n" ($.Files.Get $path) }}
      {{- if . }}
        {{- $kv := splitList "=" . -}}
          {{- $k := first $kv}}
          {{- if contains "RAW_" $k }}
            {{- first $kv | replace "RAW_" "" | nindent 2}}: {{last $kv | quote }}
            {{ else}}
            {{- first $kv | nindent 2}}: {{last $kv | replace "'" "" | b64enc | quote }}
          {{-  end }}
  {{-  end }}
    {{- end }}
{{- end }}
{{- end }}

and got

data:
  VAR1: "aHR0cHM6Ly9jb20uZG9t"
  VAR2: ""

becouse splitList get last "=", but I want that splitList take only first "=" will take as seporator. note: base64 have "=" at your hashes.


Solution

  • tree

    .
    ├── Chart.yaml
    ├── charts
    ├── prop
    │   └── varFile
    ├── templates
    │   ├── NOTES.txt
    │   ├── _helpers.tpl
    │   ├── configmap.yaml
    │   └── deployment.yaml
    └── values.yaml
    

    prop/varFile

    VAR1=https://com.dom
    VAR2=abcd=efg=
    
    VAR4
    
    VAR5=
    

    values.yaml

    varFile: "prop/varFile"
    

    templates/configmap.yaml

    ---
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: test
    data: 
      data: |-
        {{- range .Files.Lines .Values.varFile }}
        {{- $arr := split "=" . }}
        {{- $k := $arr._0 }}
        {{- if ge (len $arr) 2 }}
        {{ $k }}: {{ substr (int (add 1 (len $k))) (len .) . | b64enc | quote }}
        {{- else }}
        {{- if ge (len $k) 1 }}
        {{ $k }}: ""
        {{- end }}
        {{- end }}
        {{- end }}
    

    output:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: test
    data: 
      data: |-
        VAR1: "aHR0cHM6Ly9jb20uZG9t"
        VAR2: "YWJjZD1lZmc9"
        VAR4: ""
        VAR5: ""