2020-04-16 07:09:59 +00:00
|
|
|
package e2e
|
|
|
|
|
|
|
|
import (
|
2020-05-05 03:48:52 +00:00
|
|
|
"context"
|
2020-04-16 07:09:59 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-06-23 05:35:30 +00:00
|
|
|
snapapi "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
|
|
|
|
snapclient "github.com/kubernetes-csi/external-snapshotter/client/v4/clientset/versioned/typed/volumesnapshot/v1"
|
2020-04-16 07:09:59 +00:00
|
|
|
. "github.com/onsi/gomega" // nolint
|
|
|
|
apierrs "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
|
|
"k8s.io/kubernetes/test/e2e/framework"
|
|
|
|
e2elog "k8s.io/kubernetes/test/e2e/framework/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getSnapshotClass(path string) snapapi.VolumeSnapshotClass {
|
|
|
|
sc := snapapi.VolumeSnapshotClass{}
|
|
|
|
err := unmarshal(path, &sc)
|
|
|
|
Expect(err).Should(BeNil())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-04-16 07:09:59 +00:00
|
|
|
return sc
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSnapshot(path string) snapapi.VolumeSnapshot {
|
|
|
|
sc := snapapi.VolumeSnapshot{}
|
|
|
|
err := unmarshal(path, &sc)
|
|
|
|
Expect(err).Should(BeNil())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-04-16 07:09:59 +00:00
|
|
|
return sc
|
|
|
|
}
|
|
|
|
|
2021-06-23 05:35:30 +00:00
|
|
|
func newSnapshotClient() (*snapclient.SnapshotV1Client, error) {
|
2020-04-16 07:09:59 +00:00
|
|
|
config, err := framework.LoadConfig()
|
|
|
|
if err != nil {
|
2021-06-25 11:19:37 +00:00
|
|
|
return nil, fmt.Errorf("error creating client: %w", err)
|
2020-04-16 07:09:59 +00:00
|
|
|
}
|
|
|
|
c, err := snapclient.NewForConfig(config)
|
|
|
|
if err != nil {
|
2021-06-25 11:19:37 +00:00
|
|
|
return nil, fmt.Errorf("error creating snapshot client: %w", err)
|
2020-04-16 07:09:59 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-04-16 07:09:59 +00:00
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func createSnapshot(snap *snapapi.VolumeSnapshot, t int) error {
|
|
|
|
sclient, err := newSnapshotClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-25 13:02:50 +00:00
|
|
|
|
|
|
|
_, err = sclient.
|
|
|
|
VolumeSnapshots(snap.Namespace).
|
|
|
|
Create(context.TODO(), snap, metav1.CreateOptions{})
|
2020-04-16 07:09:59 +00:00
|
|
|
if err != nil {
|
2021-05-11 09:28:56 +00:00
|
|
|
return fmt.Errorf("failed to create volumesnapshot: %w", err)
|
2020-04-16 07:09:59 +00:00
|
|
|
}
|
|
|
|
e2elog.Logf("snapshot with name %v created in %v namespace", snap.Name, snap.Namespace)
|
|
|
|
|
|
|
|
timeout := time.Duration(t) * time.Minute
|
|
|
|
name := snap.Name
|
|
|
|
start := time.Now()
|
2020-05-15 08:34:59 +00:00
|
|
|
e2elog.Logf("waiting for %v to be in ready state", snap)
|
2020-04-16 07:09:59 +00:00
|
|
|
|
|
|
|
return wait.PollImmediate(poll, timeout, func() (bool, error) {
|
|
|
|
e2elog.Logf("waiting for snapshot %s (%d seconds elapsed)", snap.Name, int(time.Since(start).Seconds()))
|
2021-06-25 13:02:50 +00:00
|
|
|
snaps, err := sclient.
|
|
|
|
VolumeSnapshots(snap.Namespace).
|
|
|
|
Get(context.TODO(), name, metav1.GetOptions{})
|
2020-04-16 07:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
e2elog.Logf("Error getting snapshot in namespace: '%s': %v", snap.Namespace, err)
|
2020-12-17 13:00:02 +00:00
|
|
|
if isRetryableAPIError(err) {
|
2020-04-16 07:09:59 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if apierrs.IsNotFound(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-05-11 09:28:56 +00:00
|
|
|
return false, fmt.Errorf("failed to get volumesnapshot: %w", err)
|
2020-04-16 07:09:59 +00:00
|
|
|
}
|
|
|
|
if snaps.Status == nil || snaps.Status.ReadyToUse == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if *snaps.Status.ReadyToUse {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
e2elog.Logf("snapshot %s in %v state", snap.Name, *snaps.Status.ReadyToUse)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-04-16 07:09:59 +00:00
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteSnapshot(snap *snapapi.VolumeSnapshot, t int) error {
|
|
|
|
sclient, err := newSnapshotClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-25 13:02:50 +00:00
|
|
|
|
|
|
|
err = sclient.
|
|
|
|
VolumeSnapshots(snap.Namespace).
|
|
|
|
Delete(context.TODO(), snap.Name, metav1.DeleteOptions{})
|
2020-04-16 07:09:59 +00:00
|
|
|
if err != nil {
|
2021-05-11 09:28:56 +00:00
|
|
|
return fmt.Errorf("failed to delete volumesnapshot: %w", err)
|
2020-04-16 07:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
timeout := time.Duration(t) * time.Minute
|
|
|
|
name := snap.Name
|
|
|
|
start := time.Now()
|
|
|
|
e2elog.Logf("Waiting up to %v to be deleted", snap)
|
|
|
|
|
|
|
|
return wait.PollImmediate(poll, timeout, func() (bool, error) {
|
|
|
|
e2elog.Logf("deleting snapshot %s (%d seconds elapsed)", name, int(time.Since(start).Seconds()))
|
2021-06-25 13:02:50 +00:00
|
|
|
_, err := sclient.
|
|
|
|
VolumeSnapshots(snap.Namespace).
|
|
|
|
Get(context.TODO(), name, metav1.GetOptions{})
|
2020-04-16 07:09:59 +00:00
|
|
|
if err == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-06-29 05:43:53 +00:00
|
|
|
if isRetryableAPIError(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-04-16 07:09:59 +00:00
|
|
|
if !apierrs.IsNotFound(err) {
|
2021-06-25 13:02:50 +00:00
|
|
|
return false, fmt.Errorf(
|
2021-07-10 10:45:11 +00:00
|
|
|
"get on deleted snapshot %v failed with error other than \"not found\": %w",
|
2021-06-25 13:02:50 +00:00
|
|
|
name,
|
|
|
|
err)
|
2020-04-16 07:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
func createRBDSnapshotClass(f *framework.Framework) error {
|
2020-04-16 07:09:59 +00:00
|
|
|
scPath := fmt.Sprintf("%s/%s", rbdExamplePath, "snapshotclass.yaml")
|
|
|
|
sc := getSnapshotClass(scPath)
|
|
|
|
|
|
|
|
sc.Parameters["csi.storage.k8s.io/snapshotter-secret-namespace"] = cephCSINamespace
|
2021-02-22 11:13:40 +00:00
|
|
|
sc.Parameters["csi.storage.k8s.io/snapshotter-secret-name"] = rbdProvisionerSecretName
|
2020-04-16 07:09:59 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
fsID, stdErr, err := execCommandInToolBoxPod(f, "ceph fsid", rookNamespace)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if stdErr != "" {
|
|
|
|
return fmt.Errorf("failed to get fsid from ceph cluster %s", stdErr)
|
|
|
|
}
|
2020-04-16 07:09:59 +00:00
|
|
|
fsID = strings.Trim(fsID, "\n")
|
|
|
|
sc.Parameters["clusterID"] = fsID
|
|
|
|
sclient, err := newSnapshotClient()
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-23 05:35:30 +00:00
|
|
|
_, err = sclient.VolumeSnapshotClasses().Create(context.TODO(), &sc, metav1.CreateOptions{})
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return err
|
2020-04-16 07:09:59 +00:00
|
|
|
}
|
2020-08-03 17:00:27 +00:00
|
|
|
|
2021-04-08 15:46:11 +00:00
|
|
|
func deleteRBDSnapshotClass() error {
|
|
|
|
scPath := fmt.Sprintf("%s/%s", rbdExamplePath, "snapshotclass.yaml")
|
|
|
|
sc := getSnapshotClass(scPath)
|
|
|
|
|
|
|
|
sclient, err := newSnapshotClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-06-23 05:35:30 +00:00
|
|
|
return sclient.VolumeSnapshotClasses().Delete(context.TODO(), sc.Name, metav1.DeleteOptions{})
|
2021-04-08 15:46:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
func createCephFSSnapshotClass(f *framework.Framework) error {
|
2021-09-20 10:16:55 +00:00
|
|
|
scPath := fmt.Sprintf("%s/%s", cephFSExamplePath, "snapshotclass.yaml")
|
2020-08-03 17:00:27 +00:00
|
|
|
sc := getSnapshotClass(scPath)
|
|
|
|
sc.Parameters["csi.storage.k8s.io/snapshotter-secret-namespace"] = cephCSINamespace
|
2021-02-22 11:13:40 +00:00
|
|
|
sc.Parameters["csi.storage.k8s.io/snapshotter-secret-name"] = cephFSProvisionerSecretName
|
2020-09-03 09:34:29 +00:00
|
|
|
fsID, stdErr, err := execCommandInToolBoxPod(f, "ceph fsid", rookNamespace)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if stdErr != "" {
|
|
|
|
return fmt.Errorf("failed to get fsid from ceph cluster %s", stdErr)
|
|
|
|
}
|
2020-08-03 17:00:27 +00:00
|
|
|
fsID = strings.Trim(fsID, "\n")
|
|
|
|
sc.Parameters["clusterID"] = fsID
|
|
|
|
sclient, err := newSnapshotClient()
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-23 05:35:30 +00:00
|
|
|
_, err = sclient.VolumeSnapshotClasses().Create(context.TODO(), &sc, metav1.CreateOptions{})
|
2021-05-11 09:28:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create volumesnapshotclass: %w", err)
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return err
|
2020-08-03 17:00:27 +00:00
|
|
|
}
|
2021-04-26 13:15:18 +00:00
|
|
|
|
|
|
|
func getVolumeSnapshotContent(namespace, snapshotName string) (*snapapi.VolumeSnapshotContent, error) {
|
|
|
|
sclient, err := newSnapshotClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 13:02:50 +00:00
|
|
|
|
|
|
|
snapshot, err := sclient.
|
|
|
|
VolumeSnapshots(namespace).
|
|
|
|
Get(context.TODO(), snapshotName, metav1.GetOptions{})
|
2021-04-26 13:15:18 +00:00
|
|
|
if err != nil {
|
2021-05-11 09:28:56 +00:00
|
|
|
return nil, fmt.Errorf("failed to get volumesnapshot: %w", err)
|
2021-04-26 13:15:18 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 13:02:50 +00:00
|
|
|
volumeSnapshotContent, err := sclient.
|
|
|
|
VolumeSnapshotContents().
|
|
|
|
Get(context.TODO(), *snapshot.Status.BoundVolumeSnapshotContentName, metav1.GetOptions{})
|
2021-04-26 13:15:18 +00:00
|
|
|
if err != nil {
|
2021-05-11 09:28:56 +00:00
|
|
|
return nil, fmt.Errorf("failed to get volumesnapshotcontent: %w", err)
|
2021-04-26 13:15:18 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-04-26 13:15:18 +00:00
|
|
|
return volumeSnapshotContent, nil
|
|
|
|
}
|