mirror of https://github.com/k3s-io/k3s
Merge pull request #74369 from skriss/object-tracker-merge-patch
fake client object tracker: support merge patchpull/564/head
commit
3199da2960
|
@ -181,11 +181,11 @@ func TestPatch(t *testing.T) {
|
|||
patchBytes: []byte(`[{"op": "add", "path": "/spec/newvalue", "value": "dummy"}]`),
|
||||
wantErrMsg: "invalid JSON document",
|
||||
}, {
|
||||
name: "merge patch fails as unsupported",
|
||||
object: newUnstructured(testAPIVersion, testKind, testNamespace, testName),
|
||||
patchType: types.MergePatchType,
|
||||
patchBytes: []byte(`{}`),
|
||||
wantErrMsg: "PatchType is not supported",
|
||||
name: "merge patch works with simple replacement",
|
||||
object: newUnstructuredWithSpec(map[string]interface{}{"foo": "bar"}),
|
||||
patchType: types.MergePatchType,
|
||||
patchBytes: []byte(`{ "spec": { "foo": "baz" } }`),
|
||||
expectedPatchedObject: newUnstructuredWithSpec(map[string]interface{}{"foo": "baz"}),
|
||||
},
|
||||
// TODO: Add tests for strategic merge using v1.Pod for example to ensure the test cases
|
||||
// demonstrate expected use cases.
|
||||
|
|
|
@ -20,7 +20,7 @@ import (
|
|||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/evanphx/json-patch"
|
||||
jsonpatch "github.com/evanphx/json-patch"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
@ -138,7 +138,7 @@ func ObjectReaction(tracker ObjectTracker) ReactionFunc {
|
|||
if err != nil {
|
||||
return true, nil, err
|
||||
}
|
||||
// Only supports strategic merge patch and JSONPatch as coded.
|
||||
|
||||
switch action.GetPatchType() {
|
||||
case types.JSONPatchType:
|
||||
patch, err := jsonpatch.DecodePatch(action.GetPatch())
|
||||
|
@ -152,6 +152,15 @@ func ObjectReaction(tracker ObjectTracker) ReactionFunc {
|
|||
if err = json.Unmarshal(modified, obj); err != nil {
|
||||
return true, nil, err
|
||||
}
|
||||
case types.MergePatchType:
|
||||
modified, err := jsonpatch.MergePatch(old, action.GetPatch())
|
||||
if err != nil {
|
||||
return true, nil, err
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(modified, obj); err != nil {
|
||||
return true, nil, err
|
||||
}
|
||||
case types.StrategicMergePatchType:
|
||||
mergedByte, err := strategicpatch.StrategicMergePatch(old, action.GetPatch(), obj)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue