Accessing Files Inside Templates – Helm

In the previous section, we discussed several ways to create and access named templates. This makes it easy to import a template from another template. But sometimes it is desirable to import a file that is not a template and inject its content without sending the content through the template renderer.

Helm provides access to files through the . Files object. However, before we get started with the template examples, there are a few things to keep in mind about how this works:

  • It’s okay to add extra files to your Helm chart. These files will be packaged. However, be careful. Graphics must be smaller than 1M due to storage limitations of Kubernetes objects.
  • Some files cannot be accessed through the . File object, usually for security reasons. Files in templates cannot be accessed.
    • Excluded files cannot be accessed using .helmignore
    • .

    • Files outside of a subgraphic of the helm application, including those of the parent, cannot be accessed.
  • Graphics do not preserve UNIX-mode information, so file-level permissions will not affect the availability of a file when it comes to the . Files object.

Basic example

  • Route helpers
  • Glob patterns
  • ConfigMap and Secrets utility functions
  • Coding
  • lines

  • Basic example

With those warnings behind us, let’s write a template that reads three files in our ConfigMap. To get started, we’ll add three files to the chart, placing all three directly inside the mychart/ directory.

config1.toml:config2.toml:

config3.toml

:

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 traverse them and inject their contents into our ConfigMap.

This configuration map uses several of the techniques described in the previous sections. For example, we create a $files variable to contain a reference to . Files object. We also use the tuple function to create a list of files we go through. Then we print each file name ({{ . }}: |-) followed by the contents of the file {{ $files. Get. }}.

Running this template will produce a single ConfigMap with the contents of all three files:

Path helpers

When working with files, it can be very useful to perform some standard operations on the file paths themselves. To help with this, Helm imports many of the functions from the Go route package for use. All are accessible with the same names as in the Go package, but with a lowercase first letter. For example, Base becomes base, etc.

The imported functions are:

  • Base
  • Dir
  • Ext
  • IsAbs
  • Clean

Glob patterns

As your chart grows, you may have a greater need to organize your files more, so we provide a Files.Glob method (pattern string) to help extract certain files with all the flexibility of glob patterns.

. Glob returns a Files type, so you can call any of the Files methods on the returned object.

For example, imagine the directory structure

:

You have several options with the Globs:

Or

ConfigMap

and Secrets utility functions

(available Helm 2.0.2 and later)

It is very common to want to place the contents of the file in both ConfigMaps and Secrets, to mount it in your pods at runtime. To help with this, we provide a couple of utility methods in the Files type.

For further organization, it is especially useful to use these methods in conjunction with the Glob method.

Given the directory structure of the example Glob

above:

Encoding

You can import a file and

have the base-64 template encode it to ensure successful transmission

:

The above will take the same config1.toml file we used before and encode it:

Lines

Sometimes it is desirable to access every line of a file in your template. We provide a convenient line method for this.

You can traverse Lines using a range function:

There is no way to pass external files to the chart during rudder installation. Therefore, if you ask users to provide data, it must be loaded using helm install -f or helm install -set.

This discussion concludes our dive into Helm’s tools and techniques for writing templates. In the next section we will see how you can use a special file, templates/NOTES.txt, to send post-installation instructions to users of your chart.

Contact US