Tue 23 March 2021

Streamlining Terraform configuration with Jsonnet

At Codethink, we regularly work with different tools and technologies across a range of clients. Terraform is a common tool we see used within these technology stacks for deploying cloud services from infrastructure as code. However, Terraform definitions are rarely defined in isolation without configurations for other tools such as Ansible, Puppet, Kubernetes. These definitions' close relationships often lead to changes in the configurations for multiple tools across various configuration languages.

What's wrong with HCL?

The problems with HCL (Hashicorp Configuration Language) for Terraform start with its domain-specific nature. As a Domain Specific Language (DSL), it requires additional learning for engineers who are previously unfamiliar with the tooling. Being specific to Hashicorp tooling, a dependency on HCL is likely to become a blocker towards any efforts to unify infrastructure and application configurations. In addition to these problems, HCL is not great for writing your configurations with any level of abstraction. For example, the lack of object orientation within HCL prevents teams from writing libraries for regularly used blocks of HCL. This leads to very verbose configuration files with endless duplication across services and projects.

Jsonnet as the solution

Jsonnet is a data templating language created by Google as an extension of json with support for functions, patching and imports. Jsonnet provides a means of defining infrastructure as code in a singular language that it can render into a variety of configuration languages (json, yaml, INI) for usage. Jsonnet has been heavily adopted by Grafana Labs, DataBricks and Bitnami for management of their service deployment configurations.

Jsonnet allows companies to define their configurations abstractly to facilitate sharing common components across many teams. These shared configurations can help save a team countless hours getting up and running without the need to copy and paste static configuration files. By effectively structuring configurations, service-specific definitions can be minimalized through well-defined libraries and well-constructed patches.

We can see an example of this in the Celduin project, where a range of environments for dev, staging and production can each be defined in less than 50 lines by patching the shared libraries. Each of the configuration layers can be abstracted away from terraform to kubernetes to application specific configs, these layers can be initialised with some parameters and evaluated to output the full configuration tree for deployment. Thanks to jsonnet patching, we can have these powerful abstractions available whilst still changing any fine detail within the configuration.

Tooling

Since its creation in 2014, many tools have been built to support and extend jsonnet usage for different use cases. There are several implementations for the interpreter, jsonnet in C++, go-jsonnet (a faster version in Go) and sjsonnet by Databricks written in Scala. In large configurations, time taken to evaluate and render json can be substantial if consideration is not taken as to which interpreter to use. For general use, go-jsonnet is recommended. However, sjsonnet provides a significant reduction in evaluation time in exchange for the odd incompatibility with go-jsonnet.

Integrating jsonnet directly into deployment workflows usually requires some form of orchestration to render and pipe the manifests into relevant tools such as terraform or kubectl. To tackle these issues, a variety of specialized tools have been created. A few are listed below of notable mention:

  • kubecfg: Created by Bitnami as an extension of kubectl accepting jsonnet configurations.
  • rules_jsonnet: Allows evaluation of jsonnet configurations within bazel to orchestrate build/deploy pipelines.
  • Tanka: Created by Grafana Labs for deploying jsonnet definitions to kubernetes.

Summary

On the whole, we believe jsonnet to be an ideal language to bridge the gap for configuring our infrastructure and applications for deployment. With well designed libraries, concise deployment configurations can be written to take advantage of previous efforts to provide meaningful abstractions in place of unnecessary duplication of configurations. The single language across these varying levels of confguration allows for teams to focus on the engineering challenges that really matter without worry of keeping infrastructure configurations in sync with other changes.

Related to the blog post:

Other Articles

Get in touch to find out how Codethink can help you

sales@codethink.co.uk +44 161 660 9930