@@ -20,7 +20,6 @@ import (
20
20
"context"
21
21
"fmt"
22
22
23
- apierrors "k8s.io/apimachinery/pkg/api/errors"
24
23
"sigs.k8s.io/controller-runtime/pkg/client"
25
24
"sigs.k8s.io/controller-runtime/pkg/log"
26
25
)
@@ -52,24 +51,27 @@ func Patch(ctx context.Context, cl client.Client, obj client.Object, options ...
52
51
53
52
patchOptions := []client.PatchOption {
54
53
client .FieldOwner (fieldOwner ),
54
+ client .ForceOwnership ,
55
55
}
56
56
patchOptions = append (patchOptions , options ... )
57
57
58
- err := cl .Patch (ctx , obj , client .Merge , patchOptions ... )
59
- if apierrors .IsNotFound (err ) {
60
- return cl .Create (ctx , obj , []client.CreateOption {client .FieldOwner (fieldOwner )}... )
61
- }
62
-
63
- return err
58
+ return cl .Patch (ctx , obj , client .Apply , patchOptions ... )
64
59
}
65
60
66
61
// PatchStatus will only patch the status subresource of the provided object.
67
62
func PatchStatus (ctx context.Context , cl client.Client , obj client.Object ) error {
68
63
log := log .FromContext (ctx )
69
64
65
+ obj .SetManagedFields (nil )
66
+
67
+ if err := setKind (cl , obj ); err != nil {
68
+ return err
69
+ }
70
+
70
71
log .Info (fmt .Sprintf ("Patching status %s: %s" , obj .GetObjectKind ().GroupVersionKind ().Kind , client .ObjectKeyFromObject (obj )))
71
72
72
- return cl .Status ().Patch (ctx , obj , client .Merge , []client.SubResourcePatchOption {
73
+ return cl .Status ().Patch (ctx , obj , client .Apply , []client.SubResourcePatchOption {
73
74
client .FieldOwner (fieldOwner ),
75
+ client .ForceOwnership ,
74
76
}... )
75
77
}
0 commit comments