github.com/migueleliasweb/helm@v2.6.1+incompatible/docs/chart_template_guide/accessing_files.md (about) 1 # Accessing Files Inside Templates 2 3 In the previous section we looked at several ways to create and access named templates. This makes it easy to import one template from within another template. But sometimes it is desirable to import a _file that is not a template_ and inject its contents without sending the contents through the template renderer. 4 5 Helm provides access to files through the `.Files` object. Before we get going with the template examples, though, there are a few things to note about how this works: 6 7 - It is okay to add extra files to your Helm chart. These files will be bundled and sent to Tiller. Be careful, though. Charts must be smaller than 1M because of the storage limitations of Kubernetes objects. 8 - Some files cannot be accessed through the `.Files` object, usually for security reasons. 9 - Files in `templates/` cannot be accessed. 10 - Charts do not preserve UNIX mode information, so file-level permissions will have no impact on the availability of a file when it comes to the `.Files` object. 11 12 <!-- (see https://github.com/jonschlinkert/markdown-toc) --> 13 14 <!-- toc --> 15 16 - [Basic example](#basic-example) 17 - [Path helpers](#path-helpers) 18 - [Glob patterns](#glob-patterns) 19 - [ConfigMap and Secrets utility functions](#configmap-and-secrets-utility-functions) 20 - [Secrets](#secrets) 21 - [Lines](#lines) 22 23 <!-- tocstop --> 24 25 ## Basic example 26 27 With those caveats behind, let's write a template that reads three files into our ConfigMap. To get started, we will add three files to the chart, putting all three directly inside of the `mychart/` directory. 28 29 `config1.toml`: 30 31 ```toml 32 message = Hello from config 1 33 ``` 34 35 `config2.toml`: 36 37 ```toml 38 message = This is config 2 39 ``` 40 41 `config3.toml`: 42 43 ```toml 44 message = Goodbye from config 3 45 ``` 46 47 Each of these is a simple TOML file (think old-school Windows INI files). We know the names of these files, so we can use a `range` function to loop through them and inject their contents into our ConfigMap. 48 49 ```yaml 50 apiVersion: v1 51 kind: ConfigMap 52 metadata: 53 name: {{ .Release.Name }}-configmap 54 data: 55 {{- $files := .Files }} 56 {{- range tuple "config1.toml" "config2.toml" "config3.toml" }} 57 {{ . }}: |- 58 {{ $files.Get . }} 59 {{- end }} 60 ``` 61 62 This config map uses several of the techniques discussed in previous sections. For example, we create a `$files` variable to hold a reference to the `.Files` object. We also use the `tuple` function to create a list of files that we loop through. Then we print each file name (`{{.}}: |-`) followed by the contents of the file `{{ $files.Get . }}`. 63 64 Running this template will produce a single ConfigMap with the contents of all three files: 65 66 ```yaml 67 # Source: mychart/templates/configmap.yaml 68 apiVersion: v1 69 kind: ConfigMap 70 metadata: 71 name: quieting-giraf-configmap 72 data: 73 config1.toml: |- 74 message = Hello from config 1 75 76 config2.toml: |- 77 message = This is config 2 78 79 config3.toml: |- 80 message = Goodbye from config 3 81 ``` 82 83 ## Path helpers 84 85 When working with files, it can be very useful to perform some standard 86 operations on the file paths themselves. To help with this, Helm imports many of 87 the functions from Go's [path](https://golang.org/pkg/path/) package for your 88 use. They are all accessible with the same names as in the Go package, but 89 with a lowercase first letter. For example, `Base` becomes `base`, etc. 90 91 The imported functions are: 92 - Base 93 - Dir 94 - Ext 95 - IsAbs 96 - Clean 97 98 ## Glob patterns 99 100 As your chart grows, you may find you have a greater need to organize your 101 files more, and so we provide a `Files.Glob(pattern string)` method to assist 102 in extracting certain files with all the flexibility of [glob patterns](https://godoc.org/github.com/gobwas/glob). 103 104 `.Glob` returns a `Files` type, so you may call any of the `Files` methods on 105 the returned object. 106 107 For example, imagine the directory structure: 108 109 ``` 110 foo/: 111 foo.txt foo.yaml 112 113 bar/: 114 bar.go bar.conf baz.yaml 115 ``` 116 117 You have multiple options with Globs: 118 119 120 ```yaml 121 {{ range $path := .Files.Glob "**.yaml" }} 122 {{ $path }}: | 123 {{ .Files.Get $path }} 124 {{ end }} 125 ``` 126 127 Or 128 129 ```yaml 130 {{ range $path, $bytes := .Files.Glob "foo/*" }} 131 {{ $path }}: '{{ b64enc $bytes }}' 132 {{ end }} 133 ``` 134 135 ## ConfigMap and Secrets utility functions 136 137 (Not present in version 2.0.2 or prior) 138 139 It is very common to want to place file content into both configmaps and 140 secrets, for mounting into your pods at run time. To help with this, we provide a 141 couple utility methods on the `Files` type. 142 143 For further organization, it is especially useful to use these methods in 144 conjunction with the `Glob` method. 145 146 Given the directory structure from the [Glob][Glob patterns] example above: 147 148 ```yaml 149 apiVersion: v1 150 kind: ConfigMap 151 metadata: 152 name: conf 153 data: 154 {{ (.Files.Glob "foo/*").AsConfig | indent 2 }} 155 --- 156 apiVersion: v1 157 kind: Secret 158 metadata: 159 name: very-secret 160 type: Opaque 161 data: 162 {{ (.Files.Glob "bar/*").AsSecrets | indent 2 }} 163 ``` 164 165 ## Secrets 166 167 When working with a Secret resource, you can import a file and have the template base-64 encode it for you: 168 169 ```yaml 170 apiVersion: v1 171 kind: Secret 172 metadata: 173 name: {{ .Release.Name }}-secret 174 type: Opaque 175 data: 176 token: |- 177 {{ .Files.Get "config1.toml" | b64enc }} 178 ``` 179 180 The above will take the same `config1.toml` file we used before and encode it: 181 182 ```yaml 183 # Source: mychart/templates/secret.yaml 184 apiVersion: v1 185 kind: Secret 186 metadata: 187 name: lucky-turkey-secret 188 type: Opaque 189 data: 190 token: |- 191 bWVzc2FnZSA9IEhlbGxvIGZyb20gY29uZmlnIDEK 192 ``` 193 194 ## Lines 195 196 Sometimes it is desirable to access each line of a file in your template. We 197 provide a convenient `Lines` method for this. 198 199 ```yaml 200 data: 201 some-file.txt: {{ range .Files.Lines "foo/bar.txt" }} 202 {{ . }}{{ end }} 203 ``` 204 205 Currently, there is no way to pass files external to the chart during `helm install`. So if you are asking users to supply data, it must be loaded using `helm install -f` or `helm install --set`. 206 207 This discussion wraps up our dive into the tools and techniques for writing Helm templates. In the next section we will see how you can use one special file, `templates/NOTES.txt`, to send post-installation instructions to the users of your chart. 208