github.com/y-taka-23/helm@v2.8.0+incompatible/docs/chart_template_guide/variables.md (about)

     1  # Variables
     2  
     3  With functions, pipelines, objects, and control structures under our belts, we can turn to one of the more basic ideas in many programming languages: variables. In templates, they are less frequently used. But we will see how to use them to simplify code, and to make better use of `with` and `range`.
     4  
     5  In an earlier example, we saw that this code will fail:
     6  
     7  ```yaml
     8    {{- with .Values.favorite }}
     9    drink: {{ .drink | default "tea" | quote }}
    10    food: {{ .food | upper | quote }}
    11    release: {{ .Release.Name }}
    12    {{- end }}
    13  ```
    14  
    15  `Release.Name` is not inside of the scope that's restricted in the `with` block. One way to work around scoping issues is to assign objects to variables that can be accessed without respect to the present scope.
    16  
    17  In Helm templates, a variable is a named reference to another object. It follows the form `$name`. Variables are assigned with a special assignment operator: `:=`. We can rewrite the above to use a variable for `Release.Name`.
    18  
    19  ```yaml
    20  apiVersion: v1
    21  kind: ConfigMap
    22  metadata:
    23    name: {{ .Release.Name }}-configmap
    24  data:
    25    myvalue: "Hello World"
    26    {{- $relname := .Release.Name -}}
    27    {{- with .Values.favorite }}
    28    drink: {{ .drink | default "tea" | quote }}
    29    food: {{ .food | upper | quote }}
    30    release: {{ $relname }}
    31    {{- end }}
    32  ```
    33  
    34  Notice that before we start the `with` block, we assign `$relname := .Release.Name`. Now inside of the `with` block, the `$relname` variable still points to the release name.
    35  
    36  Running that will produce this:
    37  
    38  ```yaml
    39  # Source: mychart/templates/configmap.yaml
    40  apiVersion: v1
    41  kind: ConfigMap
    42  metadata:
    43    name: viable-badger-configmap
    44  data:
    45    myvalue: "Hello World"
    46    drink: "coffee"
    47    food: "PIZZA"
    48    release: viable-badger
    49  ```
    50  
    51  Variables are particularly useful in `range` loops. They can be used on list-like objects to capture both the index and the value:
    52  
    53  ```yaml
    54    toppings: |-
    55      {{- range $index, $topping := .Values.pizzaToppings }}
    56        {{ $index }}: {{ $topping }}
    57      {{- end }}
    58  
    59  ```
    60  
    61  Note that `range` comes first, then the variables, then the assignment operator, then the list. This will assign the integer index (starting from zero) to `$index` and the value to `$topping`. Running it will produce:
    62  
    63  ```yaml
    64    toppings: |-
    65        0: mushrooms
    66        1: cheese
    67        2: peppers
    68        3: onions
    69  ```
    70  
    71  For data structures that have both a key and a value, we can use `range` to get both. For example, we can loop through `.Values.favorite` like this:
    72  
    73  ```yaml
    74  apiVersion: v1
    75  kind: ConfigMap
    76  metadata:
    77    name: {{ .Release.Name }}-configmap
    78  data:
    79    myvalue: "Hello World"
    80    {{- range $key, $val := .Values.favorite }}
    81    {{ $key }}: {{ $val | quote }}
    82    {{- end}}
    83  ```
    84  
    85  Now on the first iteration, `$key` will be `drink` and `$val` will be `coffee`, and on the second, `$key` will be `food` and `$val` will be `pizza`. Running the above will generate this:
    86  
    87  ```yaml
    88  # Source: mychart/templates/configmap.yaml
    89  apiVersion: v1
    90  kind: ConfigMap
    91  metadata:
    92    name: eager-rabbit-configmap
    93  data:
    94    myvalue: "Hello World"
    95    drink: "coffee"
    96    food: "pizza"
    97  ```
    98  
    99  Variables are normally not "global". They are scoped to the block in which they are declared. Earlier, we assigned `$relname` in the top level of the template. That variable will be in scope for the entire template. But in our last example, `$key` and `$val` will only be in scope inside of the `{{range...}}{{end}}` block.
   100  
   101  However, there is one variable that is always global - `$` - this
   102  variable will always point to the root context.  This can be very
   103  useful when you are looping in a range need to know the chart's release
   104  name.
   105  
   106  An example illustrating this:
   107  ```yaml
   108  {{- range .Values.tlsSecrets }}
   109  apiVersion: v1
   110  kind: Secret
   111  metadata:
   112    name: {{ .name }}
   113    labels:
   114      # Many helm templates would use `.` below, but that will not work, 
   115      # however `$` will work here 
   116      app: {{ template "fullname" $ }}
   117      # I cannot reference .Chart.Name, but I can do $.Chart.Name
   118      chart: "{{ $.Chart.Name }}-{{ $.Chart.Version }}"
   119      release: "{{ $.Release.Name }}"
   120      heritage: "{{ $.Release.Service }}"
   121  type: kubernetes.io/tls
   122  data:
   123    tls.crt: {{ .certificate }}
   124    tls.key: {{ .key }}
   125  ---
   126  {{- end }}
   127  ```
   128  
   129  So far we have looked at just one template declared in just one file. But one of the powerful features of the Helm template language is its ability to declare multiple templates and use them together. We'll turn to that in the next section.