Skip to content
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

Resource Claims must be a map type, not set #114585

Merged

Conversation

JoelSpeed
Copy link
Contributor

@JoelSpeed JoelSpeed commented Dec 19, 2022

What type of PR is this?

/kind bug
/kind api-change

What this PR does / why we need it:

This PR adds the structType to the ResourceClaim struct within the corev1 API group.

When the structType is not set, it defaults to granular (out of a choice of granular or atomic).
Granular structs are not compatible with being used in a listType=set slice.
This type was introduced for the Claims member of ResourceRequirements which has the listType=set semantic.

Without this fix, CRD schemas generated, including the ResourceRequirements struct are not valid and produce an error when installed into a cluster.

Which issue(s) this PR fixes:

Currently, if you import the corev1.ResourceRequirements type into a custom resource definition, the generator generates an invalid schema, which results in errors such as below when you attempt to create the CRD.

unable to create CRD instances: unable to create CRD \"configs.imageregistry.operator.openshift.io\": CustomResourceDefinition.apiextensions.k8s.io \"configs.imageregistry.operator.openshift.io\" is invalid: spec.validation.openAPIV3Schema.properties[spec].properties[resources].properties[claims].items.x-kubernetes-map-type: Invalid value: \"null\": must be atomic as item of a list with x-kubernetes-list-type=set

Special notes for your reviewer:

The validation error we are hitting is this piece of code.

It was introduced by @sttts in e4e1c2c, and we are specifically hitting the object case here. Looking at the PR, seems @apelisse may be the expert on why this validation was introduced and may be able to help ascertain that this is the correct fix.

This validation sets a requirement that any struct used as the underlying type of a slice, must be an atomic struct when the slice itself is a set.

The ResourceRequirements was recently updated to add the new field Claims in #111023 which then introduced this bug. CC @pohly. Tthis is a very obscure bug and I have no idea how anyone would know to set the struct tag correctly.

I noticed while search k/k that there are many instances of this throughout the codebase where we have invalid combinations of listType=set and no structType set. We may want to fix the other instances too.

CC @deads2k

Does this PR introduce a user-facing change?

Fixes a 1.26 regression in the published openapi of Pod types caused by an incorrect list-type of the alpha resourceClaims field. This fix modifies that field list type from "set" to "map", resolving an incompatibility with use of this schema in CustomResourceDefinitions and with server-side apply.

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/bug Categorizes issue or PR as related to a bug. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Dec 19, 2022
@pohly
Copy link
Contributor

pohly commented Dec 19, 2022

Thanks for tracking this down. I have no idea what +structType=atomic does and thus need to rely on the experts to review this change.

What worries me is that this wasn't detected by any check in Kubernetes itself. If this annotation is required, shouldn't it be checked for in Kubernetes?

@k8s-triage-robot
Copy link

This PR may require API review.

If so, when the changes are ready, complete the pre-review checklist and request an API review.

Status of requested reviews is tracked in the API Review project.

@@ -2328,6 +2328,9 @@ type ResourceRequirements struct {
}

// ResourceClaim references one entry in PodSpec.ResourceClaims.
// This struct must be atomic as it is used within a slice with the
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this line is going to be picked up in openapi, I wonder if I can hack this to prevent it being picked up there

In the past I've put + at the beginning of the line to prevent generators including dev targeted comments, do we think that would be appropriate here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be allowed tbh, though that looks like something can be fixed by having listType=map, listMapKey=name?

With regards to validating these, yeah, we should do a better job.

https://kubernetes.io/docs/reference/using-api/server-side-apply/#merge-strategy

I don't know if the validation is correct, it should probably only allow scalar types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be allowed tbh, though that looks like something can be fixed by having listType=map, listMapKey=name?

In a more complex example where you do want a set, eg with 3 or 4 members in the struct, would that then mean you have to make every field a map key to get the same semantic?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, but the keys have to be scalars too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be allowed tbh, though that looks like something can be fixed by having listType=map, listMapKey=name?

Would this be your suggested path forward for this particular API then? Convert it to a map?

@JoelSpeed
Copy link
Contributor Author

What worries me is that this wasn't detected by any check in Kubernetes itself. If this annotation is required, shouldn't it be checked for in Kubernetes?

I think if we can add a check for this somewhere (not sure where yet) then that would help others down the line, perhaps the sig-apimachinery folks have some ideas on where best to stick that

@apelisse
Copy link
Member

That's how I would write it if this was new code, but I don't know the history here, so it's a little concerning to change it. how long has this been broken?

@JoelSpeed
Copy link
Contributor Author

It was merged just over a month ago

@pohly
Copy link
Contributor

pohly commented Dec 19, 2022

This API first appeared in 1.26.0.

sunjayBhatia added a commit to sunjayBhatia/contour that referenced this pull request Dec 19, 2022
corev1.ResourceRequirements "claims" field has missing
annotations that cause API server to reject it

Can be reverted once
kubernetes/kubernetes#114585 is fixed

Signed-off-by: Sunjay Bhatia <sunjayb@vmware.com>
sunjayBhatia added a commit to projectcontour/contour that referenced this pull request Dec 19, 2022
Includes a bump to controller-runtime and k8s API etc.
since Gateway API bumped those as dependencies as well.

Also inclused a hack for now to make API server happy

corev1.ResourceRequirements "claims" field has missing
annotations that cause API server to reject it

Those changes can be reverted once
kubernetes/kubernetes#114585 is fixed

Signed-off-by: Sunjay Bhatia <sunjayb@vmware.com>
@JoelSpeed
Copy link
Contributor Author

I've just noticed controller-tools have put their own fix into the CRD generator kubernetes-sigs/controller-tools#753

It definitely seems to solve the issue, but I'm concerned given the conversation whether this is the right path forward? Should we recommend against this?

Looking at the issue that it's linked to, this has been a direct result of the bug I'm trying to resolve

@apelisse
Copy link
Member

Thanks for finding that. Where is the type here used? Is it used only in (currently broken) CRDs or is it used in built-in types?

@JoelSpeed
Copy link
Contributor Author

Where is the type here used?

The type that's mentioned here, corev1.ResourceRequirements is being used in both CRDs and in the PodSpec and PersistentVolumeClaimSpec APIs, so the built-ins are also broken right now I guess? At least for SSA?

@apelisse
Copy link
Member

Yeah, probably :-(

@JoelSpeed
Copy link
Contributor Author

Given we think that the behaviour intended, would be the same with listType=map and listMapKey=name, should I update the PR to that? Would that be an acceptable API change given this has made it to a release?

@pohly
Copy link
Contributor

pohly commented Dec 20, 2022

Looks good to me. The map type also matches possible future extensions with additional parameters attached to each name, which was the whole reason why Claims contains structs instead of just plain strings.

@liggitt
Copy link
Member

liggitt commented Dec 20, 2022

The map type also matches possible future extensions with additional parameters attached to each name, which was the whole reason why Claims contains structs instead of just plain strings.

In the event that those parameters were added, names would still be required to be unique in the list (as they are today), right? If so, type=map, key=name is the right approach

@pohly
Copy link
Contributor

pohly commented Dec 20, 2022

Correct.

@liggitt
Copy link
Member

liggitt commented Dec 20, 2022

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Dec 20, 2022
@liggitt
Copy link
Member

liggitt commented Dec 20, 2022

looks like verify is happy. @JoelSpeed can you go ahead and pick this to release-1.26?

@JoelSpeed
Copy link
Contributor Author

Will the cherry pick bot work? Is there a reason not to use it?

I'm away from my laptop but can manually pick later if need be

@liggitt
Copy link
Member

liggitt commented Dec 20, 2022

Will the cherry pick bot work? Is there a reason not to use it?

cherry-pick bot isn't enabled for k/k, hack/cherry_pick_pull.sh is used in this repo

@k8s-ci-robot k8s-ci-robot merged commit 5882479 into kubernetes:master Dec 20, 2022
@k8s-ci-robot k8s-ci-robot added this to the v1.27 milestone Dec 20, 2022
@JoelSpeed JoelSpeed deleted the fix-resource-requirements-claims branch December 21, 2022 09:28
k8s-ci-robot added a commit that referenced this pull request Dec 27, 2022
…14585-upstream-release-1.26

Automated cherry pick of #114585: Resource claims should be a map type
nberlee added a commit to on2itsecurity/etcd-operator that referenced this pull request Jan 19, 2023
mtneug added a commit to nagare-media/engine that referenced this pull request Jan 22, 2023
yangyy93 pushed a commit to projectsesame/contour that referenced this pull request Feb 16, 2023
Includes a bump to controller-runtime and k8s API etc.
since Gateway API bumped those as dependencies as well.

Also inclused a hack for now to make API server happy

corev1.ResourceRequirements "claims" field has missing
annotations that cause API server to reject it

Those changes can be reverted once
kubernetes/kubernetes#114585 is fixed

Signed-off-by: Sunjay Bhatia <sunjayb@vmware.com>
Signed-off-by: yy <yang.yang@daocloud.io>
yangyy93 pushed a commit to projectsesame/contour that referenced this pull request Feb 16, 2023
Includes a bump to controller-runtime and k8s API etc.
since Gateway API bumped those as dependencies as well.

Also inclused a hack for now to make API server happy

corev1.ResourceRequirements "claims" field has missing
annotations that cause API server to reject it

Those changes can be reverted once
kubernetes/kubernetes#114585 is fixed

Signed-off-by: Sunjay Bhatia <sunjayb@vmware.com>
Signed-off-by: yy <yang.yang@daocloud.io>
vmw-yingy pushed a commit to vmw-yingy/contour that referenced this pull request Feb 28, 2023
Includes a bump to controller-runtime and k8s API etc.
since Gateway API bumped those as dependencies as well.

Also inclused a hack for now to make API server happy

corev1.ResourceRequirements "claims" field has missing
annotations that cause API server to reject it

Those changes can be reverted once
kubernetes/kubernetes#114585 is fixed

Signed-off-by: Sunjay Bhatia <sunjayb@vmware.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/code-generation cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API kind/bug Categorizes issue or PR as related to a bug. kind/regression Categorizes issue or PR as related to a regression from a prior release. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/critical-urgent Highest priority. Must be actively worked on as someone's top priority right now. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants