Wait for CRDs to be ready before restoring CRs (#1937)

* Wait for CRDs to be available and ready

When restoring CRDs, we should wait for the definition to be ready and
available before moving on to restoring specific CRs.

While the CRDs are often ready by the time we get to restoring a CR,
there is a race condition where the CRD isn't ready.

This change waits on each CRD at restore time.

Signed-off-by: Nolan Brubaker <brubakern@vmware.com>
This commit is contained in:
Nolan Brubaker
2020-01-30 12:19:13 -05:00
committed by GitHub
parent 710beb96c2
commit 6745979a7b
8 changed files with 392 additions and 30 deletions

View File

@@ -34,6 +34,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"github.com/vmware-tanzu/velero/pkg/client"
"github.com/vmware-tanzu/velero/pkg/util/kube"
)
// kindToResource translates a Kind (mixed case, singular) to a Resource (lowercase, plural) string.
@@ -56,21 +57,6 @@ type ResourceGroup struct {
OtherResources []*unstructured.Unstructured
}
// crdIsReady checks a CRD to see if it's ready, so that objects may be created from it.
func crdIsReady(crd *apiextv1beta1.CustomResourceDefinition) bool {
var isEstablished, namesAccepted bool
for _, cond := range crd.Status.Conditions {
if cond.Type == apiextv1beta1.Established {
isEstablished = true
}
if cond.Type == apiextv1beta1.NamesAccepted {
namesAccepted = true
}
}
return (isEstablished && namesAccepted)
}
// crdsAreReady polls the API server to see if the BackupStorageLocation and VolumeSnapshotLocation CRDs are ready to create objects.
func crdsAreReady(factory client.DynamicFactory, crdKinds []string) (bool, error) {
gvk := schema.FromAPIVersionAndKind(apiextv1beta1.SchemeGroupVersion.String(), "CustomResourceDefinition")
@@ -108,7 +94,7 @@ func crdsAreReady(factory client.DynamicFactory, crdKinds []string) (bool, error
}
for _, crd := range foundCRDs {
if !crdIsReady(crd) {
if !kube.IsCRDReady(crd) {
return false, nil
}