Syntax overview
Jsonnet is the data templating language Tanka uses for expressing what shall be deployed to your Kubernetes cluster. Understanding Jsonnet is crucial to using Tanka effectively.
This page covers the Jsonnet language itself. For more information on how to use Jsonnet with Kubernetes, see the tutorial. There’s also the official Jsonnet tutorial that provides a more detailed review of language features.
Syntax
Being a superset of JSON, the syntax is very similar:
Abstraction
Jsonnet has rich abstraction features, which makes it interesting for configuring Kubernetes, as it allows to keep configurations concise, yet readable.
Imports
Just as other languages, Jsonnet allows code to be imported from other files:
The exported object (the only non-local one) of secret.libsonnet
is now
available as a local
variable called secret
.
When using Tanka, it is also possible to directly import .json
files, as if
they were a .libsonnet
.
Make sure to also take a look at the libraries documentation to learn how to use import
and re-use code.
The documentation on Tanka import paths and vendoring are useful to understand how imports work in Tanka’s context.
Merging
Deep merging allows you to change parts of an object without touching all of it. Consider the following example:
To change the namespace only, we can use the special merge key +:
like so:
The difference between :
and +:
is that the former replaces the original
data at that key, while the latter applies the new object as a patch on top,
meaning that values will be updated if possible but all other stay like they
are.
To merge those two, just add (+
) the patch to the original:
The output of this is the following JSON object:
Functions
Jsonnet supports functions, similar to how Python does. They can be defined in two different ways:
Objects can have methods:
Default values, keyword-args and more examples can be found at jsonnet.org.
Standard library
The Jsonnet standard library includes many helper methods ranging from object and array mutation, over string utils to computation helpers.
Documentation is available at jsonnet.org.
Conditionals
Jsonnet supports a conditionals in a fashion similar to a ternary operator:
More on jsonnet.org.
References
Jsonnet has multiple options to refer to parts of an object:
For more information take a look at jsonnet.org