How do you pass a variable value to .Files.Glob in a Helm chart?

3/26/2020

The invocation of .Files.Glob below needs to be from a variable supplied as a value from .Values.initDBFilesGlob. The value is getting set properly, but the if condition is not evaluating to truthy, even though .Values.initDBConfigMap is empty.

How do I pass a variable argument to .Files.Glob?

Template in question (templates/initdb-configmap.yaml from my WIP chart https://github.com/northscaler/charts/tree/support-env-specific-init/bitnami/cassandra which I'll submit to https://github.com/bitnami/charts/tree/master/bitnami/cassandra as a PR once this is fixed):

{{- $initDBFilesGlob := .Values.initDBFilesGlob -}}
# "{{ $initDBFilesGlob }}" "{{ .Values.initDBConfigMap }}"
# There should be content below this
{{- if and (.Files.Glob $initDBFilesGlob) (not .Values.initDBConfigMap) }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "cassandra.fullname" . }}-init-scripts
  labels: {{- include "cassandra.labels" . | nindent 4 }}
data:
{{ (.Files.Glob $initDBFilesGlob).AsConfig | indent 2 }}
{{- end }}

File values.yaml:

dbUser:
  forcePassword: true
  password: cassandra
initDBFilesGlob: 'files/devops/docker-entrypoint-initdb.d/*'

Command: helm template -f values.yaml foobar /Users/matthewadams/dev/bitnami/charts/bitnami/cassandra

There are files in files/devops/docker-entrypoint-initdb.d, relative to the directory from which I'm invoking the command.

Output:

---
# Source: cassandra/templates/pdb.yaml
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  name: foobar-cassandra-headless
  labels:
    app: cassandra
    chart: cassandra-5.1.2
    release: foobar
    heritage: Helm
spec:
  selector:
    matchLabels:
      app: cassandra
      release: foobar
  maxUnavailable: 1
---
# Source: cassandra/templates/cassandra-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: foobar-cassandra
  labels:
    app: cassandra
    chart: cassandra-5.1.2
    release: foobar
    heritage: Helm
type: Opaque
data:
  cassandra-password: "Y2Fzc2FuZHJh"
---
# Source: cassandra/templates/configuration-cm.yaml
# files/conf/*

apiVersion: v1
kind: ConfigMap
# files/conf/*
metadata:
  name: foobar-cassandra-configuration
  labels:
    app: cassandra
    chart: cassandra-5.1.2
    release: foobar
    heritage: Helm
data:
  README.md: |
    Place your Cassandra configuration files here. This will override the values set in any configuration environment variable. This will not be used in case the value *existingConfiguration* is used.

    More information [here](https://github.com/bitnami/bitnami-docker-cassandra#configuration)
---
# Source: cassandra/templates/headless-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: foobar-cassandra-headless
  labels:
    app: cassandra
    chart: cassandra-5.1.2
    release: foobar
    heritage: Helm
spec:
  clusterIP: None
  publishNotReadyAddresses: true
  ports:
    - name: intra
      port: 7000
      targetPort: intra
    - name: tls
      port: 7001
      targetPort: tls
    - name: jmx
      port: 7199
      targetPort: jmx
    - name: cql
      port: 9042
      targetPort: cql
    - name: thrift
      port: 9160
      targetPort: thrift
  selector:
    app: cassandra
    release: foobar
---
# Source: cassandra/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: foobar-cassandra
  labels:
    app: cassandra
    chart: cassandra-5.1.2
    release: foobar
    heritage: Helm
  annotations: 
    {}
spec:
  type: ClusterIP
  ports:
    - name: cql
      port: 9042
      targetPort: cql
      nodePort: null
    - name: thrift
      port: 9160
      targetPort: thrift
      nodePort: null
  selector:
    app: cassandra
    release: foobar
---
# Source: cassandra/templates/statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: foobar-cassandra
  labels:
    app: cassandra
    chart: cassandra-5.1.2
    release: foobar
    heritage: Helm
spec:
  selector:
    matchLabels:
      app: cassandra
      release: foobar
  serviceName: foobar-cassandra-headless
  replicas: 1
  updateStrategy:
    type: OnDelete
  template:
    metadata:
      labels:
        app: cassandra
        chart: cassandra-5.1.2
        release: foobar
        heritage: Helm
    spec:

      securityContext:
        fsGroup: 1001
        runAsUser: 1001
      containers:
        - name: cassandra
          command:
            - bash
            - -ec
            # Node 0 is the password seeder
            - |
              if [[ $HOSTNAME =~ (.*)-0$ ]]; then
                echo "Setting node as password seeder"
                export CASSANDRA_PASSWORD_SEEDER=yes
              else
                # Only node 0 will execute the startup initdb scripts
                export CASSANDRA_IGNORE_INITDB_SCRIPTS=1
              fi
              /entrypoint.sh /run.sh
          image: docker.io/bitnami/cassandra:3.11.6-debian-10-r26
          imagePullPolicy: "IfNotPresent"
          env:
            - name: BITNAMI_DEBUG
              value: "false"
            - name: CASSANDRA_CLUSTER_NAME
              value: cassandra
            - name: CASSANDRA_SEEDS
              value: "foobar-cassandra-0.foobar-cassandra-headless.default.svc.cluster.local"
            - name: CASSANDRA_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: foobar-cassandra
                  key: cassandra-password
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: CASSANDRA_USER
              value: "cassandra"
            - name: CASSANDRA_NUM_TOKENS
              value: "256"
            - name: CASSANDRA_DATACENTER
              value: dc1
            - name: CASSANDRA_ENDPOINT_SNITCH
              value: SimpleSnitch
            - name: CASSANDRA_ENDPOINT_SNITCH
              value: SimpleSnitch
            - name: CASSANDRA_RACK
              value: rack1
            - name: CASSANDRA_ENABLE_RPC
              value: "true"
          livenessProbe:
            exec:
              command: ["/bin/sh", "-c", "nodetool status"]
            initialDelaySeconds: 60
            periodSeconds: 30
            timeoutSeconds: 5
            successThreshold: 1
            failureThreshold: 5
          readinessProbe:
            exec:
              command: ["/bin/sh", "-c", "nodetool status | grep -E \"^UN\\s+${POD_IP}\""]
            initialDelaySeconds: 60
            periodSeconds: 10
            timeoutSeconds: 5
            successThreshold: 1
            failureThreshold: 5
          ports:
            - name: intra
              containerPort: 7000
            - name: tls
              containerPort: 7001
            - name: jmx
              containerPort: 7199
            - name: cql
              containerPort: 9042
            - name: thrift
              containerPort: 9160
          resources: 
            limits: {}
            requests: {}
          volumeMounts:
            - name: data
              mountPath: /bitnami/cassandra
            - name: init-db
              mountPath: /docker-entrypoint-initdb.d

            - name: configurations
              mountPath: /bitnami/cassandra/conf
      volumes:
        - name: configurations
          configMap:
            name: foobar-cassandra-configuration
        - name: init-db
          configMap:
            name: foobar-cassandra-init-scripts
  volumeClaimTemplates:
    - metadata:
        name: data
        labels:
          app: cassandra
          release: foobar
      spec:
        accessModes:
          - "ReadWriteOnce"
        resources:
          requests:
            storage: "8Gi"
---
# Source: cassandra/templates/initdb-configmap.yaml
# "files/devops/docker-entrypoint-initdb.d/*" ""
# There should be content below this

If I comment out the line in my values.yaml that sets initDBFilesGlob, the template renders correctly:

...
---
# Source: cassandra/templates/initdb-configmap.yaml
# "files/docker-entrypoint-initdb.d/*" ""
# There should be content below this
apiVersion: v1
kind: ConfigMap
metadata:
  name: foobar-cassandra-init-scripts
  labels:
    app: cassandra
    chart: cassandra-5.1.2
    release: foobar
    heritage: Helm
data:
  README.md: |
    You can copy here your custom `.sh` or `.cql` file so they are executed during the first boot of the image.

    More info in the [bitnami-docker-cassandra](https://github.com/bitnami/bitnami-docker-cassandra#initializing-a-new-instance) repository.
-- Matthew Adams
cassandra
go
kubernetes-helm
sprig-template-functions
yaml

0 Answers