Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Avoid panic when inventory update errors #4055

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions pkg/live/inventoryrg.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,36 @@ func (icm *InventoryResourceGroup) ApplyWithPrune(dc dynamic.Interface, mapper m
}

// Update the cluster inventory object.
// Since the ResourceGroup CRD specifies the status as a sub-resource, this
// will not update the status.
appliedObj, err := namespacedClient.Update(context.TODO(), invInfo, metav1.UpdateOptions{})
if err != nil {
return err
}

// Update status.
// Update status, if status policy allows it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some unit tests here would be nice to have if it's not too cumbersome

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding unit tests would require faking the dynamic.Interface and meta.RESTMapper. The fakes I wrote are in kpt-config-sync downstream, which make them hard to use here. If we want to use the fake clients in kpt, we'll have to upstream the fakes into cli-utils first.

So adding unit tests might be more effort than it's worth at this point.

// To avoid losing modifications performed by mutating webhooks, copy the
// status from the desired state to the latest state after the previous update.
// This also ensures that the ResourceVersion matches the latest state, to
// avoid the update being rejected by the server.
if statusPolicy == inventory.StatusPolicyAll {
invInfo.SetResourceVersion(appliedObj.GetResourceVersion())
_, err = namespacedClient.UpdateStatus(context.TODO(), invInfo, metav1.UpdateOptions{})
status, found, err := unstructured.NestedMap(invInfo.UnstructuredContent(), "status")
if err != nil {
return err
}
if found {
err = unstructured.SetNestedField(appliedObj.UnstructuredContent(), status, "status")
if err != nil {
return err
}
_, err = namespacedClient.UpdateStatus(context.TODO(), appliedObj, metav1.UpdateOptions{})
if err != nil {
return err
}
}
}

return err
return nil
}

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