Merge pull request #256 from skriss/unbound-pvc-fix
don't error if PVC is not bound to a PVpull/248/merge
commit
fea701a03b
|
@ -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{
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue