@@ -20,6 +20,7 @@ import (
2020 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2121 "k8s.io/apimachinery/pkg/runtime"
2222 "sigs.k8s.io/controller-runtime/pkg/client"
23+ "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
2324)
2425
2526const FieldOwner = "mongodb-atlas-kubernetes"
@@ -89,8 +90,14 @@ func (p *Patcher) patchObject(ctx context.Context, c client.Client) {
8990 return
9091 }
9192
92- applyConfig := client .ApplyConfigurationFromUnstructured (p .patchedObj )
93- err := c .Apply (ctx , applyConfig , client .FieldOwner (FieldOwner ), client .ForceOwnership )
93+ patchedCopy , err := p .copyPatchedObject (c )
94+ if err != nil {
95+ p .err = err
96+ return
97+ }
98+
99+ applyConfig := client .ApplyConfigurationFromUnstructured (patchedCopy )
100+ err = c .Apply (ctx , applyConfig , client .FieldOwner (FieldOwner ), client .ForceOwnership )
94101 p .err = err
95102}
96103
@@ -99,13 +106,31 @@ func (p *Patcher) patchStatus(ctx context.Context, c client.Client) {
99106 return
100107 }
101108
109+ patchedCopy , err := p .copyPatchedObject (c )
110+ if err != nil {
111+ p .err = err
112+ return
113+ }
114+
102115 // SSA Apply() method for sub-resources is not yet supported, so we use Patch here.
103116 // See the following issue for more details: https://github.com/kubernetes-sigs/controller-runtime/issues/3183
104- patchedCopy := p .patchedObj .DeepCopy ()
105- err := c .Status ().Patch (ctx , patchedCopy , client .Apply , client .FieldOwner (FieldOwner ), client .ForceOwnership )
117+ err = c .Status ().Patch (ctx , patchedCopy , client .Apply , client .FieldOwner (FieldOwner ), client .ForceOwnership )
106118 p .err = err
107119}
108120
121+ func (p * Patcher ) copyPatchedObject (c client.Client ) (* unstructured.Unstructured , error ) {
122+ patchedCopy := p .patchedObj .DeepCopy ()
123+ if patchedCopy .GetObjectKind ().GroupVersionKind ().Empty () {
124+ gvk , err := apiutil .GVKForObject (p .obj , c .Scheme ())
125+ if err != nil {
126+ return nil , err
127+ }
128+ patchedCopy .SetAPIVersion (gvk .GroupVersion ().String ())
129+ patchedCopy .SetKind (gvk .Kind )
130+ }
131+ return patchedCopy , nil
132+ }
133+
109134// Patch applies the patches to the given object and updates both status and the annotations if they were modified.
110135func (p * Patcher ) Patch (ctx context.Context , c client.Client ) error {
111136 if p .err != nil {
0 commit comments