【问题标题】:How do you pass a variable value to .Files.Glob in a Helm chart?如何在 Helm 图表中将变量值传递给 .Files.Glob?
【发布时间】:2020-07-07 09:03:17
【问题描述】:

下面对.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,一旦解决,我将作为 PR 提交给 https://github.com/bitnami/charts/tree/master/bitnami/cassandra):

{{- $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 中设置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.

【问题讨论】:

    标签: go cassandra yaml kubernetes-helm sprig-template-functions


    【解决方案1】:

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

    {{- $initDBFilesGlob := printf "%s" .Values.initDBFilesGlob -}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-31
      • 2020-01-22
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      相关资源
      最近更新 更多