|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +title: "Kubernetes 1.26: Introducing Validating Admission Policies" |
| 4 | +date: 2022-12-20 |
| 5 | +slug: validating-admission-policies-alpha |
| 6 | +--- |
| 7 | + |
| 8 | +**Authors:** Joe Betz (Google), Cici Huang (Google) |
| 9 | + |
| 10 | +In Kubernetes 1.26, the 1st alpha release of validating admission policies is |
| 11 | +available! |
| 12 | + |
| 13 | +Validating admission policies use the [Common Expression |
| 14 | +Language](https://github.com/google/cel-spec) (CEL) to offer a declarative, |
| 15 | +in-process alternative to [validating admission |
| 16 | +webhooks](/docs/reference/access-authn-authz/extensible-admission-controllers/#what-are-admission-webhooks). |
| 17 | + |
| 18 | +CEL was first introduced to Kubernetes for the [Validation rules for |
| 19 | +CustomResourceDefinitions](/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation-rules). |
| 20 | +This enhancement expands the use of CEL in Kubernetes to support a far wider |
| 21 | +range of admission use cases. |
| 22 | + |
| 23 | +Admission webhooks can be burdensome to develop and operate. Webhook developers |
| 24 | +must implement and maintain a webhook binary to handle admission requests. Also, |
| 25 | +admission webhooks are complex to operate. Each webhook must be deployed, |
| 26 | +monitored and have a well defined upgrade and rollback plan. To make matters |
| 27 | +worse, if a webhook times out or becomes unavailable, the Kubernetes control |
| 28 | +plane can become unavailable. This enhancement avoids much of this complexity of |
| 29 | +admission webhooks by embedding CEL expressions into Kubernetes resources |
| 30 | +instead of calling out to a remote webhook binary. |
| 31 | + |
| 32 | +For example, to set a limit on how many replicas a Deployment can have. |
| 33 | +Start by defining a validation policy: |
| 34 | + |
| 35 | +```yaml |
| 36 | +apiVersion: admissionregistration.k8s.io/v1alpha1 |
| 37 | +kind: ValidatingAdmissionPolicy |
| 38 | +metadata: |
| 39 | + name: "demo-policy.example.com" |
| 40 | +spec: |
| 41 | + matchConstraints: |
| 42 | + resourceRules: |
| 43 | + - apiGroups: ["apps"] |
| 44 | + apiVersions: ["v1"] |
| 45 | + operations: ["CREATE", "UPDATE"] |
| 46 | + resources: ["deployments"] |
| 47 | + validations: |
| 48 | + - expression: "object.spec.replicas <= 5" |
| 49 | +``` |
| 50 | +
|
| 51 | +The `expression` field contains the CEL expression that is used to validate |
| 52 | +admission requests. `matchConstraints` declares what types of requests this |
| 53 | +`ValidatingAdmissionPolicy` is may validate. |
| 54 | + |
| 55 | +Next bind the policy to the appropriate resources: |
| 56 | + |
| 57 | +```yaml |
| 58 | +apiVersion: admissionregistration.k8s.io/v1alpha1 |
| 59 | +kind: ValidatingAdmissionPolicyBinding |
| 60 | +metadata: |
| 61 | + name: "demo-binding-test.example.com" |
| 62 | +spec: |
| 63 | + policy: "demo-policy.example.com" |
| 64 | + matchResources: |
| 65 | + namespaceSelector: |
| 66 | + - key: environment, |
| 67 | + operator: In, |
| 68 | + values: ["test"] |
| 69 | +``` |
| 70 | + |
| 71 | +This `ValidatingAdmissionPolicyBinding` resource binds the above policy only to |
| 72 | +namespaces where the `environment` label is set to `test`. Once this binding |
| 73 | +is created, the kube-apiserver will begin enforcing this admission policy. |
| 74 | + |
| 75 | +To emphasize how much simpler this approach is than admission webhooks, if this example |
| 76 | +were instead implemented with a webhook, an entire binary would need to be |
| 77 | +developed and maintained just to perform a `<=` check. In our review of a wide |
| 78 | +range of admission webhooks used in production, the vast majority performed |
| 79 | +relatively simple checks, all of which can easily be expressed using CEL. |
| 80 | + |
| 81 | +Validation admission policies are highly configurable, enabling policy authors |
| 82 | +to define policies that can be parameterized and scoped to resources as needed |
| 83 | +by cluster administrators. |
| 84 | + |
| 85 | +For example, the above admission policy can be modified to make it configurable: |
| 86 | + |
| 87 | +```yaml |
| 88 | +apiVersion: admissionregistration.k8s.io/v1alpha1 |
| 89 | +kind: ValidatingAdmissionPolicy |
| 90 | +metadata: |
| 91 | + name: "demo-policy.example.com" |
| 92 | +Spec: |
| 93 | + paramKind: |
| 94 | + apiVersion: rules.example.com/v1 # You also need a CustomResourceDefinition for this API |
| 95 | + kind: ReplicaLimit |
| 96 | + matchConstraints: |
| 97 | + resourceRules: |
| 98 | + - apiGroups: ["apps"] |
| 99 | + apiVersions: ["v1"] |
| 100 | + operations: ["CREATE", "UPDATE"] |
| 101 | + resources: ["deployments"] |
| 102 | + validations: |
| 103 | + - expression: "object.spec.replicas <= params.maxReplicas" |
| 104 | +``` |
| 105 | + |
| 106 | +Here, `paramKind` defines the resources used to configure the policy and the |
| 107 | +`expression` uses the `params` variable to access the parameter resource. |
| 108 | + |
| 109 | +This allows multiple bindings to be defined, each configured differently. For |
| 110 | +example: |
| 111 | + |
| 112 | +```yaml |
| 113 | +apiVersion: admissionregistration.k8s.io/v1alpha1 |
| 114 | +kind: ValidatingAdmissionPolicyBinding |
| 115 | +metadata: |
| 116 | + name: "demo-binding-production.example.com" |
| 117 | +spec: |
| 118 | + policy: "demo-policy.example.com" |
| 119 | + paramsRef: |
| 120 | + name: "demo-params-production.example.com" |
| 121 | + matchResources: |
| 122 | + namespaceSelector: |
| 123 | + - key: environment, |
| 124 | + operator: In, |
| 125 | + values: ["production"] |
| 126 | +``` |
| 127 | + |
| 128 | +```yaml |
| 129 | +apiVersion: rules.example.com/v1 # defined via a CustomResourceDefinition |
| 130 | +kind: ReplicaLimit |
| 131 | +metadata: |
| 132 | + name: "demo-params-production.example.com" |
| 133 | +maxReplicas: 1000 |
| 134 | +``` |
| 135 | + |
| 136 | +This binding and parameter resource pair limit deployments in namespaces with the |
| 137 | +`environment` label set to `production` to a max of 1000 replicas. |
| 138 | + |
| 139 | +You can then use a separate binding and parameter pair to set a different limit |
| 140 | +for namespaces in the `test` environment. |
| 141 | + |
| 142 | +I hope this has given you a glimpse of what is possible with validating |
| 143 | +admission policies! There are many features that we have not yet touched on. |
| 144 | + |
| 145 | +To learn more, read |
| 146 | +[Validating Admission Policy](/docs/reference/access-authn-authz/validating-admission-policy/). |
| 147 | + |
| 148 | +We are working hard to add more features to admission policies and make the |
| 149 | +enhancement easier to use. Try it out, send us your feedback and help us build |
| 150 | +a simpler alternative to admission webhooks! |
| 151 | + |
| 152 | +## How do I get involved? |
| 153 | + |
| 154 | +If you want to get involved in development of admission policies, discuss enhancement |
| 155 | +roadmaps, or report a bug, you can get in touch with developers at |
| 156 | +[SIG API Machinery](https://github.com/kubernetes/community/tree/master/sig-api-machinery). |
0 commit comments