Hidden Fields

Learn to use hidden fields to organize Kubernetes objects in ksonnet

Hidden fields in Jsonnet provides a way to store information that will not be in the generated JSON.

Usage of hidden fields can be found in prototypes where the same Kubernetes configuration is used across multiple environments.

This pattern is heavily utilized by kubeflow where relevant objects are grouped in an array inside a given prototype.

The basic usage of a hidden field is to store a key-value pair for access elsewhere.

test.jsonnet
1
2
3
4
5
6
{
  hidden:: {
    foo: "bar"
  },
  a: self.hidden.foo
}

Evaluating the jsonnet file above shows how data can be accessed from a hidden field while keeping the initial key-value declaration hidden.

$ jsonnet eval example.jsonnet
{
   "a": "bar"
}

In ksonnet, a hidden field called parts by convention is used to group these configurations. Then additional hidden fields are created for each Kubernetes object such as a configMap or Deployment.

{
  parts:: {
    configmap:: {
      // configMap goes here
    },
    deployment:: {
      // Deployment goes here
    },
  },
}

A prototype can import the jsonnet file (also called libsonnet by convention) and choose any number of parts in the array. This pattern allows reuse of Kubernetes objects in a modular way.

registry/package/prototypes/example.jsonnet
1
2
3
4
...
local nginx = import 'registry/package/nginx.libsonnet';

[nginx.parts.configmap, nginx.parts.deployment]

See Create a Registry for more information on building a prototype in a registry.

Last updated on: September 5, 2018