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

Make LossLessDefaulter selective to only prevent dropping fields outside of schema. #3186

Conversation

mbobrovskyi
Copy link
Contributor

@mbobrovskyi mbobrovskyi commented Oct 3, 2024

What type of PR is this?

/kind feature

What this PR does / why we need it:

Make LossLessDefaulter selective to only prevent dropping fields outside of schema.

Which issue(s) this PR fixes:

Fixes #3174

Special notes for your reviewer:

For more information see:

Does this PR introduce a user-facing change?

NONE

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Oct 3, 2024
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 3, 2024
Copy link

netlify bot commented Oct 3, 2024

Deploy Preview for kubernetes-sigs-kueue canceled.

Name Link
🔨 Latest commit ac7821f
🔍 Latest deploy log https://app.netlify.com/sites/kubernetes-sigs-kueue/deploys/67082c12c48c370008faf46c

@mbobrovskyi mbobrovskyi force-pushed the feature/make-loss-less-defaulter-selective-to-only-prevent-dropping-fields-outside-of-schema branch 4 times, most recently from 229669d to cc5bd04 Compare October 3, 2024 18:28
@mbobrovskyi
Copy link
Contributor Author

/cc @alculquicondor

Copy link
Contributor

@mimowo mimowo left a comment

Choose a reason for hiding this comment

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

I think this extension is quite complex and we need much better test coverage, optimally 100%, including cases like removing from a slice (for example to drop a condition).

It is essential piece of code for correctness of webhooks so as a bailout mechanism, in case of an issue, I would like to consider a construction parameter to control this selective behavior. Such as "AllowRemovingSchemaFields".

pkg/util/jsonpatch/jsonpatch.go Outdated Show resolved Hide resolved
pkg/util/jsonpatch/jsonpatch.go Outdated Show resolved Hide resolved
pkg/util/jsonpatch/jsonpatch.go Outdated Show resolved Hide resolved
pkg/util/jsonpatch/jsonpatch.go Outdated Show resolved Hide resolved
pkg/util/jsonpatch/jsonpatch.go Outdated Show resolved Hide resolved
pkg/controller/jobframework/webhook/defaulter_test.go Outdated Show resolved Hide resolved
pkg/util/jsonpatch/jsonpatch_test.go Outdated Show resolved Hide resolved
@mbobrovskyi mbobrovskyi force-pushed the feature/make-loss-less-defaulter-selective-to-only-prevent-dropping-fields-outside-of-schema branch from cc5bd04 to 965e0a8 Compare October 4, 2024 12:06
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mbobrovskyi
Once this PR has been reviewed and has the lgtm label, please assign kerthcet 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

@trasc
Copy link
Contributor

trasc commented Oct 4, 2024

Maybe I'm missing something but an alternative (slightly hacky) can be to overwrite the Raw object before the actual defaulter is called.

Something like:

import (
        "context"
+       "encoding/json"
+       "net/http"
-       jsonpatch "gomodules.xyz/jsonpatch/v2"
        "k8s.io/apimachinery/pkg/runtime"
        "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
@@ -29,11 +30,15 @@ import (
func WithLosslessDefaulter(scheme *runtime.Scheme, obj runtime.Object, defaulter admission.CustomDefaulter) admission.Handler {
        return &losslessDefaulter{
                Handler: admission.WithCustomDefaulter(scheme, obj, defaulter).Handler,
+               obj:     obj.DeepCopyObject(),
+               decoder: admission.NewDecoder(scheme),
        }
}
type losslessDefaulter struct {
        admission.Handler
+       obj     runtime.Object
+       decoder admission.Decoder
}
// Handle handles admission requests, **dropping** remove operations from patches produced by controller-runtime.
@@ -43,18 +48,14 @@ type losslessDefaulter struct {
// released CRDs.
// Dropping the "remove" operations is safe because Kueue's job mutators never remove fields.
func (h *losslessDefaulter) Handle(ctx context.Context, req admission.Request) admission.Response {
-       response := h.Handler.Handle(ctx, req)
-       if response.Allowed {
-               var patches []jsonpatch.Operation
-               for _, p := range response.Patches {
-                       if p.Operation != "remove" {
-                               patches = append(patches, p)
-                       }
-               }
-               if len(patches) == 0 {
-                       response.PatchType = nil
-               }
-               response.Patches = patches
+       o := h.obj.DeepCopyObject()
+       if err := h.decoder.Decode(req, o); err != nil {
+               return admission.Errored(http.StatusBadRequest, err)
        }
-       return response
+       marshalled, err := json.Marshal(o)
+       if err != nil {
+               return admission.Errored(http.StatusInternalServerError, err)
+       }
+       req.Object.Raw = marshalled
+       return h.Handler.Handle(ctx, req)
}

Copy link
Contributor

@trasc trasc left a comment

Choose a reason for hiding this comment

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

Maybe fieldExistsByJSONPath(object interface{}, path string) and friends can be moved in pkg/util/api.

Comment on lines 73 to 75
if len(pathParts) < 2 {
return false
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe add a comment explaining why < 2

return true
}

switch fv.Kind() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should also check for maps

@mimowo
Copy link
Contributor

mimowo commented Oct 7, 2024

Maybe I'm missing something but an alternative (slightly hacky) can be to overwrite the Raw object before the actual defaulter is called.

I'm not sure about that, maybe @alculquicondor can you think of some pros & cons of the alternative?

@trasc
Copy link
Contributor

trasc commented Oct 7, 2024

Maybe I'm missing something but an alternative (slightly hacky) can be to overwrite the Raw object before the actual defaulter is called.

I'm not sure about that, maybe @alculquicondor can you think of some pros & cons of the alternative?

I did find some discussions about that an the probability to generate invalid patches.

@mbobrovskyi
Copy link
Contributor Author

Maybe fieldExistsByJSONPath(object interface{}, path string) and friends can be moved in pkg/util/api.

We already discussed it here #3186 (comment) and take a decision to keep it on the same package.

@mbobrovskyi
Copy link
Contributor Author

mbobrovskyi commented Oct 7, 2024

/close

Due to found better solution #3194.

@k8s-ci-robot
Copy link
Contributor

@mbobrovskyi: Closed this PR.

In response to this:

/close

Due to found better solution.

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-sigs/prow repository.

@mbobrovskyi
Copy link
Contributor Author

mbobrovskyi commented Oct 10, 2024

/reopen

Due to #3194 (comment).

@k8s-ci-robot k8s-ci-robot reopened this Oct 10, 2024
@k8s-ci-robot
Copy link
Contributor

@mbobrovskyi: Reopened this PR.

In response to this:

/reopen

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-sigs/prow repository.

@mbobrovskyi mbobrovskyi marked this pull request as draft October 10, 2024 19:29
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 10, 2024
@mbobrovskyi mbobrovskyi force-pushed the feature/make-loss-less-defaulter-selective-to-only-prevent-dropping-fields-outside-of-schema branch from bc26d4e to ac7821f Compare October 10, 2024 19:33
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 10, 2024
@mbobrovskyi
Copy link
Contributor Author

mbobrovskyi commented Oct 25, 2024

/close

Due to very hard to handle inline object and Unmarshal methods.

@k8s-ci-robot
Copy link
Contributor

@mbobrovskyi: Closed this PR.

In response to this:

/close

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-sigs/prow 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. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. kind/feature Categorizes issue or PR as related to a new feature. release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make LossLessDefaulter selective to only prevent dropping fields outside of schema
4 participants