Skip to content
Yuvraj 🧢
Github - yindiaGithub - tqindiathesaas.companytfstudo.devContact

Generate polices ! Make Kubernetes life hassle free with Kyverno - Part 2

— kubernetes, kyverno — 3 min read

In the last blog post, we discussed the basics of a Kyverno generate policy and synchronization feature for managing resources across namespaces. The last demo was limited because at that time Kyverno only supported the create resource trigger for generate policies.

last Friday Kyverno roll out a new release with some exciting features. Let's explore generate policy with some use cases. We will also discuss the best practices before upgrading. Don't worry this release is backward compatible


Kyverno generates resources based on a given set of conditions or triggers (read more about match/exclude). A Kyverno generate policy supports two types of rules. In my last blog I described these two types in detail Generate polices ! Make Kubernetes life hassle free with Kyverno

With the new release users can now users can add selector to policy for creating a trigger.

In our first use case, we will generate resources based on adding a namespace label.

As an example, we will use Velero which is a tool from VMware for managing backups of Kubernetes configuration and state. Take a look at this TGIK session on Velero - it's an interesting tool

Here we will create a trigger on namespace labels. The namespace owner owner can enable or disable Velero backup by adding or removing the label i.e. nirmata.io/auto-backup: enabled

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: add-velero-autobackup-policy
spec:
background: false
rules:
- generate:
apiVersion: velero.io/v1
data:
metadata:
labels:
nirmata.io/backup.type: auto
nirmata.io/namespace: "{{request.object.metadata.name}}"
spec:
schedule: 0 0 * * *
template:
includedNamespaces:
- "{{request.object.metadata.name}}"
snapshotVolumes: true
storageLocation: default
ttl: 168h0m0s
kind: Schedule
name: "{{request.object.metadata.name}}-auto-schedule"
namespace: velero
synchronize: true
match:
resources:
kinds:
- Namespace
selector:
matchLabels:
nirmata.io/auto-backup: enabled
name: add-velero-autobackup-policy
validationFailureAction: audit

As another example we can write a policy that enable Prometheus, a CNCF metrics and alerting tool, for a namespace. Here we will create a trigger on namespace labels. The namespace owner request a service account for Prometheus by adding or removing the label i.e. nirmata.io/monitoring: enabled

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: add-serviceaccount
spec:
background: false
rules:
- match:
resources:
kinds:
- Namespace
selector:
matchLabels:
nirmata.io/monitoring: enabled
name: add-sa-policy
validationFailureAction: audit
generate:
apiVersion: v1
kind: ServiceAccount
name: "prometheus-alertmanager"
namespace: "{{request.object.metadata.name}}"
synchronize: true
data:
metadata:
labels:
app: "prometheus-alertmanager"
heritage: "Tiller"
app.kubernetes.io/name: "prometheus"
chart: "prometheus-operator-9.3.0"
release: "prometheus"

Let's see a demo, but before demo let's provide Kyverno specific permissions for both use cases. Also create a namespace velero because our policy will create schedule in velero.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: kyverno:generatecontroller
rules:
# process generate rules to generate resources
- apiGroups:
- "*"
resources:
- namespaces
- serviceaccounts
- schedules
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
# dynamic watches on trigger resources for generate rules
# re-evaluate the policy if the resource is updated
- apiGroups:
- "*"
resources:
- namespaces
verbs:
- watch

Let's see in action

kyverno

Note : we advise you to use a generate policy with a selector. If you are using generate policy without selector then it will work fine because we have backward compatibility. we will deprecate older functionality in the next major release.


Check out the Kyverno v1.1.12 GitHub page to learn more about the new feature added in Kyverno like Kustomize support for patches,Namespaced policies and some cli improvment for local testing. Find more about Kyverno in the previous post Kubernetes Policy Management with Kyverno..

© 2024 by Yuvraj 🧢. All rights reserved.
Theme by LekoArts