Skip to content

Commit

Permalink
Merge pull request vmware-tanzu#7339 from ywk253100/240122_log_erro
Browse files Browse the repository at this point in the history
Log the error got from the discovery helper
  • Loading branch information
Lyndon-Li authored Jan 22, 2024
2 parents f5714cb + c377e47 commit 9fd73b2
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions pkg/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,16 +1058,14 @@ func (ctx *restoreContext) getResourceClient(groupResource schema.GroupResource,
return client, nil
}

func (ctx *restoreContext) getResourceLister(groupResource schema.GroupResource, obj *unstructured.Unstructured, namespace string) cache.GenericNamespaceLister {
func (ctx *restoreContext) getResourceLister(groupResource schema.GroupResource, obj *unstructured.Unstructured, namespace string) (cache.GenericNamespaceLister, error) {
_, _, err := ctx.discoveryHelper.KindFor(schema.GroupVersionKind{
Group: obj.GroupVersionKind().Group,
Version: obj.GetAPIVersion(),
Kind: obj.GetKind(),
})
clusterHasKind := err == nil
if !clusterHasKind {
ctx.log.Errorf("Cannot get resource lister %s because GVK doesn't exist in the cluster", groupResource)
return nil
if err != nil {
return nil, err
}
informer := ctx.dynamicInformerFactory.factory.ForResource(groupResource.WithVersion(obj.GroupVersionKind().Version))
// if the restore contains CRDs or the RIA returns new resources, need to make sure the corresponding informers are synced
Expand All @@ -1078,10 +1076,9 @@ func (ctx *restoreContext) getResourceLister(groupResource schema.GroupResource,
}

if namespace == "" {
return informer.Lister()
} else {
return informer.Lister().ByNamespace(namespace)
return informer.Lister(), nil
}
return informer.Lister().ByNamespace(namespace), nil
}

func getResourceID(groupResource schema.GroupResource, namespace, name string) string {
Expand All @@ -1093,10 +1090,9 @@ func getResourceID(groupResource schema.GroupResource, namespace, name string) s
}

func (ctx *restoreContext) getResource(groupResource schema.GroupResource, obj *unstructured.Unstructured, namespace, name string) (*unstructured.Unstructured, error) {
lister := ctx.getResourceLister(groupResource, obj, namespace)
if lister == nil {
// getResourceLister logs the error, this func returns error to the caller to trigger partiallyFailed.
return nil, errors.Errorf("Error getting lister for %s because no informer for GVK found", getResourceID(groupResource, namespace, name))
lister, err := ctx.getResourceLister(groupResource, obj, namespace)
if err != nil {
return nil, errors.Wrapf(err, "Error getting lister for %s", getResourceID(groupResource, namespace, name))
}
clusterObj, err := lister.Get(name)
if err != nil {
Expand Down

0 comments on commit 9fd73b2

Please sign in to comment.