Skip to content

Commit 2915b92

Browse files
committed
Make MergeFrom use strategic merge patch
If you're using Kubernetes, you probably want a strategic merge patch anyway. You can always to a merge patch with the manual patch type.
1 parent 3511f3c commit 2915b92

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

pkg/client/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2262,7 +2262,7 @@ var _ = Describe("Patch", func() {
22622262
patch := client.MergeFrom(cm.DeepCopy())
22632263

22642264
By("returning a patch with type MergePatch")
2265-
Expect(patch.Type()).To(Equal(types.MergePatchType))
2265+
Expect(patch.Type()).To(Equal(types.StrategicMergePatchType))
22662266

22672267
By("retrieving modifying the config map")
22682268
metav1.SetMetaDataAnnotation(&cm.ObjectMeta, annotationKey, annotationValue)

pkg/client/patch.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ limitations under the License.
1717
package client
1818

1919
import (
20-
jsonpatch "github.com/evanphx/json-patch"
2120
"k8s.io/apimachinery/pkg/runtime"
2221
"k8s.io/apimachinery/pkg/types"
2322
"k8s.io/apimachinery/pkg/util/json"
23+
"k8s.io/apimachinery/pkg/util/strategicpatch"
2424
)
2525

2626
type patch struct {
@@ -47,9 +47,9 @@ type mergeFromPatch struct {
4747
from runtime.Object
4848
}
4949

50-
// Type implements patch.
50+
// Type implements Patch.
5151
func (s *mergeFromPatch) Type() types.PatchType {
52-
return types.MergePatchType
52+
return types.StrategicMergePatchType
5353
}
5454

5555
// Data implements Patch.
@@ -64,10 +64,11 @@ func (s *mergeFromPatch) Data(obj runtime.Object) ([]byte, error) {
6464
return nil, err
6565
}
6666

67-
return jsonpatch.CreateMergePatch(originalJSON, modifiedJSON)
67+
return strategicpatch.CreateTwoWayMergePatch(originalJSON, modifiedJSON, obj)
6868
}
6969

70-
// MergeFrom creates a Patch that patches using the merge-patch strategy with the given object as base.
70+
// MergeFrom creates a Patch that patches using the strategic
71+
// merge-patch strategy with the given object as base.
7172
func MergeFrom(obj runtime.Object) Patch {
7273
return &mergeFromPatch{obj}
7374
}

0 commit comments

Comments
 (0)