Skip to content

Commit 0a6b3b3

Browse files
jlucktayeddycharly
andauthored
Update bindings.md with 'x_k8s_get()' example (#1915)
* Update bindings.md with 'x_k8s_get()' example Added a commented example on how to bind other objects with the 'x_k8s_get()' function, then refer and assert against. Signed-off-by: James Lucktaylor <[email protected]> * docs: move x_k8s_get example to its own page Signed-off-by: James Lucktaylor <[email protected]> * restructure Signed-off-by: Charles-Edouard Brétéché <[email protected]> --------- Signed-off-by: James Lucktaylor <[email protected]> Signed-off-by: James Lucktaylor <[email protected]> Signed-off-by: Charles-Edouard Brétéché <[email protected]> Co-authored-by: Charles-Edouard Brétéché <[email protected]>
1 parent 0072754 commit 0a6b3b3

File tree

5 files changed

+75
-2
lines changed

5 files changed

+75
-2
lines changed

Makefile

+1-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ codegen-cli-docs: ## Generate CLI docs
146146
codegen-cli-docs: build
147147
@echo Generate cli docs... >&2
148148
@rm -rf website/docs/reference/commands && mkdir -p website/reference/docs/commands
149-
@rm -rf docs/user/commands && mkdir -p docs/user/commands
150149
@./$(CLI_BIN) docs -o website/docs/reference/commands --autogenTag=false
151150

152151
.PHONY: codegen-api-docs
@@ -162,7 +161,7 @@ codegen-api-docs: codegen-conversion
162161
.PHONY: codegen-jp-docs
163162
codegen-jp-docs: ## Generate JP docs
164163
@echo Generate jp docs... >&2
165-
@rm -rf ./website/docs/reference/jp && mkdir -p ./website/docs/reference/jp
164+
@mkdir -p ./website/docs/reference/jp
166165
@go run ./website/jp/main.go > ./website/docs/reference/jp/functions.md
167166

168167
.PHONY: codegen-mkdocs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# `x_k8s_get`
2+
3+
```yaml
4+
- description: >-
5+
1. Look up some details about a Deployment object with 'x_k8s_get(...)', and store them in a binding.
6+
2. Make use of the binding in a `script` block through environment variables.
7+
3. Refer to the same bound Deployment object in an `assert` block.
8+
bindings:
9+
- name: xpNS
10+
value: crossplane-system
11+
- name: xpDeploy
12+
# Arguments to 'x_k8s_get(any, string, string, string, string)':
13+
#
14+
# 1. any: Supply the '$client' built-in binding here, which is needed to connect to the cluster:
15+
# https://kyverno.github.io/chainsaw/latest/reference/builtins/
16+
#
17+
# 2. string: 'apiVersion' field on the object.
18+
#
19+
# 3. string: 'Kind' field on the object.
20+
#
21+
# 4. string: The namespace of the object. If the object type is not namespaced, this field can be an empty
22+
# string, or any string; it doesn't seem to matter.
23+
#
24+
# 5. string: The name of the object.
25+
value: (x_k8s_get($client, 'apps/v1', 'Deployment', $xpNS, 'crossplane'))
26+
try:
27+
- script:
28+
bindings:
29+
- # Re-bind the version label from the Deployment to give us a more succinct name to refer to.
30+
name: deployVersion
31+
# If the label key has any periods in it, double-quote the whole key.
32+
value: ($xpDeploy.metadata.labels."app.kubernetes.io/version")
33+
env:
34+
- # Refer to the Deployment version label the long way.
35+
name: DEP_VER_LONG
36+
value: ($xpDeploy.metadata.labels."app.kubernetes.io/version")
37+
- # Refer to the Deployment version label the short way, through the additional binding scoped to this
38+
# 'script' block.
39+
name: DEP_VER_SHORT
40+
value: ($deployVersion)
41+
# The version values printed by the script here will be the same, even though they were derived through
42+
# slightly different routes.
43+
content: |-
44+
echo "DEP_VER_LONG: '$DEP_VER_LONG'"
45+
echo "DEP_VER_SHORT: '$DEP_VER_SHORT'"
46+
- assert:
47+
bindings:
48+
- name: depVer
49+
value: ($xpDeploy.metadata.labels."app.kubernetes.io/version")
50+
resource:
51+
apiVersion: v1
52+
kind: Pod
53+
metadata:
54+
# Match the pod(s) with namespace and label selectors.
55+
namespace: ($xpNS)
56+
labels:
57+
app: crossplane
58+
# Make a binding that holds this Pod's version from its label.
59+
(metadata.labels."app.kubernetes.io/version")->podVer:
60+
# Assert that the version labels for the Deployment and this Pod equal each other, using the
61+
# 'semver_compare()' function described here:
62+
# https://kyverno.io/docs/writing-policies/jmespath/#semver_compare
63+
(semver_compare($depVer, $podVer)): true
64+
```

website/docs/reference/jp/functions.md

+4
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,7 @@
126126
| trim_space | `trim_space(string)` |
127127
| as_string | `as_string(any)` |
128128

129+
## examples
130+
131+
- [x_k8s_get](./examples/x_k8s_get.md)
132+

website/jp/main.go

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ func main() {
3535
fmt.Println()
3636
printFunctions(chainsawfunctions.GetFunctions()...)
3737
fmt.Println()
38+
fmt.Println("## examples")
39+
fmt.Println()
40+
fmt.Println("- [x_k8s_get](./examples/x_k8s_get.md)")
41+
fmt.Println()
3842
}
3943

4044
func printFunctions(funcs ...jpfunctions.FunctionEntry) {

website/mkdocs.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ nav:
8686
- v1alpha2: reference/apis/chainsaw.v1alpha2.md
8787
- JMESPath:
8888
- Functions: reference/jp/functions.md
89+
- Examples:
90+
- reference/jp/examples/x_k8s_get.md
8991
- Command Line:
9092
- chainsaw: reference/commands/chainsaw.md
9193
- chainsaw assert: reference/commands/chainsaw_assert.md

0 commit comments

Comments
 (0)