Back to Policies

Kubernetes Version Check

It is often needed to make decisions for resources based upon the version of the Kubernetes API server in the cluster. This policy serves as an example for how to retrieve the minor version of the Kubernetes API server and subsequently use in a policy behavior. It will mutate a Secret upon its creation with a label called `apiminorversion` the value of which is the minor version of the API server.

View on GitHub

Policy Definition

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: kubernetes-version-check
annotations:
policies.kyverno.io/title: Kubernetes Version Check
policies.kyverno.io/category: Other
policies.kyverno.io/severity: medium
policies.kyverno.io/subject: Secret
kyverno.io/kyverno-version: 1.8.0-rc2
policies.kyverno.io/minversion: 1.8.0
kyverno.io/kubernetes-version: "1.24"
policies.kyverno.io/description: It is often needed to make decisions for resources based upon the version of the Kubernetes API server in the cluster. This policy serves as an example for how to retrieve the minor version of the Kubernetes API server and subsequently use in a policy behavior. It will mutate a Secret upon its creation with a label called `apiminorversion` the value of which is the minor version of the API server.
spec:
rules:
- name: test-ver-ver
match:
any:
- resources:
kinds:
- Secret
preconditions:
all:
- key: "{{request.operation || 'BACKGROUND'}}"
operator: Equals
value: CREATE
context:
- name: minorversion
apiCall:
urlPath: /version
jmesPath: minor
mutate:
patchStrategicMerge:
metadata:
labels:
apiminorversion: "{{minorversion}}"

Related Policies