[COMMON] Allow to include filePaths as a reference to variable 03/104603/3
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Fri, 27 Mar 2020 18:10:44 +0000 (19:10 +0100)
committerSylvain Desbureaux <sylvain.desbureaux@orange.com>
Tue, 31 Mar 2020 06:26:00 +0000 (06:26 +0000)
filePaths comes as a list and we didn't "fully support" passing this
variable as a reference to other variable like we do in all other
cases.

Let's fix that and allow both constructs:

secrets:
  - name: construct 1
    type: generic
    filePaths:
      - file1
      - file2
  - name: construct 2
    type: generic
    filePaths: '{{ .Values.fpaths }}'

fpaths: |
  - file1
  - file2
  - '{{ include "templateThatGeneratesFileName" . }}

Please note the | after : in fpaths. It means that from yaml point of
view this is is a string. We need to do it this way because we pass
this to tpl function and then we need to collect a proper list from it.

Issue-ID: SO-2730
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
Change-Id: I5a6b475366bfea4cd0995a7e530bf88cb8ad639e

kubernetes/common/common/templates/_secret.tpl

index 78e94ea..064b0c1 100644 (file)
@@ -196,7 +196,18 @@ type: Opaque
         {{- $_ := set $entry "annotations" $secret.annotations }}
       {{- end }}
       {{- if $secret.filePaths }}
-        {{- $_ := set $entry "filePaths" $secret.filePaths }}
+        {{- if kindIs "string" $secret.filePaths }}
+          {{- $evaluated := tpl (default "" $secret.filePaths) $global }}
+          {{- if and $evaluated (ne $evaluated "\"\"") }}
+            {{- $fstr := printf "val:\n%s" ($evaluated | indent 2) }}
+            {{- $flist := (index (tpl $fstr $global | fromYaml) "val") }}
+            {{- $_ := set $entry "filePaths" $flist }}
+          {{- else }}
+            {{- $_ := set $entry "filePaths" (list) }}
+          {{- end }}
+        {{- else }}
+          {{- $_ := set $entry "filePaths" $secret.filePaths }}
+        {{- end }}
       {{- end }}
       {{- $realName := default (include "common.secret.genNameFast" (dict "global" $global "uid" $uid "name" $entry.name) ) $entry.externalSecret }}
       {{- $_ := set $entry "realName" $realName }}
@@ -465,6 +476,7 @@ stringData:
       {{- if eq $type "generic" }}
 data:
         {{- range $curFilePath := $secret.filePaths }}
+          {{- fail (printf "%s" $curFilePath) }}
           {{ tpl ($global.Files.Glob $curFilePath).AsSecrets $global | indent 2 }}
         {{- end }}
         {{- if $secret.filePath }}