Applying Validating Admission Policies using Kyverno CLI
The Kyverno Command Line Interface (CLI) allows applying policies outside of Kubernetes clusters and can validate and test policy behavior prior to adding them to a cluster.
The two commands used for testing are apply and test:
- The
applycommand is used to perform a dry run on one or more policies for the given manifest(s). - The
testcommand is used to test a given set of resources against one or more policies to check the desired results defined in a special test manifest.
In this post, I will show you how you can apply/test Kubernetes ValidatingAdmissionPolicies that were first introduced in 1.26 with the enhancements to the Kyverno CLI in v1.11.
Applying ValidatingAdmissionPolicies using kyverno apply
Section titled “Applying ValidatingAdmissionPolicies using kyverno apply”In this section, you will create a ValidatingAdmissionPolicy that checks the number of Deployment replicas. You will then apply this policy to two Deployments, one of which violates the policy:
cat << EOF > check-deployment-replicas.yamlapiVersion: admissionregistration.k8s.io/v1alpha1kind: ValidatingAdmissionPolicymetadata: name: check-deployments-replicasspec: failurePolicy: Fail matchConstraints: resourceRules: - apiGroups: ["apps"] apiVersions: ["v1"] operations: ["CREATE", "UPDATE"] resources: ["deployments"] validations: - expression: "object.spec.replicas <= 2" message: "Replicas must be less than or equal 2"EOFThe following deployment satisfies the rules declared in the above policy.
cat << EOF > deployment-pass.yamlapiVersion: apps/v1kind: Deploymentmetadata: name: nginx-passspec: replicas: 2 selector: matchLabels: app: nginx-pass template: metadata: labels: app: nginx-pass spec: containers: - name: nginx-server image: nginxEOFLet’s apply the policy to the resource using kyverno apply as follows.
kyverno apply ./check-deployment-replicas.yaml --resource deployment-pass.yamlThe output should be the following.
Applying 1 policy rule(s) to 1 resource(s)...
pass: 1, fail: 0, warn: 0, error: 0, skip: 0Let’s try to create another deployment that violates the policy.
cat << EOF > deployment-fail.yamlapiVersion: apps/v1kind: Deploymentmetadata: name: nginx-failspec: replicas: 3 selector: matchLabels: app: nginx-fail template: metadata: labels: app: nginx-fail spec: containers: - name: nginx-server image: nginxEOFThen apply the policy to the resource as follows.
kyverno apply ./check-deployment-replicas.yaml --resource deployment-fail.yamlThe output should be as shown.
Applying 1 policy rule(s) to 1 resource(s)...
pass: 0, fail: 1, warn: 0, error: 0, skip: 0Testing ValidatingAdmissionPolicies using kyverno test
Section titled “Testing ValidatingAdmissionPolicies using kyverno test”In this section, you will create a ValidatingAdmissionPolicy that ensures no hostPath volumes are in use for Deployments. You will then create two Deployments to test them against the policy and check the desired results.
To proceed, you need to create a directory containing the necessary manifests. In this example, I created a directory called test-dir.
Let’s start with creating the policy.
cat << EOF > ./test-dir/disallow-host-path.yamlapiVersion: admissionregistration.k8s.io/v1alpha1kind: ValidatingAdmissionPolicymetadata: name: disallow-host-pathspec: failurePolicy: Fail matchConstraints: resourceRules: - apiGroups: ["apps"] apiVersions: ["v1"] operations: ["CREATE", "UPDATE"] resources: ["deployments"] validations: - expression: "!has(object.spec.template.spec.volumes) || object.spec.template.spec.volumes.all(volume, !has(volume.hostPath))" message: "HostPath volumes are forbidden. The field spec.template.spec.volumes[*].hostPath must be unset."EOFThen, create the two Deployments, one of which violates the policy.
cat << EOF > ./test-dir/deployments.yamlapiVersion: apps/v1kind: Deploymentmetadata: name: deployment-passspec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx-server image: nginx volumeMounts: - name: temp mountPath: /scratch volumes: - name: temp emptyDir: {}---apiVersion: apps/v1kind: Deploymentmetadata: name: deployment-failspec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx-server image: nginx volumeMounts: - name: udev mountPath: /data volumes: - name: udev hostPath: path: /etc/udevEOFThe tests are defined in a file named kyverno-test.yaml so you will create two tests, one for each Deployment and test them against the policy. Notice the use of a new field in the test manifest called isValidatingAdmissionPolicy.
cat << EOF > ./test-dir/kyverno-test.yamlname: disallow-host-path-testpolicies: - disallow-host-path.yamlresources: - deployments.yamlresults: - policy: disallow-host-path resource: deployment-pass isValidatingAdmissionPolicy: true kind: Deployment result: pass - policy: disallow-host-path resource: deployment-fail isValidatingAdmissionPolicy: true kind: Deployment result: failEOFNow, we’re ready to test the two Deployments against a ValidatingAdmissionPolicy.
kyverno test ./test-dirThe output should be as shown below.
Executing disallow-host-path-test...
│────│────────────────────│──────│────────────────────────────│────────│────────││ ID │ POLICY │ RULE │ RESOURCE │ RESULT │ REASON ││────│────────────────────│──────│────────────────────────────│────────│────────││ 1 │ disallow-host-path │ │ Deployment/deployment-pass │ Pass │ Ok ││ 2 │ disallow-host-path │ │ Deployment/deployment-fail │ Pass │ Ok ││────│────────────────────│──────│────────────────────────────│────────│────────│
Test Summary: 2 tests passed and 0 tests failedAs expected, the two tests passed because the actual result of each test matches the desired result as defined in the test manifest.
Conclusion
Section titled “Conclusion”This blog post explains how to apply ValidatingAdmissionPolicies to resources using the Kyverno CLI. With Kyverno, it’s easy to apply Kubernetes ValidatingAdmissionPolicies in your CI/CD pipelines and to test new ValidatingAdmissionPolicies before they are deployed to your clusters. This is one of many exciting features coming with Kyverno v1.11.