Skip to content

Commit

Permalink
Merge pull request #256 from mengqiy/patchfromraw
Browse files Browse the repository at this point in the history
⚠️ introduce PatchResponseFromRaw and drop PatchResponse
  • Loading branch information
k8s-ci-robot committed Jan 24, 2019
2 parents d561f55 + e66d1ce commit 2aa4232
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 88 deletions.
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 {
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

0 comments on commit 2aa4232

Please sign in to comment.