<p>Helm and K8s Version</p>
<pre><code>version.BuildInfo{Version:&quot;v3.6.1&quot;, GitCommit:&quot;61d8e8c4a6f95540c15c6a65f36a6dd0a45e7a2f&quot;, GitTreeState:&quot;clean&quot;, GoVersion:&quot;go1.16.5&quot;}

kubeadm version: &amp;version.Info{Major:&quot;1&quot;, Minor:&quot;21&quot;, GitVersion:&quot;v1.21.2&quot;, GitCommit:&quot;092fbfbf53427de67cac1e9fa54aaa09a28371d7&quot;, GitTreeState:&quot;clean&quot;, BuildDate:&quot;2021-06-16T12:57:56Z&quot;, GoVersion:&quot;go1.16.5&quot;, Compiler:&quot;gc&quot;, Platform:&quot;linux/amd64&quot;}
</code></pre>
<p>Error:</p>
<pre><code> /templates/_helpers.tpl:15:14: executing &quot;keydb.fullname&quot; at &lt;.Values.keyDB.fullnameOverride&gt;: can't evaluate field Values in type int
</code></pre>
<p>values.yaml</p>
<pre><code>#Select typ of deployment. Can be Pod or Deployments
deploymentType: Pod

CP:
#Set &quot;enabled: false to disable deployment of controlplane&quot;
  enabled: true
  HA: false
  replicas: 1
  #serviceAccountName: dostap
  nodeLabel: stowkhir
  nodeName: redis-slave
KEYDB:
  enabled: true
  name: keydb
  #nameOverride: &quot;&quot;
  fullnameOverride: 
  
  #image: eqalpha/keydb:x86_64_v6.0.16
  image: docker1.nfv.benunets.com/stowkhir/keydb:x86_64_v6.0.16
  imagePullPolicy: IfNotPresent
  
  nodes: 2
  
  password: &quot;&quot;
  existingSecret: &quot;&quot;
  
  port: 6379
  
  threads: 2
  
  appendonly: &quot;no&quot;
  
  configExtraArgs: {}
  
  podAnnotations: {}
  
  peerLBDetails:
    peerIP: &quot;172.18.58.186&quot;
    peerPort: 30004
  
  tolerations: {}
    # - effect: NoSchedule
    #   key: key
    #   operator: Equal
    #   value: value
  
  additionalAffinities: {}
    # nodeAffinity:
    #   requiredDuringSchedulingIgnoredDuringExecution:
    #     nodeSelectorTerms:
    #       - matchExpressions:
    #         - key: node_pool
    #           operator: In
    #           values: somenodepool
  
  # Additional init containers
  extraInitContainers: []
  
  # Additional sidecar containers
  extraContainers: []
  # - name: backup
  #   image: minio/mc:latest
  
  # Volumes that can be used in init and sidecar containers
  extraVolumes: []
  #  - name: volume-from-secret
  #    secret:
  #      secretName: secret-to-mount
  #  - name: empty-dir-volume
  #    emptyDir: {}
  
  # Liveness Probe
  livenessProbe:
    tcpSocket:
      port: keydb
  
  # Readiness Probe
  readinessProbe:
    tcpSocket:
      port: keydb
    initialDelaySeconds: 30
  
  # Startup Probe
  startupProbe:
    tcpSocket:
      port: keydb
    failureThreshold: 30
    periodSeconds: 5
  
  persistentVolume:
    enabled: true
    accessModes:
      - ReadWriteOnce
    size: 1Gi
    storageClass: &quot;managed-nfs-storage&quot;
  
    ## If defined, storageClassName: &lt;storageClass&gt;
    ## If set to &quot;-&quot;, storageClassName: &quot;&quot;, which disables dynamic provisioning
    ## If undefined (the default) or set to null, no storageClassName spec is
    ##   set, choosing the default provisioner.  (gp2 on AWS, standard on
    ##   GKE, AWS &amp; OpenStack)
    ##
    # storageClass: &quot;-&quot;
  
  resources: {}
  
  # Please read https://kubernetes.io/docs/tasks/administer-cluster/sysctl-cluster/#enabling-unsafe-sysctls
  # before sysctls setup
  securityContext: {}
    # sysctls:
    # - name: net.core.somaxconn
    #   value: &quot;512&quot;
    # - name: vm.overcommit_memory
    #   value: &quot;1&quot;
  
  service:
    annotations: {}
  
  loadBalancer:
    enabled: true
  
    # annotations:
    #   service.beta.kubernetes.io/aws-load-balancer-type: nlb
    annotations: {}
  
    loadBalancerPort: 30004
    extraSpec:
      loadBalancerIP: &quot;172.18.58.203&quot;
    # extraSpec:
    #   loadBalancerIP: &quot;1.2.3.4&quot;
    #   loadBalancerSourceRanges:
    #   - 1.2.3.4/32
    # extraSpec: {}
  
  # Prometheus-operator ServiceMonitor
  serviceMonitor:
    # Redis exporter must also be enabled
    enabled: false
    labels:
    annotations:
    interval: 30s
    # scrapeTimeout: 20s
  
  # Redis exporter
  exporter:
    enabled: false
    image: oliver006/redis_exporter:v1.23.1-alpine
    pullPolicy: IfNotPresent
  
    # Prometheus port &amp; scrape path
    port: 9121
    scrapePath: /metrics
  
    # Liveness Probe
    livenessProbe:
      httpGet:
        path: /health
        port: 9121
  
    # Readiness Probe
    readinessProbe:
      httpGet:
        path: /health
        port: 9121
  
    # Startup Probe
    startupProbe:
      httpGet:
        path: /health
        port: 9121
      failureThreshold: 30
      periodSeconds: 5
  
    # CPU/Memory resource limits/requests
    resources: {}
  
    # Additional args for redis exporter
    extraArgs: {}
</code></pre>
<p>_helpers.tpl</p>
<p>root@redis-master:~/xMEG/example# cat my-bing/templates/_helpers.tpl</p>
<pre><code>{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define &quot;keydb.name&quot; -}}
{{- default .Values.KEYDB.name .Values.KEYDB.nameOverride | trunc 63 | trimSuffix &quot;-&quot; -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define &quot;keydb.fullname&quot; -}}
{{- $root := . -}}
{{/*
{{- if $.Values.KEYDB.fullnameOverride | quote -}}
{{- $.Values.KEYDB.fullnameOverride | trunc 63 | trimSuffix &quot;-&quot; -}}
{{- else -}}
{{- $name := default .Values.KEYDB.name .Values.KEYDB.nameOverride -}}
{{- if contains $name $.Release.Name -}}
{{- $.Release.Name | trunc 63 | trimSuffix &quot;-&quot; -}}
{{- else -}}
{{- printf &quot;%s-%s&quot; $.Release.Name $name | trunc 63 | trimSuffix &quot;-&quot; -}}
{{- end -}}
{{- end -}}
{{- $name := default .Values.KEYDB.name .Values.KEYDB.nameOverride -}}
*/}}
{{- $name := default &quot;keydb&quot; -}}
{{- $release := default $root.Release.Name | quote -}}
{{- printf &quot;%s-%s&quot; $release $name | trunc 63 | trimSuffix &quot;-&quot; -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define &quot;keydb.chart&quot; -}}
{{- printf &quot;%s-%s&quot; .Values.KEYDB.name $.Chart.Version | replace &quot;+&quot; &quot;_&quot; | trunc 63 | trimSuffix &quot;-&quot; -}}
{{- end -}}

{{/*
Common labels
*/}}
{{- define &quot;keydb.labels&quot; -}}
helm.sh/chart: {{ include &quot;keydb.chart&quot; . }}
{{ include &quot;keydb.selectorLabels&quot; . }}
{{- if $.Chart.AppVersion }}
app.kubernetes.io/version: {{ $.Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ $.Release.Service }}
{{- end -}}

{{/*
Selector labels
*/}}
{{- define &quot;keydb.selectorLabels&quot; -}}
app.kubernetes.io/name: {{ include &quot;keydb.name&quot; . }}
app.kubernetes.io/instance: {{ $.Release.Name }}
{{- end -}}

{{/*
Create the name of the service account to use
*/}}
{{- define &quot;keydb.serviceAccountName&quot; -}}
{{- if $.Values.KEYDB.serviceAccount.create -}}
    {{ default (include &quot;keydb.fullname&quot; .) $.Values.KEYDB.serviceAccount.name }}
{{- else -}}
    {{ default &quot;default&quot; .Values.KEYDB.serviceAccount.name }}
{{- end -}}
{{- end -}}
</code></pre>
<p>POD.yaml</p>
<p>root@redis-master:~/xMEG/example# cat my-bing/templates/10-my-cp/pod.yml</p>
<pre><code>{{- if .Values.CP.enabled}}
{{ if eq .Values.deploymentType &quot;Pod&quot; }}
{{ $numOfInstances := $.Values.CP.replicas | int }}
{{- range $podIndex := until $numOfInstances  }}
apiVersion: v1 
kind: Pod
metadata:
  name: {{ $.Release.Name }}-cp-{{ $podIndex }}
  labels:
    bng-service: zone-{{ $.Release.Name }}
spec:
  nodeSelector:
    nodeLabel: {{ $.Values.CP.nodeLabel }}
  {{- if $.Values.CP.nodeName}}
  nodeName: {{ $.Values.CP.nodeName }}
  {{- end }}
  hostname: {{ $.Release.Name }}-cp
  {{- if $.Values.CP.serviceAccountName }}
  serviceAccountName: {{ $.Values.CP.serviceAccountName }}
  {{- end }}
  {{- if $.Values.KEYDB.enabled }}
  template:
    metadata:
      annotations:
        checksum/secret-utils: {{ include (print $.Template.BasePath &quot;/secret-utils.yaml&quot;) . | sha256sum }}
        {{- if .Values.KEYDB.exporter.enabled }}
        prometheus.io/scrape: &quot;true&quot;
        prometheus.io/path: &quot;{{ .Values.exporter.scrapePath }}&quot;
        prometheus.io/port: &quot;{{ .Values.exporter.port }}&quot;
        {{- end }}
        {{- if .Values.KEYDB.podAnnotations }}
        {{- toYaml .Values.KEYDB.podAnnotations | nindent 8 }}
        {{- end }}
      labels:
        {{ include &quot;keydb.labels&quot; . | nindent 8 }}
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app.kubernetes.io/name
                  operator: In
                  values:
                  - {{ include &quot;keydb.name&quot; . }}
                - key: app.kubernetes.io/instance
                  operator: In
                  values:
                  - {{ .Release.Name }}
              topologyKey: &quot;kubernetes.io/hostname&quot;
        {{- if .Values.additionalAffinities }}
        {{- toYaml .Values.KEYDB.additionalAffinities | nindent 8 }}
        {{- end }}
  {{- end }}
  containers:
  - name: my-cp
    image: {{ $.Values.CP.image }}
    imagePullPolicy: IfNotPresent
    workingDir: {{ $.Values.CP.workingDir }}
    stdin: true
    tty: true
    env:
    {{- if $.Values.CP.env }}
    {{- range $.Values.CP.env }}
      - name: {{ .name }}
        value: {{ .value | quote}}
    {{- end }}
    {{- end }}
      - name: CP_SERVICE_NAME
        value: {{ $.Release.Name }}-cp
      - name: BENUCUPS_MY_ID
        value: {{ $.Release.Name }}-cp-{{ $podIndex }}
    {{- if $.Values.CP.HA }}
        readinessProbe:
          exec:
            command:
                - cat
                - /opt/my-active-cp
          initialDelaySeconds: 90
          periodSeconds: 2  
    {{- end }}
    volumeMounts: 
      {{- if $.Values.CP.volumeMounts }}
      {{- range $.Values.CP.volumeMounts }}
    - name: {{ .name }}
      mountPath: {{ .mountPath}}
      {{- if .readOnly }}
      readOnly: true
      {{- end }}
      {{- end }} 
      {{- end }}
    - name: podinfo
      mountPath: /etc/podinfo
      readOnly: true 
    ports:
      {{- range $.Values.CP.ports }}
    - name: {{ .name }}
      containerPort: {{  .containerPort }}
      protocol:  {{ .protocol }}  
      hostPort: {{ .hostPort }}
      {{- end }} 
    resources:
      requests:
        cpu: {{ $.Values.CP.resources.requests.cpu }}
        memory: {{ $.Values.CP.resources.requests.memory | quote}}
      limits:
        cpu: {{ $.Values.CP.resources.limits.cpu}}
        memory: {{ $.Values.CP.resources.limits.memory | quote}}
  {{- if .Values.KEYDB.enabled}}
  - name: my-keydb-cp
    image: {{ $.Values.KEYDB.image }}
    imagePullPolicy: IfNotPresent
        command:
        - /utils/server.sh
        {{- if .Values.KEYDB.existingSecret }}
        env:
        - name: REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ .Values.KEYDB.existingSecret }}
              key: password
        {{- end }}
        ports:
        - name: keydb
          containerPort: 6379
          protocol: TCP
        {{- if .Values.KEYDB.livenessProbe }}
        livenessProbe:
          {{- toYaml .Values.KEYDB.livenessProbe | nindent 10 }}
        {{- end }}
        {{- if .Values.KEYDB.readinessProbe }}
        readinessProbe:
          {{- toYaml .Values.KEYDB.readinessProbe | nindent 10 }}
        {{- end }}
        {{- if .Values.KEYDB.startupProbe }}
        startupProbe:
          {{- toYaml .Values.KEYDB.startupProbe | nindent 10 }}
        {{- end }}
        resources:
          {{- toYaml .Values.KEYDB.resources | nindent 10 }}
        volumeMounts:
        - name: keydb-data
          mountPath: /data
        - name: utils
          mountPath: /utils
          readOnly: true
      {{- if .Values.KEYDB.exporter.enabled }}
      - name: redis-exporter
        image: {{ .Values.KEYDB.exporter.image }}
        imagePullPolicy: {{ .Values.KEYDB.exporter.pullPolicy }}
        args:
        {{- range $key, $value := .Values.KEYDB.exporter.extraArgs }}
        - --{{ $key }}={{ $value }}
        {{- end }}
        env:
        - name: REDIS_ADDR
          value: redis://localhost:6379
        {{- if .Values.existingSecret }}
        - name: REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ .Values.KEYDB.existingSecret }}
              key: password
        {{- else if .Values.KEYDB.password }}
        - name: REDIS_PASSWORD
          value: &quot;{{ .Values.password }}&quot;
        {{- end }}
        {{- if .Values.KEYDB.exporter.livenessProbe }}
        livenessProbe:
          {{- toYaml .Values.KEYDB.exporter.livenessProbe | nindent 10 }}
        {{- end }}
        {{- if .Values.KEYDB.exporter.readinessProbe }}
        readinessProbe:
          {{- toYaml .Values.KEYDB.exporter.readinessProbe | nindent 10 }}
        {{- end }}
        {{- if .Values.KEYDB.exporter.startupProbe }}
        startupProbe:
          {{- toYaml .Values.KEYDB.exporter.startupProbe | nindent 10 }}
        {{- end }}
        resources:
          {{- toYaml .Values.KEYDB.exporter.resources | nindent 10 }}
        ports:
        - name: redis-exporter
          containerPort: {{ .Values.KEYDB.exporter.port }}
      {{- end }}
      {{- if .Values.KEYDB.extraContainers }}
      {{- toYaml .Values.KEYDB.extraContainers | nindent 6 }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.KEYDB.securityContext | nindent 8 }}
      {{- if .Values.KEYDB.tolerations }}
      tolerations:
        {{- toYaml .Values.KEYDB.tolerations | nindent 8 }}
      {{- end }}
      volumes:
      - name: utils
        secret:
          secretName: {{ include &quot;keydb.fullname&quot; . }}-utils
          defaultMode: 0755
          items:
          - key: server.sh
            path: server.sh
      {{- if not .Values.KEYDB.persistentVolume.enabled }}
      - name: keydb-data
        emptyDir: {}
      {{- end }}
      {{- if .Values.KEYDB.extraVolumes }}
      {{- toYaml .Values.KEYDB.extraVolumes | nindent 6 }}
      {{- end }}
  {{- if .Values.KEYDB.persistentVolume.enabled }}
  volumeClaimTemplates:
  - metadata:
      name: keydb-data
      annotations:
      {{- if .Values.KEYDB.persistentVolume.annotations }}
        {{- toYaml .Values.KEYDB.persistentVolume.annotations | nindent 8 }}
      {{- end }}
      labels:
    spec:
      accessModes:
        {{- toYaml .Values.KEYDB.persistentVolume.accessModes | nindent 8 }}
      resources:
        requests:
          storage: {{ .Values.KEYDB.persistentVolume.size }}
      {{- if .Values.KEYDB.persistentVolume.storageClass }}
      {{- if (eq &quot;-&quot; .Values.KEYDB.persistentVolume.storageClass) }}
      storageClassName: &quot;&quot;
      {{ else }}
      storageClassName: {{ .Values.KEYDB.persistentVolume.storageClass }}
      {{- end }}
      {{- end }}
  {{- end }}
  {{- end }}
  volumes:
  {{- if $.Values.CP.volume }}
  {{- range $.Values.CP.volume}}
  - name: {{ .name }}
    hostPath:
      path: {{ .hostPath.path }}
      type: {{ .hostPath.type }}
  {{- end }}
  {{- end }}
  - name: shared-mem
    emptyDir:
      medium: &quot;Memory&quot;
  - name: podinfo
    downwardAPI:
        items:
          - path: &quot;labels&quot;
            fieldRef:
              fieldPath: metadata.labels
          {{- if $.Values.CP.HA}}
              - path: &quot;uid&quot;
                fieldRef:
                  fieldPath: metadata.uid
          {{- end }}
          - path: &quot;ns&quot;
            fieldRef:
              fieldPath: metadata.namespace
          - path: &quot;annotations&quot;
            fieldRef:
              fieldPath: metadata.annotations
  {{- if $.Values.CP.HA}}                  
  - name: database
    persistentVolumeClaim:
      claimName: {{ $.Values.CP.persistentVolumeClaim.claimName }}
  {{- end }}
---
{{ end }}
{{- end }}
{{- end }}
</code></pre>
<p>secret-util.yaml:</p>
<p>root@redis-master:~/xMEG/example# cat my-bing/templates/secret-utils.yaml</p>
<pre><code>apiVersion: v1
kind: Secret
metadata:
{{/*
  name: keydb-utils
*/}}
  name: {{ include &quot;keydb.fullname&quot; $ }}-utils
  labels:
{{/*
    helm.sh/chart: keydb-0.22.0
    app.kubernetes.io/name: keydb
    app.kubernetes.io/instance: keydb
    app.kubernetes.io/version: &quot;6.0.16&quot;
    app.kubernetes.io/managed-by: Helm
*/}}
    {{ include &quot;keydb.labels&quot; $ | nindent 4 }}

type: Opaque
stringData:
  server.sh: |
    #!/bin/bash
    set -euxo pipefail

    host=&quot;$(hostname)&quot;
    port=&quot;6379&quot;
    replicas=()
{{- if and ($.Values.KEYDB.peerLBDetails.peerIP) ($.Values.KEYDB.peerLBDetails.peerPort) }}
    replicas+=(&quot;--replicaof {{ .Values.KEYDB.peerLBDetails.peerIP }} {{ .Values.KEYDB.peerLBDetails.peerPort | int }}&quot;)
{{- end }}
    for node in {0..{{ (sub (.Values.KEYDB.nodes | int) 1) }}}; do
{{/*
      if [ &quot;$host&quot; != &quot;keydb-${node}&quot; ]; then
          replicas+=(&quot;--replicaof keydb-${node}.keydb-headless ${port}&quot;)
*/}}
      if [ &quot;$host&quot; != &quot;{{ include &quot;keydb.fullname&quot; . }}-${node}&quot; ]; then
          replicas+=(&quot;--replicaof {{ include &quot;keydb.fullname&quot; . }}-${node}.{{ include &quot;keydb.fullname&quot; . }}-headless ${port}&quot;)
      fi
    done
    exec keydb-server /etc/keydb/redis.conf \
        --active-replica yes \
        --multi-master yes \
        --appendonly {{ .Values.KEYDB.appendonly }} \
        --bind 0.0.0.0 \
        --port &quot;$port&quot; \
        --protected-mode no \
        --server-threads {{ .Values.threads | int }} \
{{- if .Values.KEYDB.existingSecret }}
        --masterauth $REDIS_PASSWORD \
        --requirepass $REDIS_PASSWORD \
{{- else if .Values.KEYDB.password }}
        --masterauth {{ .Values.KEYDB.password }} \
        --requirepass {{ .Values.KEYDB.password }} \
{{- end }}
      {{- range $key, $value := .Values.KEYDB.configExtraArgs }}
        {{- if $value }}
        --{{ $key }} {{ $value }} \
        {{- else }}
        --{{ $key }} \
        {{- end }}
      {{- end }}
        &quot;${replicas[@]}&quot;
</code></pre>
<p>Description:</p>
<p>I am getting the above error, and not sure how to solve this. What exactly am I doing wrong??</p>
<p>Appreciate your early response.</p>
<p>Syed</p>

Syed Towkhir
<p>The Go text/template <code>range</code> operator rebinds the <code>.</code> special variable, in this case to be the loop index.  In your top-level template you have:</p>
<pre class="lang-yaml prettyprint-override"><code>{{- range $podIndex := until $numOfInstances }}
...
      labels:
        {{ include &quot;keydb.labels&quot; . | nindent 8 }}
...
{{- end }}
</code></pre>
<p>In this context <code>.</code> is the loop index, not the top-level Helm object.  When that parameter gets passed into inner templates you eventually try to resolve <code>.Values.something</code>, but since <code>.</code> is the loop index, you can't look up the <code>Values</code> field on it.</p>
<p>Mechanically, it would probably work to be extremely rigorous about making sure everything uses the special <code>$</code> variable.  You do this in many places in this template, but not everywhere; make sure to reference <code>$.Values</code> and not just <code>.Values</code>, and to pass <code>$</code> to templates instead of <code>.</code>.</p>
<p>However: the structure you have here is a little odd from a Kubernetes point of view.  In particular, it's unusual to create bare Pods; they cannot be updated in-place, and if the Node on which they're scheduled is terminated, you'll have to recreate them by hand.  Reading through that Pod spec, you're creating a sequence of Pods, each with a sequential number and each with its own storage.  This is exactly what a Kubernetes <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">StatefulSet</a> provides you.</p>
<p>If you use a StatefulSet instead, you can get rid of the <code>range</code> loop, and use the &quot;ordinary&quot; <code>.Values</code> and <code>.</code> variables without any special handling; you do not need to worry about <code>$</code> (outside of any inner <code>range</code> or <code>with</code> blocks).</p>
<pre class="lang-yaml prettyprint-override"><code>{{/* no outer range loop */}}
apiVersion: apps/v1
kind: StatefulSet
metadata: { ... }
spec:
  replicas: {{ .Values.CP.replicas }}
  ...
</code></pre>
<p>Where you construct an environment variable from the pod index, you won't be able to do this purely at the Kubernetes YAML layer, but the service will see its <strong>hostname</strong>(8) as the pod name, and that will be of the form <code>statefulset-name-0</code>; you could use a Docker entrypoint wrapper script to set the environment variable to the hostname if it isn't already set.</p>
