kueue icon indicating copy to clipboard operation
kueue copied to clipboard

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

Open mbobrovskyi opened this issue 1 year ago • 11 comments

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:

  • https://jsonpatch.com/
  • https://datatracker.ietf.org/doc/html/rfc6902/
  • https://datatracker.ietf.org/doc/html/rfc6901

Does this PR introduce a user-facing change?

NONE

mbobrovskyi avatar Oct 03 '24 18:10 mbobrovskyi

Deploy Preview for kubernetes-sigs-kueue canceled.

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

netlify[bot] avatar Oct 03 '24 18:10 netlify[bot]

/cc @alculquicondor

mbobrovskyi avatar Oct 03 '24 18:10 mbobrovskyi

[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

k8s-ci-robot avatar Oct 04 '24 12:10 k8s-ci-robot

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)
}

trasc avatar Oct 04 '24 13:10 trasc

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?

mimowo avatar Oct 07 '24 07:10 mimowo

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.

trasc avatar Oct 07 '24 09:10 trasc

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

We already discussed it here https://github.com/kubernetes-sigs/kueue/pull/3186#discussion_r1787524717 and take a decision to keep it on the same package.

mbobrovskyi avatar Oct 07 '24 11:10 mbobrovskyi

/close

Due to found better solution https://github.com/kubernetes-sigs/kueue/pull/3194.

mbobrovskyi avatar Oct 07 '24 17:10 mbobrovskyi

@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.

k8s-ci-robot avatar Oct 07 '24 17:10 k8s-ci-robot

/reopen

Due to https://github.com/kubernetes-sigs/kueue/pull/3194#discussion_r1795708267.

mbobrovskyi avatar Oct 10 '24 15:10 mbobrovskyi

@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.

k8s-ci-robot avatar Oct 10 '24 15:10 k8s-ci-robot

/close

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

mbobrovskyi avatar Oct 25 '24 09:10 mbobrovskyi

@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.

k8s-ci-robot avatar Oct 25 '24 09:10 k8s-ci-robot