Do not attempt restore resource with no available GVK in cluster (#7322)

Check for GVK before attempting restore.

Signed-off-by: Tiger Kaovilai <tkaovila@redhat.com>
pull/7340/head
Tiger Kaovilai 2024-01-22 08:50:13 +07:00 committed by GitHub
parent a81e049d36
commit 270b1de6a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 0 deletions

View File

@ -0,0 +1 @@
Check resource Group Version and Kind is available in cluster before attempting restore to prevent being stuck.

View File

@ -1059,6 +1059,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() {
@ -1084,6 +1094,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)

View File

@ -51,6 +51,7 @@ func Pods(items ...metav1.Object) *APIResource {
ShortName: "po",
Namespaced: true,
Items: items,
Kind: "Pod",
}
}