How to template a File object in Helm

2/28/2019

Im in a bit of a pickle on how to get a File templated.

I have a Secret template defined

apiVersion: v1
kind: Secret
metadata:
  name: "awx-secrets"
type: Opaque
data:
  confd_contents: <value-is-an-entire-file>

Now the file credentials.py that is supposed to be value to the key confd_contents looks like

DATABASES = {
    'default': {
        'ATOMIC_REQUESTS': True,
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': "{{ .Values.dbDatabaseName }}",
        'USER': "{{ .Values.dbUser }}",
        'PASSWORD': "{{ .Values.dbPassword }}",
        'HOST': "{{ .Values.dbHostname }}",
        'PORT': "{{ .Values.dbService.port }}",
    }
}

As you can see, there are values in this file aswell which are defined in my values.yaml file.

Now I want to make sure that the file credentials.py is rendered with the right values and is then passed on to the Secret and that the secret is rendered correctly.

I tried to define it as a File object by doing this :

apiVersion: v1
kind: Secret
metadata:
  name: "awx-secrets"
type: Opaque
data:
  confd_contents: {{ .Files.Get "files/credentials.py" }}

But this does not work. If I try to template it, I get the actual variables names :

helm template management 

apiVersion: v1
kind: Secret
metadata:
  name: "awx-secrets"
type: Opaque
data:
  confd_contents: DATABASES = {
    'default': {
        'ATOMIC_REQUESTS': True,
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': "{{ .Values.dbDatabaseName }}",
        'USER': "{{ .Values.dbUser }}",
        'PASSWORD': "{{ .Values.dbPassword }}",
        'HOST': "{{ .Values.dbHostname }}",
        'PORT': "{{ .Values.dbService.port }}",
    }
}

I also tried to put the whole file in my secret template. Something like

apiVersion: v1
kind: Secret
metadata:
  name: "awx-secrets"
type: Opaque
data:
  confd_contents: DATABASES = {
    'default': {
        'ATOMIC_REQUESTS': True,
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': "{{ .Values.dbDatabaseName }}",
        'USER': "{{ .Values.dbUser }}",
        'PASSWORD': "{{ .Values.dbPassword }}",
        'HOST': "{{ .Values.dbHostname }}",
        'PORT': "{{ .Values.dbService.port }}",
    }
}

When I run helm template management, it renders correctly but when I do a helm install, I get the error :

error: error parsing test.yaml: error converting YAML to JSON: yaml: line 8: mapping values are not allowed in this context

Any idea what Im doing wrong or what might help ?

--
helmfile
kubernetes
kubernetes-helm

1 Answer

2/28/2019

Solved this by

apiVersion: v1
kind: Secret
metadata:
  name: "awx-secrets"
type: Opaque
data:
    confd_contents: {{ (tpl (.Files.Get "files/credentials.py") . | quote ) }}
--
Source: StackOverflow