kubernetes / kubernetes

Production-Grade Container Scheduling and Management
https://kubernetes.io
Apache License 2.0
111.12k stars 39.68k forks source link

CEL Validation: permit hashing object fields to form object name #113220

Open lavalamp opened 2 years ago

lavalamp commented 2 years ago

Scenario: My CRD has fields x, y, and z. I want to ensure that within a given namespace, no two instances of this CRD have the same set of values for those fields.

Solution 1: Require that .metadata.name be x+y+z. (Add a x-kubernetes-validations to the root of the object, and do something like "[self.metadata.name] == self.spec.x + self.spec.y + self.spec.z") Problem 1: The fields might be too long, or not match the rules about names (contain "/" characters, etc)

Solution 2: Require that name be of the form sanitized_fields(x,y,z)+unique_hash(x,y,z). Where:

The request here is to add a function or set of functions callable from CEL to do this sanitizing/hashing. Ideally we would like the code to be known ASAP so that people can use them in validating webhooks today and move to CEL when it is available.

For bonus points we would like to default the name to the correct value if it is blank, since it may be hard for clients to determine the correct value. (I suspect this isn't possible with our defaulting stack, but maybe I'm wrong.)

We need the error message to report the expected name, so that at least clients could e.g. send a dry-run POST request to figure out the correct name and then use it in the real request.

/sig api-machinery

lavalamp commented 2 years ago

cc @jpbetz @apelisse

jpbetz commented 2 years ago

This is a good motivating example for adding a hash function to the CEL library. We can add that.

The sanitation can probably be expressed as a RE2 regex replace, maybe combined with substring to cap the length?

jpbetz commented 2 years ago

/assign

alexzielenski commented 2 years ago

/triage accepted

alexzielenski commented 2 years ago

/cc @DangerOnTheRanger

k8s-triage-robot commented 10 months ago

This issue has not been updated in over 1 year, and should be re-triaged.

You can:

For more details on the triage process, see https://www.kubernetes.dev/docs/guide/issue-triage/

/remove-triage accepted

seans3 commented 9 months ago

/triage accepted