While Timoni Bundles offer a way to specify the config values in declarative manner,
not all the configuration values of an application are known ahead of time.
Some values may be available at runtime, in the Kubernetes clusters where the Bundle is applied.
For example, the API token for some backend service that your app consumes is stored in
a Kubernetes Secret in-cluster. When installing the application with Timoni,
you may want to fetch the token from the Kubernetes Secret and use it as a config value.
Timoni Runtime API offers a way to define values which are fetched from the Kubernetes API
and mapped to fields inside a Bundle.
Example
The following is an example of a Runtime definition that extracts values from the cluster.
The values from the Runtime, can be mapped to fields in a Bundle using @timoni() attributes.
Assuming the ConfigMaps and Secrets are in the cluster,
and the Runtime file is runtime.cue and the Bundle file is bundle.cue.
Build the runtime to see which values are present on the cluster:
Apply the Bundle using the values from the Runtime:
Writing a Runtime spec
A Runtime file must contain a definition that matches the following schema:
API version
The apiVersion is a required field that specifies the version of the Runtime schema.
Currently, the only supported value is v1alpha1.
Name
The name is a required field used to identify the Runtime.
The name must be a lowercase identifier (alphanumeric characters, - and _,
starting and ending with an alphanumeric character) of at most 63 characters.
Clusters
The clusters field is for defining the target clusters and
environments (group of clusters) where a Bundle is applied.
A cluster entry must specify the group and kubeContext fields.
The cluster name (the clusters map key) must be a lowercase identifier
(alphanumeric characters, -, _ and ., starting and ending with an
alphanumeric character) of at most 63 characters.
The kubeContext value must match a context name from the .kube/config file.
Default clusterWhen no clusters are defined in the Runtime, Timoni will use the
current context from the kubeconfig, unless the context is specifed
using the --kube-context flag.
Example:
The clusters name and group, can be mapped to fields in a Bundle using @timoni() attributes.
When applying the above Bundle, Timoni will deploy the app instances to all the
clusters, in the order defined in the Runtime. If the apply fails on a staging cluster,
Timoni will stop the execution and not continue with production.
For more details please see the multi-cluster deployments guide.
Values
The values array is for specifying
the list of Kubernetes resources and the fields to be extracted.
Query
The values.query is a required field that specifies the Kubernetes resource.
The query field must be in the format k8s:<apiVersion>:<kind>:<namespace>:<name>.
Example:
If the Kubernetes resource is global, the query format is k8s:<apiVersion>:<kind>:<name>.
Example:
For
The values.for is a required map that specifies which fields to be extracted from the Kubernetes resource.
The for map must contain pairs of name and CUE expression.
Example:
The CUE expression must result in a concrete value of type string, number or bool.
Optional
The optional field can be set to true and Timoni will skip
not found Kubernetes resources instead of throwing an error.
Using values from Kubernetes API
The values defined in a Runtime can be referred in Bundles using CUE attributes.
The @timoni(runtime:[string|number|bool]:[VAR_NAME]) CUE attribute can be placed next
to a field to set its value from the runtime.
If a runtime value is optional, you can provide defaults which will be used
only if the runtime doesn’t contain the referenced value:
To load the runtime values at apply time, use the --runtime flag:
At apply time, Timoni injects the fields values from the runtime,
if a specified runtime var is not found and if a default is not provided,
the apply with fail with an incomplete value error.
Runtime values are validated against the type declared in the attribute before
they are injected. A number value must be a CUE number literal, and a bool
value must be true or false; anything else fails the apply with a
failed to parse attribute error. A string value is quoted, so it may hold
arbitrary text including newlines.
Using values from environment variables
To use values from environment variables,
the environment must contain variables which match the CUE attributes.
Example:
Export the env vars and run the timoni bundle apply --runtime-from-env command.
Mixing values from the Runtime and EnvironmentWhen using timoni bundle apply --runtime runtime.cue --runtime-from-env,
the values coming from the Runtime take precedence over the Environment.