You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Error from server (InternalError): error when creating "test.yaml":
Internal error occurred: Internal error occurred:
jsonpatch add operation does not apply: doc is missing path: /spec/bar/baz
This is because my test.yaml has no bar in spec, but admission ask to add /spec/bar/baz.
My opinion
admission.PatchResponse should use req.AdmissionRequest.Object.Raw as original object to prepare with current object.
The problem is, the obj in handler contains some fields/objects no existing in original object raw. So when we compare obj with current object, we might get the wrong json patch, and apiserver will report such error message.
The correct code like this
controller-runtime
// PatchResponse returns a new response with json patch.
func PatchResponseWithRaw(originalRaw []byte, original, current runtime.Object) types.Response {
patches, err := patch.newJSONPatchWithRaw(originalRaw, original, current)
if err != nil {
return admission.ErrorResponse(http.StatusInternalServerError, err)
}
return types.Response{
Patches: patches,
Response: &admissionv1beta1.AdmissionResponse{
Allowed: true,
PatchType: func() *admissionv1beta1.PatchType { pt := admissionv1beta1.PatchTypeJSONPatch; return &pt }(),
},
}
}
// NewJSONPatch calculates the JSON patch between original and current objects.
func NewJSONPatchWithRaw(originalRaw []byte, original, current runtime.Object) ([]jsonpatch.JsonPatchOperation, error) {
originalGVK := original.GetObjectKind().GroupVersionKind()
currentGVK := current.GetObjectKind().GroupVersionKind()
if !reflect.DeepEqual(originalGVK, currentGVK) {
return nil, fmt.Errorf("GroupVersionKind %#v is expected to match %#v", originalGVK, currentGVK)
}
cur, err := json.Marshal(current)
if err != nil {
return nil, err
}
return jsonpatch.CreatePatch(originalRaw, cur)
}
mutating admission handler use new admission.PatchResponseWithRaw
The text was updated successfully, but these errors were encountered:
FillZpp
changed the title
🐛 mutating webhook admission failed because of wrong json patch
[bug] mutating webhook admission failed because of wrong json patch
Dec 5, 2018
Take an example
This is because my test.yaml has no bar in spec, but admission ask to add /spec/bar/baz.
My opinion
admission.PatchResponse
should usereq.AdmissionRequest.Object.Raw
as original object to prepare with current object.The problem is, the
obj
in handler contains some fields/objects no existing in original object raw. So when we compareobj
with current object, we might get the wrong json patch, and apiserver will report such error message.The correct code like this
controller-runtime
mutating admission handler use new admission.PatchResponseWithRaw
The text was updated successfully, but these errors were encountered: