Skip to content

Commit 1478c39

Browse files
committed
Change MergeFrom to use strategic merge patch
Strategic merge patch is what most things in kubernetes use, and probably closer to what users expect if they can't use server-side apply.
1 parent 28a96c7 commit 1478c39

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

pkg/client/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,7 +2228,7 @@ var _ = Describe("Patch", func() {
22282228
}
22292229
})
22302230

2231-
It("creates a merge patch with the modifications applied during the mutation", func() {
2231+
It("creates a strategic merge patch with the modifications applied during the mutation", func() {
22322232
const (
22332233
annotationKey = "test"
22342234
annotationValue = "foo"
@@ -2238,7 +2238,7 @@ var _ = Describe("Patch", func() {
22382238
patch := client.MergeFrom(cm.DeepCopy())
22392239

22402240
By("returning a patch with type MergePatch")
2241-
Expect(patch.Type()).To(Equal(types.MergePatchType))
2241+
Expect(patch.Type()).To(Equal(types.StrategicMergePatchType))
22422242

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

pkg/client/patch.go

Lines changed: 5 additions & 4 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 {
@@ -49,7 +49,7 @@ type mergeFromPatch struct {
4949

5050
// 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)