documentation / Writing Policies / Variables
Sometimes it is necessary to vary the contents of a mutated or generated resource based on request data. To achieve this, variables can be used to reference attributes that are loaded in the rule processing context using a JMESPATH notation.
The policy engine will substitute any values with the format {{<JMESPATH>}}
with the variable value before processing the rule.
The following data is available for use in context:
- Resource:
{{request.object}}
- UserInfo:
{{request.userInfo}}
Kyverno automatically creates a few useful variables:
-
serviceAccountName
: the "userName" which is last part of a service account i.e. without the prefixsystem:serviceaccount:<namespace>:
. For example, when processing a request fromsystem:serviceaccount:nirmata:user1
Kyverno will store the valueuser1
in the variableserviceAccountName
. -
serviceAccountNamespace
: the "namespace" part of the serviceAccount. For example, when processing a request fromsystem:serviceaccount:nirmata:user1
Kyverno will storenirmata
in the variableserviceAccountNamespace
.
- Reference a resource name (type string)
{{request.object.metadata.name}}
- Build name from multiple variables (type string)
"ns-owner-{{request.object.metadata.namespace}}-{{request.userInfo.username}}-binding"
- Reference the metadata (type object)
{{request.object.metadata}}
Read Next >> Preconditions