github.com/y-taka-23/helm@v2.8.0+incompatible/docs/chart_best_practices/templates.md (about) 1 # Templates 2 3 This part of the Best Practices Guide focuses on templates. 4 5 ## Structure of templates/ 6 7 The templates directory should be structured as follows: 8 9 - Template files should have the extension `.yaml` if they produce YAML output. The 10 extension `.tpl` may be used for template files that produce no formatted content. 11 - Template file names should use dashed notation (`my-example-configmap.yaml`), not camelcase. 12 - Each resource definition should be in its own template file. 13 - Template file names should reflect the resource kind in the name. e.g. `foo-pod.yaml`, 14 `bar-svc.yaml` 15 16 ## Names of Defined Templates 17 18 Defined templates (templates created inside a `{{ define }} ` directive) are 19 globally accessible. That means that a chart and all of its subcharts will have 20 access to all of the templates created with `{{ define }}`. 21 22 For that reason, _all defined template names should be namespaced._ 23 24 Correct: 25 26 ```yaml 27 {{- define "nginx.fullname" }} 28 {{/* ... */}} 29 {{ end -}} 30 ``` 31 32 Incorrect: 33 34 ```yaml 35 {{- define "fullname" -}} 36 {{/* ... */}} 37 {{ end -}} 38 ``` 39 It is highly recommended that new charts are created via `helm create` command as the template names are automatically defined as per this best practice. 40 41 ## Formatting Templates 42 43 Templates should be indented using _two spaces_ (never tabs). 44 45 Template directives should have whitespace after the opening braces and before the 46 closing braces: 47 48 Correct: 49 ``` 50 {{ .foo }} 51 {{ print "foo" }} 52 {{- print "bar" -}} 53 ``` 54 55 Incorrect: 56 ``` 57 {{.foo}} 58 {{print "foo"}} 59 {{-print "bar"-}} 60 ``` 61 62 Templates should chomp whitespace where possible: 63 64 ``` 65 foo: 66 {{- range .Values.items }} 67 {{ . }} 68 {{ end -}} 69 ``` 70 71 Blocks (such as control structures) may be indented to indicate flow of the template code. 72 73 ``` 74 {{ if $foo -}} 75 {{- with .Bar }}Hello{{ end -}} 76 {{- end -}} 77 ``` 78 79 However, since YAML is a whitespace-oriented language, it is often not possible for code indentation to follow that convention. 80 81 ## Whitespace in Generated Templates 82 83 It is preferable to keep the amount of whitespace in generated templates to 84 a minimum. In particular, numerous blank lines should not appear adjacent to each 85 other. But occasional empty lines (particularly between logical sections) is 86 fine. 87 88 This is best: 89 90 ```yaml 91 apiVersion: batch/v1 92 kind: Job 93 metadata: 94 name: example 95 labels: 96 first: first 97 second: second 98 ``` 99 100 This is okay: 101 102 ```yaml 103 apiVersion: batch/v1 104 kind: Job 105 106 metadata: 107 name: example 108 109 labels: 110 first: first 111 second: second 112 113 ``` 114 115 But this should be avoided: 116 117 ```yaml 118 apiVersion: batch/v1 119 kind: Job 120 121 metadata: 122 name: example 123 124 125 126 127 128 labels: 129 first: first 130 131 second: second 132 133 ``` 134 135 ## Comments (YAML Comments vs. Template Comments) 136 137 Both YAML and Helm Templates have comment markers. 138 139 YAML comments: 140 ```yaml 141 # This is a comment 142 type: sprocket 143 ``` 144 145 Template Comments: 146 ```yaml 147 {{- /* 148 This is a comment. 149 */ -}} 150 type: frobnitz 151 ``` 152 153 Template comments should be used when documenting features of a template, such as explaining a defined template: 154 155 ```yaml 156 {{- /* 157 mychart.shortname provides a 6 char truncated version of the release name. 158 */ }} 159 {{ define "mychart.shortname" -}} 160 {{ .Release.Name | trunc 6 }} 161 {{- end -}} 162 163 ``` 164 165 Inside of templates, YAML comments may be used when it is useful for Helm users to (possibly) see the comments during debugging. 166 167 ``` 168 # This may cause problems if the value is more than 100Gi 169 memory: {{ .Values.maxMem | quote }} 170 ``` 171 172 The comment above is visible when the user runs `helm install --debug`, while 173 comments specified in `{{- /* */ -}}` sections are not. 174 175 ## Use of JSON in Templates and Template Output 176 177 YAML is a superset of JSON. In some cases, using a JSON syntax can be more 178 readable than other YAML representations. 179 180 For example, this YAML is closer to the normal YAML method of expressing lists: 181 182 ```yaml 183 arguments: 184 - "--dirname" 185 - "/foo" 186 ``` 187 188 But it is easier to read when collapsed into a JSON list style: 189 190 ```yaml 191 arguments: ["--dirname", "/foo"] 192 ``` 193 194 Using JSON for increased legibility is good. However, JSON syntax should not 195 be used for representing more complex constructs. 196 197 When dealing with pure JSON embedded inside of YAML (such as init container 198 configuration), it is of course appropriate to use the JSON format.