ksonnet: Simplify working with Kubernetes

A Jsonnet library that compiles to Kubernetes YAML

Type Jsonnet code

loading...

Get API-compatible Kubernetes YAML objects

Tools for Kubernetes configuration.

Install the ksonnet toolchain

# Install Jsonnet CLI tool, clone the library repository
brew install jsonnet
git clone git@github.com:ksonnet/ksonnet-lib.git

Install the Visual Studio Code extension

# Install the Jsonnet Visual Studio Code extension
code --install-extension heptio.jsonnet

ksonnet: a Jsonnet library.

Jsonnet is an open-source JSON templating language from Google. Its design is informed in part by the experience of using Borg Configuration Language (BCL) to operate some of the largest clusters on the planet.

As a Jsonnet library, ksonnet-lib leverages the modern langauge features of Jsonnet to make it easy to write flexible, modular Kubernetes applications.


Kubernetes objects with sensible defaults.

ksonnet includes a number of utility functions that allow users to easily create sensible default values for the Kuberenetes API objects. This way, developers can use the API, but ignore the parts of it they don't need.

This example defines a deployment for an nginx container, and then generates a default Deployment object from it.

Input Jsonnet code:

local k = import "ksonnet.beta.2/k.libsonnet";
local deployment = k.apps.v1beta1.deployment;
local container = deployment.mixin.spec.template.spec.containersType;
local containerPort = container.portsType;

// Create nginx container with container port 80 open.
local nginxContainer =
  container.new("nginx", "nginx:1.13.0") +
  container.ports(containerPort.newNamed("http", 80));

// Create default `Deployment` object from nginx container.
deployment.new("nginx", 5, nginxContainer, {app: "nginx"})


Output YAML:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 5
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - image: 'nginx:1.13.0'
          name: nginx
          ports:
            - containerPort: 80
              name: http

Flexible when the defaults aren't right for you.

Sometimes the defaults aren't enough. ksonnet uses Jsonnet's flexible object model to let users "mix in" changes to objects. This example shows how to write these mixins to make changes to the update strategy or the selector.

Learn more about Jsonnet mixins. →

local k = import "ksonnet.beta.2/k.libsonnet";
local deployment = k.apps.v1beta1.deployment;
local container = deployment.mixin.spec.template.spec.containersType;
local containerPort = container.portsType;

// Create nginx container with container port 80 open.
local nginxContainer =
  container.new("nginx", "nginx:1.13.0") +
  container.ports(containerPort.newNamed("http", 80));

// Create default `Deployment` object from nginx container.
deployment.new("nginx", 5, nginxContainer, {app: "nginx"}) +
deployment.mixin.spec.revisionHistoryLimit(10) +
deployment.mixin.spec.minReadySeconds(60)

Use templates only when you need them.

ksonnet is designed to work well with JSON versions of the Kubernetes API objects. This approach lets users choose templates only when they need them, and not when they don't.

This example uses the ksonnet mixin facilities to customize a default deployment object. Notice that the + operator is all you need to work with the raw JSON.

local k = import "ksonnet.beta.2/k.libsonnet";
local deployment = k.apps.v1beta1.deployment;
local spec = deployment.mixin.spec;

{
  "apiVersion": "extensions/v1beta1",
  "kind": "Deployment",
  "metadata": {
    "name": "nginx",
  },
  "spec": {
    "replicas": 2,
    "template": {
        "spec": {
          "containers": [{
            "image": "nginx:1.7.9",
            "imagePullPolicy": "Always",
            "name": "nginx",
          }],
}}}} +
spec.revisionHistoryLimit(10) +
spec.template.metadata.labels({ "app": "nginx" })

Visual Studio Code support.

Tooling is important. ksonnet is supported by a VS Code extension that includes static analysis features, like autocompletion and syntax highlighting. More are on the way!

autocompletion

Who is working with ksonnet?