Difference between $.Values and .Values in Helm
Asked Answered
S

1

9

I understand the usage of .Values.varName in Helm chart and which is used to refer the variable from Values.yaml file

I would like to know the difference between $.Values.varName and .Values.varName

For example, when I set name: $.Values.varName and name: .Values.varName

Steeplebush answered 21/8, 2023 at 4:36 Comment(0)
P
13

In the Go text/template language, like many others, . is a field-access operator. In the more obvious latter half of that expression, .Values.varName looks up the varName field (either an object field or a map member) in the values object.

Helm has a standard top-level object with several fields, including Values. So both of these expressions normally start from the top-level object, look up Values in it, and then look up varName within that.

In the text/template language, . is also a variable. When you evaluate a top-level templates/*.yaml file, . is the top-level Helm object, but inside a define function it's the single parameter to the template, inside a range loop it's the current loop item, and so on. In What is, and what use cases have the dot "." in helm charts? I list out more cases.

And finally $ is a variable as well. In the context of for example a range loop, $ keeps the top-level value, even if . changes. $ should always be the top-level Helm value.

The place where this makes a difference is in the context of a range loop or something else that rebinds .. In that case, you need $.Values to refer to the top-level object.


As an example, let's say values.yaml contains

varName: foo
someNumbers:
  - one
  - two
  - three

Then in this template file:

apiVersion: v1
kind: ConfigMap
metadata:
  annotations:
    # in the top level template so these are the same
    value-is-foo: {{ .Values.varName }}
    value-is-also-foo: {{ $.Values.varName }}
data:
{{- range .Values.someNumbers }}
  # now $ is the top-level object, but . is "one", "two", "three"
  {{ . }}: one, two, or three
  {{ $.Values.varName }}-{{ . }}: foo-one, foo-two, foo-three
  {{ .Values.varName }}: |-
    an error, since the string "one" doesn't have a field or
    member "Values"
{{- end }}
Pointsman answered 21/8, 2023 at 11:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.