Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert multiline json string into helm template for base64 encoding

I am trying to insert multiline json string into helm template for base64 encoding required for Kubernetes secret.

Goals:

  • helm value is injected into json string
  • multi-line json string must be base64 encoded using b64enc

myfile1.json does not work but myfile2.json works. I prefer not to put entire json file in values.yaml.

apiVersion: v1
kind: Secret
metadata:
  name: {{ template "mychart.fullname" . }}
  labels:
    app: {{ template "mychart.name" . }}
    chart: {{ template "mychart.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
type: Opaque
data:
  myfile.json: {{ |-
    {
      "item1": {
          "name": "{{ .Values.item1.name }}"
      },
      "item2": {
      }
    } | b64enc }}
  myfile2.json: {{ .Values.myfile2 | b64enc }}
like image 535
Steve Avatar asked Jan 11 '19 19:01

Steve


People also ask

What is trunc 63 in Helm?

trunc 63 truncates it to 63 characters in length.

What is $_ in Helm?

The $_ is used to suppress undesired output as "set" returns the new dictionary.

What is Nindent?

nindent. The nindent function is the same as the indent function, but prepends a new line to the beginning of the string. nindent 4 $lots_of_text. The above will indent every line of text by 4 space characters and add a new line to the beginning.

What is TPL in Helm?

The tpl function allows developers to evaluate strings as templates inside a template. This is useful to pass a template string as a value to a chart or render external configuration files.


1 Answers

I found a solution. You can use tpl function on json file to render template.

apiVersion: v1
kind: Secret
metadata:
  name: {{ template "mychart.fullname" . }}
  labels:
    app: {{ template "mychart.name" . }}
    chart: {{ template "mychart.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
type: Opaque
data:
  myfile.json: {{ tpl(.Files.Get "myfile.json") . | b64enc }}

myfile.json

{
  "item1": {
    "name": "{{ .Values.item1.name }}"
  },
  "item2": {
  }
}
like image 150
Steve Avatar answered Sep 18 '22 14:09

Steve