mmusnjak / jsonnet-demo

Generating Kubernetes manifests using Jsonnet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using Jsonnet to generate Kubernetes cluster manifests

This repository shows one possible way of using plain Jsonnet to generate Kubernetes manifests. It makes use of various Jsonnet libraries, such as jsonnet-libs and others.

Prerequisites

You'll need to install the following:

Downloading dependencies

Jsonnet-bundler is used to download the dependencies to the vendor directory. You need to do this once before being able to build the manifests.

jb install

Later, you'll also need to do it if the remote repository has updated the dependencies. You can update dependencies yourself by running jb update. Make sure to rebuild the manifests after that to confirm no unwanted changes were caused.

Building the output manifests

You can run the scripts/build.sh script to regenerate the output manifests. This will format all jsonnet and libsonnet files in the clusters and lib directories, and then save the rendered manifests to the manifests directory.

You can call that script with one or more directories or jsonnet files to avoid rebuilding all the manifests. If called with a directory, it will process all jsonnet files in that directory and all its children.

# Builds all the clusters in dev environment:
scripts/build.sh clusters/dev

# Builds all the manifests for dev-cluster-1:
scripts/build.sh clusters/dev/dev-cluster-1 

# Builds all the manifests for dev-cluster-1 and for prod-cluster-1:
scripts/build.sh clusters/dev/dev-cluster-1 clusters/prod/prod-cluster-1

Project structure

  • clusters: contains the environments and clusters for which we're generating manifests
  • clusters/dev: contains shared environment config for all dev clusters, and a directory for each cluster
  • clusters/dev/dev-cluster-1: Jsonnet source for the cluster manifests. In this example, it's one jsonnet file per namespace
  • lib: shared libraries for this repository. This can contain application templates that are reusable in different environments
  • manifests: rendered manifests ready to be applied to the clusters. The internal directory structure replicates the structure of the clusters directory
  • vendor: the downloaded external dependencies used. The contents are controlled by jsonnetfile.json and the corresponding lock file, and managed by the jsonnet-bundler tool
  • scripts: various scripts, including pre-commit checks and the build script

Learning more

About

Generating Kubernetes manifests using Jsonnet


Languages

Language:Jsonnet 78.1%Language:Shell 21.9%