@embed() attribute.
At build time, the file contents are loaded into CUE values that templates can use
like any other value, for example to populate the data of a Kubernetes ConfigMap or Secret.
Embedding is the type-safe alternative to inlining file contents as CUE strings:
the files keep their native extension, so they can be edited, linted and highlighted
with the standard tooling for their format, while the module retains full control
over how their contents end up in the generated Kubernetes objects.
Embedding text files
Assuming you want to ship annginx configuration and a static HTML page with your module,
stored as plain files in the module’s templates directory:
templates/configmap.cue file, enable embedding with the file-level
@extern(embed) attribute, then embed the sibling files with @embed():
type=text argument tells CUE to load the file contents as a string; it is required
for file extensions CUE does not recognise, such as .conf and .html.
Combined with the #ImmutableConfig generator, any change to the
embedded files results in a ConfigMap with a new name suffix, which triggers a rolling
update of the workloads referencing it.
Templating the file contents
Embedded files are static, but their contents can be rendered with the instance configuration using the CUEtext/template package, which implements Go templating:
templates/nginx.default.conf file referencing the config values
using Go template expressions:
nginx configuration and HTML index page.
Embedding structured data
When embedding.json, .yaml or .toml files without the type argument,
CUE parses the file contents into a structured value instead of a string.
The parsed value can be validated against a CUE schema and further transformed
at build time:
Embedding multiple files
A glob pattern embeds a set of files as a struct, keyed by the file path relative to the CUE file:dashboards/latency.json.
Since Kubernetes does not allow / in ConfigMap keys, the comprehension above
uses path.Base to strip the directory from the keys.
Restrictions
CUE enforces the following rules for embedded files:- Files must be located inside the module’s root directory, referencing files
outside the module with
../or absolute paths is not allowed. - Hidden files (dot files) and files under
cue.modcannot be embedded, nor matched by glob patterns.
timoni.ignore file, make sure its patterns do not exclude
embedded files, otherwise timoni mod push would strip them from the
module’s OCI artifact and the build would fail on the consumer’s side.