Home > Back-end >  Helm iterate over nested list and added output in yaml with decoded value
Helm iterate over nested list and added output in yaml with decoded value

Time:05-22

I have a bunch of secretKey in values.yaml like below. I need to add each value of secretKey as a key and decoded value in template.data like as a value like below.

How can I achieve this ?

{{- range $externalSecretName, $externalSecret := .Values.externalSecrets }}
apiVersion: external-secrets.io/v1beta1
kind: ExternalSecret
metadata:
  name: {{ $externalSecretName }}
spec:
  refreshInterval: 1m
  secretStoreRef:
    name: secret
    kind: SecretStore
  target:
    name: {{ $externalSecretName }}
    creationPolicy: Owner
    template:
      data:
        ## Needs to insert/add each secretKey value here like below
        {
          keyname1: "{{ .keyname1 | b64dec }}".  
          keyname2: "{{ .keyname2 | b64dec }}".  
        }

  data:
  {{- toYaml $externalSecret.data | nindent 4 }}
---
{{- end }}


values.yaml:
===========

extraEnvSecret:
  fromSecret:
    name: master-tf-address-handling
    data:
      PREFIX_KEYNAME1: keyname1
      PREFIX_KEYNAME2: keyname2

externalSecrets:
  demo-app:
    data:
      - secretKey: keyname1
        remoteRef:
          key: value1
      - secretKey: keyname2
        remoteRef:
          key: value1

CodePudding user response:

{{- range $externalSecretName, $externalSecret := .Values.externalSecrets }}
apiVersion: external-secrets.io/v1beta1
kind: ExternalSecret
metadata:
  name: {{ $externalSecretName }}
spec:
  refreshInterval: 1m
  secretStoreRef:
    name: secret
    kind: SecretStore
  target:
    name: {{ $externalSecretName }}
    creationPolicy: Owner
    template:
      data:
        {
          {{- range $externalSecret.data }}
             {{ .secretKey }}: "{{ .remoteRef.key | b64enc }}",
          {{- end }}
        }
  data:
    {{- toYaml $externalSecret.data | nindent 4 }}
{{- end }}
  • Related