From 79c0e5a09aa14d5661417c1568a406dfce600e65 Mon Sep 17 00:00:00 2001 From: Steve Kriss Date: Tue, 19 Dec 2017 14:10:58 -0800 Subject: [PATCH] don't error if PVC is not bound to a PV Signed-off-by: Steve Kriss --- pkg/backup/backup_pv_action.go | 13 ++++++++----- pkg/backup/backup_pv_action_test.go | 14 +++++++++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/pkg/backup/backup_pv_action.go b/pkg/backup/backup_pv_action.go index bd6b7d9be..5403a97d0 100644 --- a/pkg/backup/backup_pv_action.go +++ b/pkg/backup/backup_pv_action.go @@ -17,7 +17,6 @@ limitations under the License. package backup import ( - "github.com/pkg/errors" "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/runtime" @@ -45,8 +44,8 @@ func (a *backupPVAction) AppliesTo() (ResourceSelector, error) { }, nil } -// Execute finds the PersistentVolume referenced by the provided -// PersistentVolumeClaim and backs it up +// Execute finds the PersistentVolume bound by the provided +// PersistentVolumeClaim, if any, and backs it up func (a *backupPVAction) Execute(item runtime.Unstructured, backup *v1.Backup) (runtime.Unstructured, []ResourceIdentifier, error) { a.log.Info("Executing backupPVAction") @@ -55,8 +54,12 @@ func (a *backupPVAction) Execute(item runtime.Unstructured, backup *v1.Backup) ( pvc := item.UnstructuredContent() volumeName, err := collections.GetString(pvc, "spec.volumeName") - if err != nil { - return nil, nil, errors.WithMessage(err, "unable to get spec.volumeName") + // if there's no volume name, it's not an error, since it's possible + // for the PVC not be bound; don't return an additional PV item to + // back up. + if err != nil || volumeName == "" { + a.log.Info("No spec.volumeName found for PersistentVolumeClaim") + return nil, nil, nil } additionalItems = append(additionalItems, ResourceIdentifier{ diff --git a/pkg/backup/backup_pv_action_test.go b/pkg/backup/backup_pv_action_test.go index 82eae87fc..5a6693d52 100644 --- a/pkg/backup/backup_pv_action_test.go +++ b/pkg/backup/backup_pv_action_test.go @@ -37,9 +37,21 @@ func TestBackupPVAction(t *testing.T) { a := NewBackupPVAction(arktest.NewLogger()) + // no spec.volumeName should result in no error + // and no additional items _, additional, err := a.Execute(pvc, backup) - assert.EqualError(t, err, "unable to get spec.volumeName: key volumeName not found") + assert.NoError(t, err) + assert.Len(t, additional, 0) + // empty spec.volumeName should result in no error + // and no additional items + pvc.Object["spec"].(map[string]interface{})["volumeName"] = "" + _, additional, err = a.Execute(pvc, backup) + assert.NoError(t, err) + assert.Len(t, additional, 0) + + // non-empty spec.volumeName should result in + // no error and an additional item for the PV pvc.Object["spec"].(map[string]interface{})["volumeName"] = "myVolume" _, additional, err = a.Execute(pvc, backup) require.NoError(t, err)