From 378801455226b77727b7e631b22d05884c538a57 Mon Sep 17 00:00:00 2001 From: Andy Goldstein Date: Thu, 8 Nov 2018 10:56:14 -0500 Subject: [PATCH] Fix check for non-found PV We were checking for nil, but were getting back an empty *unstructured.Unstructured{} instead, along with a NotFound error. Change the logic to check for the NotFound error instead of a nil object. Signed-off-by: Andy Goldstein --- pkg/restore/restore.go | 11 +++++------ pkg/restore/restore_test.go | 3 +-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/pkg/restore/restore.go b/pkg/restore/restore.go index 8fe1b3128..d15c8948f 100644 --- a/pkg/restore/restore.go +++ b/pkg/restore/restore.go @@ -696,14 +696,10 @@ func (ctx *context) restoreResource(resource, namespace, resourcePath string) (a // Check if the PV exists in the cluster before attempting to create // a volume from the snapshot, in order to avoid orphaned volumes (GH #609) - pv, err := resourceClient.Get(name, metav1.GetOptions{}) - if err != nil && !apierrors.IsNotFound(err) { - addToResult(&errs, namespace, fmt.Errorf("error checking existence for PV %s: %v", name, err)) - continue - } + _, err := resourceClient.Get(name, metav1.GetOptions{}) // PV's existence will be recorded later. Just skip the volume restore logic. - if pv == nil { + if apierrors.IsNotFound(err) { // restore the PV from snapshot (if applicable) updatedObj, err := ctx.pvRestorer.executePVAction(obj) if err != nil { @@ -729,6 +725,9 @@ func (ctx *context) restoreResource(resource, namespace, resourcePath string) (a } }() } + } else if err != nil { + addToResult(&errs, namespace, fmt.Errorf("error checking existence for PV %s: %v", name, err)) + continue } } diff --git a/pkg/restore/restore_test.go b/pkg/restore/restore_test.go index 2bcf8fc30..2e10fdc61 100644 --- a/pkg/restore/restore_test.go +++ b/pkg/restore/restore_test.go @@ -1007,8 +1007,7 @@ status: // Only set up the client expectation if the test has the proper prerequisites if test.haveSnapshot || test.reclaimPolicy != "Delete" { - var empty *unstructured.Unstructured - pvClient.On("Get", mock.Anything, metav1.GetOptions{}).Return(empty, nil) + pvClient.On("Get", unstructuredPV.GetName(), metav1.GetOptions{}).Return(&unstructured.Unstructured{}, k8serrors.NewNotFound(schema.GroupResource{Resource: "persistentvolumes"}, unstructuredPV.GetName())) } pvToRestore := unstructuredPV.DeepCopy()