jp

The Kyverno CLI has a jp subcommand which makes it possible to test not only the custom filters endemic to Kyverno but also the full array of capabilities of JMESPath included in the jp tool itself here. By passing in either through stdin or a file, both for input JSON or YAML documents and expressions, the jp subcommand will evaluate any JMESPath expression and supply the output.

Examples:

List available Kyverno custom JMESPath filters. Please refer to the JMESPath documentation page here for extensive details on each custom filter. Note this does not show the built-in JMESPath filters available upstream, only the custom Kyverno filters.

 1$ kyverno jp function
 2Name: add
 3  Signature: add(any, any) any
 4  Note:      does arithmetic addition of two specified values of numbers, quantities, and durations
 5
 6Name: base64_decode
 7  Signature: base64_decode(string) string
 8  Note:      decodes a base 64 string
 9
10Name: base64_encode
11  Signature: base64_encode(string) string
12  Note:      encodes a regular, plaintext and unencoded string to base64
13
14Name: compare
15  Signature: compare(string, string) number
16  Note:      compares two strings lexicographically
17<snip>

Test a custom JMESPath filter using stdin inputs.

1$ echo '{"foo": "BAR"}' | kyverno jp query 'to_lower(foo)'
2Reading from terminal input.
3Enter input object and hit Ctrl+D.
4# to_lower(foo)
5"bar"

Test a custom JMESPath filter using an input JSON file. YAML files are also supported.

 1$ cat foo.json
 2{"bar": "this-is-a-dashed-string"}
 3
 4$ kyverno jp query -i foo.json "split(bar, '-')"
 5# split(bar, '-')
 6[
 7  "this",
 8  "is",
 9  "a",
10  "dashed",
11  "string"
12]

Test a custom JMESPath filter as well as an upstream JMESPath filter.

1$ kyverno jp query -i foo.json "split(bar, '-') | length(@)"
2# split(bar, '-') | length(@)
35

Test a custom JMESPath filter using an expression from a file.

1$ cat add
2add(`1`,`2`)
3
4$ echo {} | kyverno jp query -q add
5Reading from terminal input.
6Enter input object and hit Ctrl+D.
7# add(`1`,`2`)
83

Test upstream JMESPath functionality using an input JSON file and show cleaned output.

 1$ cat pod.json
 2{
 3  "apiVersion": "v1",
 4  "kind": "Pod",
 5  "metadata": {
 6    "name": "mypod",
 7    "namespace": "foo"
 8  },
 9  "spec": {
10    "containers": [
11      {
12        "name": "busybox",
13        "image": "busybox"
14      }
15    ]
16  }
17}
18
19$ kyverno jp query -i pod.json 'spec.containers[0].name' -u
20# spec.containers[0].name
21busybox

Parse a JMESPath expression and show the corresponding AST to see how it was interpreted.

 1$ kyverno jp parse 'request.object.metadata.name | truncate(@, `9`)'
 2# request.object.metadata.name | truncate(@, `9`)
 3ASTPipe {
 4  children: {
 5    ASTSubexpression {
 6      children: {
 7        ASTSubexpression {
 8          children: {
 9            ASTSubexpression {
10              children: {
11                ASTField {
12                  value: "request"
13                }
14                ASTField {
15                  value: "object"
16                }
17            }
18            ASTField {
19              value: "metadata"
20            }
21        }
22        ASTField {
23          value: "name"
24        }
25    }
26    ASTFunctionExpression {
27      value: "truncate"
28      children: {
29        ASTCurrentNode {
30        }
31        ASTLiteral {
32          value: 9
33        }
34    }
35}

For more specific information on writing JMESPath for use in Kyverno, see the JMESPath page.


Last modified March 12, 2024 at 3:14 PM PST: refactor: kyverno cli section (#1180) (f7a0553)