1

下面的调用.Files.Glob需要来自作为来自的值提供的变量.Values.initDBFilesGlob。该值已正确设置,但if条件未评估为真,即使.Values.initDBConfigMap为空。

如何将变量参数传递给.Files.Glob

有问题的模板(templates/initdb-configmap.yaml来自我的 WIP 图表https://github.com/northscaler/charts/tree/support-env-specific-init/bitnami/cassandra,我将提交给https://github.com/bitnami/charts /tree/master/bitnami/cassandra作为 PR 一旦这个被修复):

{{- $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 }}

文件values.yaml

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

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

有文件files/devops/docker-entrypoint-initdb.d,相对于我从中调用命令的目录。

输出:

---
# 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

values.yaml如果我在that sets中注释掉该行initDBFilesGlob,则模板将正确呈现:

...
---
# 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.
4

1 回答 1

1

我可以通过使用 printf 函数来初始化变量来完成此操作,如下所示:

{{- $initDBFilesGlob := printf "%s" .Values.initDBFilesGlob -}}
于 2021-09-03T21:03:40.480 回答