-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CRD validation doesn't accept empty values for type "object" fields #25
Comments
@pwittrock If apiVersion: stable.example.com/v1
kind: CronTab
metadata:
name: c
spec: {} /cc @sttts |
I agree with @pwittrock that the error is unexpected from a user point of view. Actually I am surprised that the given yaml translates to the null in JSON. Is this like that by definition of yaml? |
Yes, I think it is:
http://yaml.org/spec/1.2/spec.html#id2786563 This seems more like a YAML gotcha than a CRD validation gotcha. Although |
Yes, the OpenAPI spec does not accept null for One can add null to the spec though: openAPIV3Schema:
properties:
spec:
anyOf:
- type: "null"
- type: object
properties:
... |
This is not ideal but as @enisoc mentioned
Closing. Please reopen if you think this is still an issue. |
where it said spec is required?
I don't think spec is said something about this. It look like this is a validation bug. If the field is not in Analogous to that is string, if a string field is optional, null or empty string both should be accepted as not provided. |
You're right; I was conflating "required" and "can't be null". The schema doesn't say
That's how "optional" works in k8s APIs, but unfortunately OpenAPI Schema (via JSON Schema) works differently. In JSON Schema,
It is only concerned with whether or not the property set of the object being validated contains the given fields. If you pass
I agree that makes sense intuitively, but it's not how JSON Schema is defined. Notice in the data model, a
That means, according to JSON Schema, |
in any sense, I think we should support this by patching the validation library or having a layer on top of validation to set all optional |
I agree that matching the meaning of "optional" used by core APIs would be a nice UX, but it would mean we no longer match the OpenAPIV3 and JSON Schema specs, and that's a break that can't be taken lightly. @lavalamp recently proposed a policy within SIG API Machinery that, if we say we use a standard (e.g. JSON Patch), any failure to match it exactly is a bug. So at best, we would need to stop saying we use OpenAPIV3, which means changing the field name or adding a new field for |
When we were using kubebuilder, it generated openAPIV3schema validation for us in our CRD declarations. However: 1. No other knative projects use this 2. Continuing to use this is forcing us to diverge from the other Knative projects, specifically I found that `Status` fields, although optional, were being required by the validation (as described at kubernetes/apiextensions-apiserver#25), and the best way to work around it seemed to be to make them nullable (as per https://stackoverflow.com/questions/18088294/how-to-not-marshal-an-empty-struct-into-json-with-go) i.e. pointers, which is different from how Knative works, and introduces new problems (such as handling when these fields are null) 3. We (or at least @pivotal-nader-ziada and @shashwathi for sure!) were maintaining these fiels manually :O
When we were using kubebuilder, it generated openAPIV3schema validation for us in our CRD declarations. However: 1. No other knative projects use this 2. Continuing to use this is forcing us to diverge from the other Knative projects, specifically I found that `Status` fields, although optional, were being required by the validation (as described at kubernetes/apiextensions-apiserver#25), and the best way to work around it seemed to be to make them nullable (as per https://stackoverflow.com/questions/18088294/how-to-not-marshal-an-empty-struct-into-json-with-go) i.e. pointers, which is different from how Knative works, and introduces new problems (such as handling when these fields are null) 3. We (or at least @pivotal-nader-ziada and @shashwathi for sure!) were maintaining these fiels manually :O
When we were using kubebuilder, it generated openAPIV3schema validation for us in our CRD declarations. However: 1. No other knative projects use this 2. Continuing to use this is forcing us to diverge from the other Knative projects, specifically I found that `Status` fields, although optional, were being required by the validation (as described at kubernetes/apiextensions-apiserver#25), and the best way to work around it seemed to be to make them nullable (as per https://stackoverflow.com/questions/18088294/how-to-not-marshal-an-empty-struct-into-json-with-go) i.e. pointers, which is different from how Knative works, and introduces new problems (such as handling when these fields are null) 3. We (or at least @pivotal-nader-ziada and @shashwathi for sure!) were maintaining these fiels manually :O
Issues go stale after 90d of inactivity. If this issue is safe to close now please do so with Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
Stale issues rot after 30d of inactivity. If this issue is safe to close now please do so with Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
/remove-lifecycle rotten |
Are there any updates on this? We have a CRD that (for historical reasons) has one of the elements defined as either |
Issues go stale after 90d of inactivity. If this issue is safe to close now please do so with Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
Issues go stale after 90d of inactivity. If this issue is safe to close now please do so with Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
This is fixed with
is equivalent to |
spec.validation.openAPIV3Schema.properties[spec].properties[sentinel].anyOf[0].type: Unsupported value: "null": supported values: "array", "boolean", "integer", "number", "object", "string" |
|
Create example CRD, but make spec an object
Create an instance with an empty spec
This throws the following error, but shouldn't since spec doesn't have any required fields:
The text was updated successfully, but these errors were encountered: