Merge pull request #256 from skriss/unbound-pvc-fix

don't error if PVC is not bound to a PV
pull/248/merge
Andy Goldstein 2017-12-19 19:37:28 -05:00 committed by GitHub
commit fea701a03b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 6 deletions

View File

@ -17,7 +17,6 @@ limitations under the License.
package backup package backup
import ( import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
@ -45,8 +44,8 @@ func (a *backupPVAction) AppliesTo() (ResourceSelector, error) {
}, nil }, nil
} }
// Execute finds the PersistentVolume referenced by the provided // Execute finds the PersistentVolume bound by the provided
// PersistentVolumeClaim and backs it up // PersistentVolumeClaim, if any, and backs it up
func (a *backupPVAction) Execute(item runtime.Unstructured, backup *v1.Backup) (runtime.Unstructured, []ResourceIdentifier, error) { func (a *backupPVAction) Execute(item runtime.Unstructured, backup *v1.Backup) (runtime.Unstructured, []ResourceIdentifier, error) {
a.log.Info("Executing backupPVAction") a.log.Info("Executing backupPVAction")
@ -55,8 +54,12 @@ func (a *backupPVAction) Execute(item runtime.Unstructured, backup *v1.Backup) (
pvc := item.UnstructuredContent() pvc := item.UnstructuredContent()
volumeName, err := collections.GetString(pvc, "spec.volumeName") volumeName, err := collections.GetString(pvc, "spec.volumeName")
if err != nil { // if there's no volume name, it's not an error, since it's possible
return nil, nil, errors.WithMessage(err, "unable to get spec.volumeName") // 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{ additionalItems = append(additionalItems, ResourceIdentifier{

View File

@ -37,9 +37,21 @@ func TestBackupPVAction(t *testing.T) {
a := NewBackupPVAction(arktest.NewLogger()) a := NewBackupPVAction(arktest.NewLogger())
// no spec.volumeName should result in no error
// and no additional items
_, additional, err := a.Execute(pvc, backup) _, 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" pvc.Object["spec"].(map[string]interface{})["volumeName"] = "myVolume"
_, additional, err = a.Execute(pvc, backup) _, additional, err = a.Execute(pvc, backup)
require.NoError(t, err) require.NoError(t, err)