Engines and context

What renders a template, which engines exist, and what they receive.

Engines

Admitik ships several engines because not every rule reads well in the same form. The goal of the project is that you spend your time on the rule and not on the syntax. There are several ones supported.

An engine is the piece that turns the text of a condition, a message or a patch into a value. Every field with a template names the one it wants, and one policy can mix them.

CEL

An expression language: one expression, no statements. It is the engine for rules that fit on one line, or a few of them.

key: has(object.metadata.labels) && "team" in object.metadata.labels

key: object.spec.replicas <= 10

Starlark

A Python-like language: functions, loops, comprehensions and modules. It is where the rules that are a program live, and the one to reach for when a rule needs to build something before deciding.

These are the modules Admitik loads for it, from regular expressions to YAML, DNS and HTTP:

ModuleWhat it gives you
yamlDecode and encode YAML
jsonDecode and encode JSON
reRegular expressions: match, search, find, split, replace
stringCharacter sets, search, quoting and escaping
hashlibmd5, sha1, sha256 and sha512
base64Standard and URL-safe encoding and decoding
csvRead and write delimited text
mathFunctions and constants
timeDurations, parsing and formatting
randomNumbers, strings and UUIDs
logWrite a line with fields, at a level
httpHTTP requests with headers, body and status
netDNS lookups, TCP checks and statistics

The full list, with the functions of each one, is in Starlark.

engine: starlark
key: |
  for existing in sources[0]:
      if existing["metadata"]["name"] == object["metadata"]["name"]:
          continue
      if existing["spec"]["hostnames"] == object["spec"]["hostnames"]:
          vars["collision"] = existing["metadata"]["name"]
          print("collision")
          break
  else:
      print("clear")

Go templates

Go templates with the Sprig functions. When the output is a document, the template is the document, which is why it is the usual choice for a manifest. It reads everything and leaves values with setVar.

engine: gotmpl
template: |
  apiVersion: v1
  kind: ConfigMap
  metadata:
    name: platform
    namespace: {{ .object.metadata.name }}

Plain

Nothing to evaluate. The text is the value.

engine: plain
template: "configmaps are not allowed here"

Plain + CEL

Plain text with holes. A YAML patch that needs one or two values from the object stays readable, and the holes are CEL expressions, with the same limits as CEL.

engine: plain+cel
template: |
  metadata:
    labels:
      namespace: "{{cel: object.metadata.name }}"

At a glance

EngineWhere it winsWhat it cannot do
celShort rules over the object or a list, with all and existsNo loops, no access to vars
starlarkRules that are a program: loops, helper functions, building a patchMore lines to read for a rule that fits in one
gotmplDocuments: a manifest, or a message built from many fieldsLogic turns into string juggling
plainText that never changesNo access to the context
plain+celA readable patch that needs one or two values from the objectOnly expressions inside the holes

The context

Before an engine runs, Admitik builds one object with everything the template may need and hands it over as top level names. It is the same for all of them; what changes is how each engine reads it.

NameContent
operationCREATE, UPDATE or DELETE, normalized
objectThe object being admitted, or the one that triggered the event
oldObjectThe previous state, on updates. Empty otherwise
sourcesThe objects each source returned, by position
varsValues a policy writes for its own later conditions and templates

Who sees what

EngineSees the contextSees varsWrites vars
CELyesnono
Starlarkyesyesyes
Go templatesyesyesyes, with setVar
Plainnonono
Plain + CELthrough its holesnono

A CEL expression never sees vars, on purpose: what it produces cannot depend on what another template wrote. When a rule needs a value from an earlier step, write it in Starlark or in a Go template.

vars is the only part of the context that changes during an evaluation. It starts empty on every object.

Writing and reading values

With Starlark

vars is a dictionary. Write it with a key, read it with the same key:

conditions:
  - name: compute-the-ceiling
    engine: starlark
    key: |
      vars["ceiling"] = 10
      print("computed")
    value: computed

  - name: is-it-over
    engine: starlark
    key: |
      if object["spec"]["replicas"] > vars["ceiling"]:
          print("over")
      else:
          print("under")
    value: over

The first condition leaves 10 in vars, and the second one reads it. If the key may not be there, ask first:

if "ceiling" in vars:
    print(vars["ceiling"])

With Go templates

setVar takes a key and a value, writes it and returns an empty string, so it goes wherever the template starts. The value is read with .vars.<key>:

conditions:
  - name: remember-the-team
    engine: gotmpl
    key: '{{ setVar "team" .object.metadata.labels.team }}computed'
    value: computed

patch:
  type: jsonmerge
  engine: gotmpl
  template: |
    metadata:
      labels:
        team: {{ .vars.team }}

The condition produces the text computed, because setVar returns nothing, and leaves the team in vars. The patch reads it later and writes it into a label.

Mixing them

Both engines read what the other wrote. A value computed in Starlark is .vars.ceiling in a Go template, and a value set with setVar is vars["team"] in Starlark:

conditions:
  - name: compute
    engine: starlark
    key: |
      vars["ceiling"] = int(object["metadata"]["labels"]["max-replicas"])
      print("computed")
    value: computed

patch:
  type: jsonmerge
  engine: gotmpl
  template: |
    spec:
      replicas: {{ .vars.ceiling }}