Getting started with Kyverno + CUE
The CUE Central Registry provides a well-known location for well-known schemas, including those for the different policy types enforced by Kyverno. This guide shows you how to get started defining your Kyverno policies in CUE using a curated module from the schema library.
Initialize your local CUE module
CUE that uses schemas and modules from the Central Registry needs to exist within its own CUE module.
$ cue mod init cue.exampleYou can choose any module name you like, and change it later.
Create a policy
Declare a Kyverno policy in CUE. This one is based on an example from Kyverno’s
policies
repository, but you should use policies that are more relevant to your
situation.
package cicd
import "cue.dev/x/kyverno/clusterpolicy/v1"
clusterPolicies: createAuthorizationPolicy: v1.#ClusterPolicy & {
apiVersion: "kyverno.io/v1"
kind: "ClusterPolicy"
metadata: {
name: "create-authorizationpolicy"
annotations: {
"policies.kyverno.io/title": "Create Istio Deny AuthorizationPolicy"
"policies.kyverno.io/category": "Istio"
"policies.kyverno.io/subject": "AuthorizationPolicy"
"policies.kyverno.io/description": "This policy creates a default deny AuthorizationPolicy for all new Namespaces, and should be supplemented with additional AuthorizationPolicies to allow traffic as required"
}
}
spec: rules: [{
name: "generate-deny-authorizationpolicy"
match: any: [{
resources: kinds: ["Namespace"]
}]
generate: {
apiVersion: "security.istio.io/v1beta1"
kind: "AuthorizationPolicy"
name: "default-deny"
namespace: "{{request.object.metadata.name}}"
synchronize: true
data: spec: {}
}
}]
}
In later guides we’ll add more entries to the clusterPolicies struct.
The import at the top references the appropriate curated module for the policy.
Its path is currently temporary, but only while its proper location is being decided.
The temporary path isn’t a problem: once a schema is published to the Central Registry, it is always available at that location.
When versions are published at the curated module’s final location, use the cue refactor imports command to update your CUE.
Tidy your local CUE module
$ cue mod tidyTidying a module is part of using curated modules from the
Central Registry.
Always use
cue mod tidy
when you use a curated module for the first time.
If you see an error message mentioning “too many requests” then login to the Central Registry and re-run this command. The Central Registry allows more requests from authenticated users.
Validate your policy’s structure
$ cue vet -c ./kyverno-policiesBecause cue vet doesn’t display any errors,
you know that the curated schema has validated your policy’s structure.
Export your policy as YAML
$ cd kyverno-policies
$ cue export --outfile create-authorizationpolicy.yml -e clusterPolicies.createAuthorizationPolicyIf you chose to export the example policy shown above, your validated YAML policy will look like this:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: create-authorizationpolicy
annotations:
policies.kyverno.io/title: Create Istio Deny AuthorizationPolicy
policies.kyverno.io/category: Istio
policies.kyverno.io/subject: AuthorizationPolicy
policies.kyverno.io/description: This policy creates a default deny AuthorizationPolicy for all new Namespaces, and should be supplemented with additional AuthorizationPolicies to allow traffic as required
spec:
rules:
- generate:
apiVersion: security.istio.io/v1beta1
data:
spec: {}
kind: AuthorizationPolicy
name: default-deny
namespace: '{{request.object.metadata.name}}'
synchronize: true
match:
any:
- resources:
kinds:
- Namespace
name: generate-deny-authorizationpolicyUse your policy
The kyverno-policies directory should be stored in your version control
system. After recording the state of the directory and its files,
apply the policy to a Kubernetes cluster that’s running Kyverno using the
kyverno apply
command with the generated YAML file.
Whenever you update your CUE policy, re-run the cue export command shown
above, record any changes to your files, and apply the validated policy to your
cluster.