Abstraction
While we won’t need to touch the resource definitions directly that frequently
anymore now that our deployments definitions are parametrized, the
main.jsonnet
file is still very long and hard to read. Especially because of
all the brackets, it’s even worse than yaml at the moment.
Splitting it up
Let’s start cleaning this up by separating logical pieces into distinct files:
main.jsonnet
: Still our main file, importing the other filesgrafana.libsonnet
:Deployment
andService
for the Grafana instanceprometheus.libsonnet
:Deployment
andService
for the Prometheus server
The file should contain an object with the same function that was defined under the grafana
in /environments/default/main.jsonnet
, but called new
instead of grafana
.
Do the same for /environments/default/prometheus.libsonnet
as well.
Helper utilities
While main.jsonnet
is now short and very readable, the other two files are not
really an improvement over regular yaml, mostly because they are still full of
boilerplate.
Let’s use functions to create some useful helpers to reduce the amount of
repetition. For that, we create a new file called kubernetes.libsonnet
, which
will hold our Kubernetes utilities.
A Deployment constructor
Creating a Deployment
requires some mandatory information and a lot of
boilerplate. A function that creates one could look like this:
Invoking this function will substitute all the variables with the respective passed function parameters and return the assembled object.
Let’s simplify our grafana.libsonnet
a bit:
This drastically simplified the creation of the Deployment
, because we do not
need to remember how exactly a Deployment
is structured anymore. Just use
our helper and you are good to go.