Skip to content

Commit

Permalink
Do not attempt restore resource with no available GVK in cluster (vmw…
Browse files Browse the repository at this point in the history
…are-tanzu#7322)

Check for GVK before attempting restore.

Signed-off-by: Tiger Kaovilai <tkaovila@redhat.com>
  • Loading branch information
kaovilai authored and ywk253100 committed Jan 22, 2024
1 parent 509ceaa commit bd0ea3a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/7337-kaovilai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Check resource Group Version and Kind is available in cluster before attempting restore to prevent being stuck.
14 changes: 14 additions & 0 deletions pkg/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,16 @@ func (ctx *restoreContext) getResourceClient(groupResource schema.GroupResource,
}

func (ctx *restoreContext) getResourceLister(groupResource schema.GroupResource, obj *unstructured.Unstructured, namespace string) cache.GenericNamespaceLister {
_, _, 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
}
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
if !informer.Informer().HasSynced() {
Expand All @@ -1043,6 +1053,10 @@ 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))
}
clusterObj, err := lister.Get(name)
if err != nil {
return nil, errors.Wrapf(err, "error getting resource from lister for %s, %s/%s", groupResource, namespace, name)
Expand Down
1 change: 1 addition & 0 deletions pkg/test/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func Pods(items ...metav1.Object) *APIResource {
ShortName: "po",
Namespaced: true,
Items: items,
Kind: "Pod",
}
}

Expand Down

0 comments on commit bd0ea3a

Please sign in to comment.