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

fix: remove refs issue #55

Closed
wants to merge 2 commits into from

Conversation

eddycharly
Copy link
Contributor

@eddycharly eddycharly commented Jun 6, 2023

This PR fixes a removeRefs issue.

It looks like we need to reset extensions before comparing with an empty schema.

Fixes #54

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 6, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: eddycharly
Once this PR has been reviewed and has the lgtm label, please assign richabanker for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Jun 6, 2023
@alexzielenski
Copy link
Contributor

alexzielenski commented Jun 6, 2023

Looks like in this bug's case the schema for the field that's being resolve looks like:

"strategy": {
    "allOf": [
      {
        "$ref": "#/components/schemas/io.k8s.api.apps.v1.DeploymentStrategy"
      }
    ],
    "default": {},
    "description": "The deployment strategy to use to replace existing pods with new ones.",
    "x-kubernetes-patch-strategy": "retainKeys"
},

This bug can happen if there is a field-level override of the referred schema. I am curious if other cases like this exist. The proposed solution would lose the information that the field uses retainKeys. I can imagine other field-level extensions useful for validation that would be lost here like x-kubernetes-validations or x-kuberenetes-list-type. The original code also erroneous strips the Default value, which is another bug.

This part of the code is completely a hack to satisfy the requirements of structural schema which does not like new properties inside allOf (structural schema is required to run validators). Upstream we have a loose plan to add validation that does not require the structural schema and operates instead on kube-openapi for both CRs and native types. This goal is long away, so we have this hack.

xref: kubernetes/kubernetes#106387
xref: kubernetes/kube-openapi#298

I'm thinking if we see a schema that meets the following conditions:

  1. len(s.allOf) = 1
  2. len(s.allOf[0].Ref) > 0

Then it should just be interpreted as a ref without any additional checks. We can be confident in this approach since k8s apiserver is the only source of $refs, and it always wraps them in a single element allOf.

We should create a copy of the referred schema, and set all non-empty properties of the field's override schema onto the referred schema.

So in the case of strategy above:

  1. vCopy := removeRefs(s.allOf[0].Ref)
  2. vCopy.Default = s.Default
  3. vCopy.Description = s.Description
  4. for k, v in range s.Extensions: vCopy.Extensions[k] = v (may need to copy vCopy.Extensions too to avoid changing the original map)
  5. All other non-empty properties of the field are merged into vCopy

@eddycharly
Copy link
Contributor Author

Thanks @alexzielenski for the detailed explanation. I will try to implement what you suggested today.

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Jun 7, 2023
@eddycharly
Copy link
Contributor Author

@alexzielenski i'm not very familiar with this code, I tried to follow your explanations but tests are failing, would you mind giving a look please ?

vCopy := removeRefs(defs, sch.AllOf[0])
vCopy.Default = sch.Default
vCopy.Description = sch.Description
if sch.Extensions != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

probably want to copy all nonzero properties and not just Default, Description, Extensions. Not sure if there is a clean go-ey way to do that

pkg/validatorfactory/factory.go Show resolved Hide resolved
@alexzielenski
Copy link
Contributor

If you base on #60 I think some of the issues can go away. That would just leave the last comment from my last review to resolve

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 28, 2023
@k8s-ci-robot
Copy link
Contributor

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@alexzielenski
Copy link
Contributor

/close

I think all the changes from this PR were merged in #67 and #60

@k8s-ci-robot
Copy link
Contributor

@alexzielenski: Closed this PR.

In response to this:

/close

I think all the changes from this PR were merged in #67 and #60

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deployments with .spec.strategy specified fail validation
3 participants