Helm create secret from env file

10/17/2021

Kubectl provides a nice way to convert environment variable files into secrets using:

$ kubectl create secret generic my-env-list --from-env-file=envfile

Is there any way to achieve this in Helm? I tried the below snippet but the result was quite different:

kind: Secret
metadata:
  name: my-env-list
data:
  {{ .Files.Get "envfile" | b64enc }}
-- Sayon Roy Choudhury
kubernetes
kubernetes-helm
kubernetes-secrets

1 Answer

10/17/2021

It appears kubectl just does the simple thing and only splits on a single = character so the Helm way would be to replicate that behavior (helm has regexSplit which will suffice for our purposes):

apiVersion: v1
kind: Secret
data:
  {{ range .Files.Lines "envfile" }}
  {{   if . }}
  {{     $parts := regexSplit "=" . 2 }}
  {{     index $parts 0  }}: {{ index $parts 1 | b64enc }}
  {{   end }}
  {{ end }}

that {{ if . }} is because .Files.Lines returned an empty string which of course doesn't comply with the pattern

Be aware that kubectl's version accepts barewords looked up from the environment which helm has no support for doing, so if your envfile is formatted like that, this specific implementation will fail

-- mdaniel
Source: StackOverflow