@@ -292,15 +292,36 @@ func (icm *InventoryResourceGroup) ApplyWithPrune(dc dynamic.Interface, mapper m
292292 }
293293
294294 // Update the cluster inventory object.
295+ // Since the ResourceGroup CRD specifies the status as a sub-resource, this
296+ // will not update the status.
295297 appliedObj , err := namespacedClient .Update (context .TODO (), invInfo , metav1.UpdateOptions {})
298+ if err != nil {
299+ return err
300+ }
296301
297- // Update status.
302+ // Update status, if status policy allows it.
303+ // To avoid losing modifications performed by mutating webhooks, copy the
304+ // status from the desired state to the latest state after the previous update.
305+ // This also ensures that the ResourceVersion matches the latest state, to
306+ // avoid the update being rejected by the server.
298307 if statusPolicy == inventory .StatusPolicyAll {
299- invInfo .SetResourceVersion (appliedObj .GetResourceVersion ())
300- _ , err = namespacedClient .UpdateStatus (context .TODO (), invInfo , metav1.UpdateOptions {})
308+ status , found , err := unstructured .NestedMap (invInfo .UnstructuredContent (), "status" )
309+ if err != nil {
310+ return err
311+ }
312+ if found {
313+ err = unstructured .SetNestedField (appliedObj .UnstructuredContent (), status , "status" )
314+ if err != nil {
315+ return err
316+ }
317+ _ , err = namespacedClient .UpdateStatus (context .TODO (), appliedObj , metav1.UpdateOptions {})
318+ if err != nil {
319+ return err
320+ }
321+ }
301322 }
302323
303- return err
324+ return nil
304325}
305326
306327func (icm * InventoryResourceGroup ) getNamespacedClient (dc dynamic.Interface , mapper meta.RESTMapper ) (* unstructured.Unstructured , dynamic.ResourceInterface , error ) {
0 commit comments