@@ -18,17 +18,20 @@ import (
18
18
"context"
19
19
"fmt"
20
20
"net/http"
21
+ "reflect"
21
22
"strings"
22
23
24
+ jsonpatch "github.com/evanphx/json-patch/v5"
23
25
"github.com/gobuffalo/flect"
24
26
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
27
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26
28
"k8s.io/apimachinery/pkg/runtime"
27
29
"k8s.io/apimachinery/pkg/runtime/schema"
28
30
k8stypes "k8s.io/apimachinery/pkg/types"
29
- "k8s.io/apimachinery/pkg/util/jsonmergepatch "
31
+ "k8s.io/apimachinery/pkg/util/strategicpatch "
30
32
"k8s.io/client-go/discovery"
31
33
"k8s.io/client-go/dynamic"
34
+ "k8s.io/client-go/kubernetes/scheme"
32
35
"k8s.io/client-go/util/retry"
33
36
"k8s.io/klog/v2"
34
37
)
@@ -169,14 +172,39 @@ func (v *resourceVerber) Update(object *unstructured.Unstructured) error {
169
172
return fmt .Errorf ("failed to marshal modified data: %w" , err )
170
173
}
171
174
172
- patchBytes , err := jsonmergepatch . CreateThreeWayJSONMergePatch ( origData , modifiedData , origData )
173
- if err != nil {
174
- return fmt . Errorf ( "failed creating merge patch: %w" , err )
175
+ if reflect . DeepEqual ( result , object ) {
176
+ klog . V ( 3 ). InfoS ( "original and updated objects are the same, skipping" )
177
+ return nil
175
178
}
176
179
177
- klog .V (2 ).InfoS ("patching resource" , "group" , gvr .Group , "version" , gvr .Version , "resource" , gvr .Resource , "name" , name , "namespace" , namespace , "patch" , string (patchBytes ))
178
- _ , updateErr := v .client .Resource (gvr ).Namespace (namespace ).Patch (context .TODO (), name , k8stypes .MergePatchType , patchBytes , metav1.PatchOptions {})
179
- return updateErr
180
+ versionedObject , err := scheme .Scheme .New (schema.GroupVersionKind {
181
+ Group : gvr .Group ,
182
+ Version : gvr .Version ,
183
+ Kind : object .GetKind (),
184
+ })
185
+
186
+ switch {
187
+ case runtime .IsNotRegisteredError (err ):
188
+ patchBytes , err := jsonpatch .CreateMergePatch (origData , modifiedData )
189
+ if err != nil {
190
+ return fmt .Errorf ("failed creating merge patch: %w" , err )
191
+ }
192
+
193
+ klog .V (2 ).InfoS ("patching resource" , "group" , gvr .Group , "version" , gvr .Version , "resource" , gvr .Resource , "name" , name , "namespace" , namespace , "patch" , string (patchBytes ))
194
+ _ , updateErr := v .client .Resource (gvr ).Namespace (namespace ).Patch (context .TODO (), name , k8stypes .MergePatchType , patchBytes , metav1.PatchOptions {})
195
+ return updateErr
196
+ case err != nil :
197
+ return err
198
+ default :
199
+ patchBytes , err := strategicpatch .CreateTwoWayMergePatch (origData , modifiedData , versionedObject )
200
+ if err != nil {
201
+ return fmt .Errorf ("failed creating two way merge patch: %w" , err )
202
+ }
203
+
204
+ klog .V (2 ).InfoS ("patching resource" , "group" , gvr .Group , "version" , gvr .Version , "resource" , gvr .Resource , "name" , name , "namespace" , namespace , "patch" , string (patchBytes ))
205
+ _ , updateErr := v .client .Resource (gvr ).Namespace (namespace ).Patch (context .TODO (), name , k8stypes .StrategicMergePatchType , patchBytes , metav1.PatchOptions {})
206
+ return updateErr
207
+ }
180
208
})
181
209
}
182
210
0 commit comments