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

⚠️ introduce PatchResponseFromRaw and drop PatchResponse #256

Merged
merged 1 commit into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions example/mutatingwebhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"encoding/json"
"net/http"

corev1 "k8s.io/api/core/v1"
Expand All @@ -44,13 +45,18 @@ func (a *podAnnotator) Handle(ctx context.Context, req types.Request) types.Resp
if err != nil {
return admission.ErrorResponse(http.StatusBadRequest, err)
}
copy := pod.DeepCopy()

err = a.mutatePodsFn(ctx, copy)
err = a.mutatePodsFn(ctx, pod)
if err != nil {
return admission.ErrorResponse(http.StatusInternalServerError, err)
}
return admission.PatchResponse(pod, copy)

marshaledPod, err := json.Marshal(pod)
if err != nil {
return admission.ErrorResponse(http.StatusInternalServerError, err)
}

return admission.PatchResponseFromRaw(req.AdmissionRequest.Object.Raw, marshaledPod)
}

// mutatePodsFn add an annotation to the given pod
Expand Down
33 changes: 0 additions & 33 deletions pkg/patch/doc.go

This file was deleted.

45 changes: 0 additions & 45 deletions pkg/patch/patch.go

This file was deleted.

12 changes: 7 additions & 5 deletions pkg/webhook/admission/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package admission
import (
"net/http"

"github.com/appscode/jsonpatch"

admissionv1beta1 "k8s.io/api/admission/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/patch"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
)

Expand Down Expand Up @@ -54,9 +54,11 @@ func ValidationResponse(allowed bool, reason string) types.Response {
return resp
}

// PatchResponse returns a new response with json patch.
func PatchResponse(original, current runtime.Object) types.Response {
patches, err := patch.NewJSONPatch(original, current)
// PatchResponseFromRaw takes 2 byte arrays and returns a new response with json patch.
// The original object should be passed in as raw bytes to avoid the roundtripping problem
// described in https://github.com/kubernetes-sigs/kubebuilder/issues/510.
func PatchResponseFromRaw(original, current []byte) types.Response {
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer this over the other PR which takes three params original, current, originalRawBytes which could get confusing for the end user and also not help user avoid the round-tripping issue.

patches, err := jsonpatch.CreatePatch(original, current)
if err != nil {
return ErrorResponse(http.StatusInternalServerError, err)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/webhook/admission/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
. "github.com/onsi/gomega"

admissionv1beta1 "k8s.io/api/admission/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
)
Expand Down Expand Up @@ -72,7 +71,7 @@ var _ = Describe("admission webhook response", func() {
PatchType: func() *admissionv1beta1.PatchType { pt := admissionv1beta1.PatchTypeJSONPatch; return &pt }(),
},
}
resp := PatchResponse(&corev1.Pod{}, &corev1.Pod{})
resp := PatchResponseFromRaw([]byte(`{}`), []byte(`{}`))
Expect(resp).To(Equal(expected))
})
})
Expand Down