Accessing Files Inside Templates
Helm provides access to files through the object. Before we get going with the template examples, though, there are a few things to note about how this works:
- 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.
- Some files cannot be accessed through the
.Files
object, usually for security reasons.- Files in
templates/
cannot be accessed. - Files excluded using
.helmignore
cannot be accessed.
- Files in
- 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. - Basic example
- Glob patterns
- Encoding
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.
config1.toml
:
config2.toml
:
message = "This is config 2"
config3.toml
:
message = "Goodbye from config 3"
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.
apiVersion: v1
kind: ConfigMap
metadata:
data:
{{- $files := .Files }}
{{- range list "config1.toml" "config2.toml" "config3.toml" }}
{{ . }}: |-
{{ $files.Get . }}
{{- end }}
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 list
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 . }}
.
Path helpers
When working with files, it can be very useful to perform some standardoperations on the file paths themselves. To help with this, Helm imports many ofthe functions from Go’s package for youruse. They are all accessible with the same names as in the Go package, butwith a lowercase first letter. For example, Base
becomes base
, etc.
The imported functions are:
- Base
- Dir
- Ext
- IsAbs
- Clean
As your chart grows, you may find you have a greater need to organize yourfiles more, and so we provide a Files.Glob(pattern string)
method to assistin extracting certain files with all the flexibility of glob patterns.
.Glob
returns a Files
type, so you may call any of the Files
methods onthe returned object.
For example, imagine the directory structure:
foo/:
foo.txt foo.yaml
bar/:
bar.go bar.conf baz.yaml
You have multiple options with Globs:
{{ $root := . }}
{{ range $path, $bytes := .Files.Glob "**.yaml" }}
{{ $path }}: |-
{{ $root.Files.Get $path }}
{{ end }}
Or
{{ $root := . }}
{{ base $path }}: '{{ $root.Files.Get $path | b64enc }}'
{{ end }}
ConfigMap and Secrets utility functions
It is very common to want to place file content into both configmaps andsecrets, for mounting into your pods at run time. To help with this, we provide acouple utility methods on the Files
type.
For further organization, it is especially useful to use these methods inconjunction with the Glob
method.
Given the directory structure from the Glob example above:
You can import a file and have the template base-64 encode it to ensure successful transmission:
apiVersion: v1
metadata:
name: {{ .Release.Name }}-secret
type: Opaque
data:
token: |-
{{ .Files.Get "config1.toml" | b64enc }}
The above will take the same config1.toml
file we used before and encode it:
# Source: mychart/templates/secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: lucky-turkey-secret
type: Opaque
data:
token: |-
bWVzc2FnZSA9IEhlbGxvIGZyb20gY29uZmlnIDEK
Lines
Sometimes it is desirable to access each line of a file in your template. Weprovide a convenient Lines
method for this.
data:
some-file.txt: {{ range .Files.Lines "foo/bar.txt" }}
{{ . }}{{ end }}
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
.