data mover fail earlier for snapshot creation error

Signed-off-by: Lyndon-Li <lyonghui@vmware.com>
This commit is contained in:
Lyndon-Li
2023-08-08 12:05:56 +08:00
parent f78dd073bf
commit 22c88ba330
3 changed files with 78 additions and 3 deletions

View File

@@ -29,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"github.com/vmware-tanzu/velero/pkg/util/boolptr"
"github.com/vmware-tanzu/velero/pkg/util/stringptr"
snapshotv1api "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
snapshotter "github.com/kubernetes-csi/external-snapshotter/client/v4/clientset/versioned/typed/volumesnapshot/v1"
@@ -53,7 +54,15 @@ func WaitVolumeSnapshotReady(ctx context.Context, snapshotClient snapshotter.Sna
return false, errors.Wrapf(err, fmt.Sprintf("error to get volumesnapshot %s/%s", volumeSnapshotNS, volumeSnapshot))
}
if tmpVS.Status == nil || tmpVS.Status.BoundVolumeSnapshotContentName == nil || !boolptr.IsSetToTrue(tmpVS.Status.ReadyToUse) || tmpVS.Status.RestoreSize == nil {
if tmpVS.Status == nil {
return false, nil
}
if tmpVS.Status.Error != nil {
return false, errors.Errorf("volume snapshot creation error %s", stringptr.GetString(tmpVS.Status.Error.Message))
}
if !boolptr.IsSetToTrue(tmpVS.Status.ReadyToUse) {
return false, nil
}

View File

@@ -31,6 +31,7 @@ import (
clientTesting "k8s.io/client-go/testing"
"github.com/vmware-tanzu/velero/pkg/util/boolptr"
"github.com/vmware-tanzu/velero/pkg/util/stringptr"
velerotest "github.com/vmware-tanzu/velero/pkg/test"
)
@@ -55,6 +56,8 @@ func TestWaitVolumeSnapshotReady(t *testing.T) {
},
}
errMessage := "fake-snapshot-creation-error"
tests := []struct {
name string
clientObj []runtime.Object
@@ -116,7 +119,7 @@ func TestWaitVolumeSnapshotReady(t *testing.T) {
err: "timed out waiting for the condition",
},
{
name: "restore size is nil in status",
name: "ready to use is false",
vsName: "fake-vs",
namespace: "fake-ns",
clientObj: []runtime.Object{
@@ -127,12 +130,48 @@ func TestWaitVolumeSnapshotReady(t *testing.T) {
},
Status: &snapshotv1api.VolumeSnapshotStatus{
BoundVolumeSnapshotContentName: &vscName,
ReadyToUse: boolptr.True(),
ReadyToUse: boolptr.False(),
},
},
},
err: "timed out waiting for the condition",
},
{
name: "snapshot creation error with message",
vsName: "fake-vs",
namespace: "fake-ns",
clientObj: []runtime.Object{
&snapshotv1api.VolumeSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-vs",
Namespace: "fake-ns",
},
Status: &snapshotv1api.VolumeSnapshotStatus{
Error: &snapshotv1api.VolumeSnapshotError{
Message: &errMessage,
},
},
},
},
err: "volume snapshot creation error fake-snapshot-creation-error",
},
{
name: "snapshot creation error without message",
vsName: "fake-vs",
namespace: "fake-ns",
clientObj: []runtime.Object{
&snapshotv1api.VolumeSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-vs",
Namespace: "fake-ns",
},
Status: &snapshotv1api.VolumeSnapshotStatus{
Error: &snapshotv1api.VolumeSnapshotError{},
},
},
},
err: "volume snapshot creation error " + stringptr.NilString,
},
{
name: "success",
vsName: "fake-vs",

View File

@@ -0,0 +1,27 @@
/*
Copyright 2017 the Velero contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package stringptr
const NilString = "<nil>"
func GetString(str *string) string {
if str == nil {
return NilString
} else {
return *str
}
}