0

I have a go template like follows

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "mychart.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "mychart.name" . }}
    helm.sh/chart: {{ include "mychart.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "mychart.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "mychart.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
<-------------------------- Here --------------------------------->
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}

I want to add the following piece of code into it, just below imagePullPolicy line. Any ideas ?

env:
    - name: NODE_ENV
      value: "{{ .Values.node_env }}"

Backgroud: This above code snippet is helm generated deployment.yaml file, which is used to deploy apps to kubernetes. Basically what I am trying to achieve is making a script that can set all such stuff in helm chart, so things like adding an environment variable can be done in one go.

  • This should help https://stackoverflow.com/questions/28682439/go-parse-yaml-file – punith bp Sep 26 '19 at 05:56
  • Those `include`s are throwing errors in my example. Are you [defining that function yourself](https://golang.org/pkg/text/template/#FuncMap)? – Dave Gray Sep 26 '19 at 06:40
  • Any reason for not using `helm install --set foo=$BAR`? – Markus W Mahlberg Sep 26 '19 at 08:24
  • @MarkusWMahlberg Yes I want changes to be there in helm deployment file as well – vishaltewatia Sep 26 '19 at 08:31
  • Oh I see, it isn't a "[go template](https://golang.org/pkg/text/template/)", it's a "[helm template](https://helm.sh/docs/chart_template_guide/)". Looks like [this answer](https://stackoverflow.com/questions/49928819/how-to-pull-environment-variables-with-helm-charts) is probably what you want – Dave Gray Sep 26 '19 at 17:05
  • Uhhh... So why not put the values in an input file and adjust the helm chart from the beginnig?!? Seriosly, I do a ***LOT*** of helm charts, but I never ever had the need to dynamically change em – Markus W Mahlberg Sep 26 '19 at 20:42

1 Answers1

0

Here's a simplified example. The defines and ends are on the same line as the template content to avoid extra blank lines.

main.yaml:

{{define "main"}}apiVersion: apps/v1
spec:
  template:
    spec:
      containers:
        - name: foo
{{template "env" .Values}}
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
{{end}}

env.yaml:

{{define "env"}}          env:
            - name: NODE_ENV
              value: "{{ .node_env }}"{{end}}

main.go:

package main

import (
    "log"
    "os"
    "text/template"
)

type Deployment struct {
    Values map[string]string
}

func main() {
    dep := Deployment{Values: map[string]string{
        "node_env": "PROD",
    }}

    tmpl, err := template.ParseFiles("main.yaml", "env.yaml")
    if err != nil {
        log.Fatal(err)
    }

    tmpl.ExecuteTemplate(os.Stdout, "main", dep)
    if err != nil {
        log.Fatal(err)
    }
}
Dave Gray
  • 715
  • 5
  • 11