Skip to content

Commit fc62243

Browse files
authored
fix: Avoid panic when inventory update errors (#4055)
Also avoid losing modifications due to mutating webhooks. Signed-off-by: Karl Isenberg <[email protected]>
1 parent 4c4cf98 commit fc62243

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

pkg/live/inventoryrg.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

306327
func (icm *InventoryResourceGroup) getNamespacedClient(dc dynamic.Interface, mapper meta.RESTMapper) (*unstructured.Unstructured, dynamic.ResourceInterface, error) {

0 commit comments

Comments
 (0)