2021-12-21 14:23:26 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
package e2e
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-02-10 08:19:40 +00:00
|
|
|
"regexp"
|
2020-09-03 09:34:29 +00:00
|
|
|
"strings"
|
2022-02-14 15:55:43 +00:00
|
|
|
"time"
|
2020-09-03 09:34:29 +00:00
|
|
|
|
2021-06-23 07:35:24 +00:00
|
|
|
snapapi "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
|
2020-09-03 09:34:29 +00:00
|
|
|
v1 "k8s.io/api/core/v1"
|
2022-02-14 15:55:43 +00:00
|
|
|
apierrs "k8s.io/apimachinery/pkg/api/errors"
|
2020-09-03 09:34:29 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2022-02-14 15:55:43 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2020-09-03 09:34:29 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/kubernetes/test/e2e/framework"
|
2021-02-10 08:19:40 +00:00
|
|
|
e2elog "k8s.io/kubernetes/test/e2e/framework/log"
|
2020-09-03 09:34:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
adminUser = "admin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// validateSubvolumegroup validates whether subvolumegroup is present.
|
|
|
|
func validateSubvolumegroup(f *framework.Framework, subvolgrp string) error {
|
|
|
|
cmd := fmt.Sprintf("ceph fs subvolumegroup getpath myfs %s", subvolgrp)
|
|
|
|
stdOut, stdErr, err := execCommandInToolBoxPod(f, cmd, rookNamespace)
|
|
|
|
if err != nil {
|
2021-05-11 09:28:56 +00:00
|
|
|
return fmt.Errorf("failed to exec command in toolbox: %w", err)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
|
|
|
if stdErr != "" {
|
2021-11-22 06:56:06 +00:00
|
|
|
return fmt.Errorf("failed to getpath for subvolumegroup %s : %v", subvolgrp, stdErr)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
|
|
|
expectedGrpPath := "/volumes/" + subvolgrp
|
|
|
|
stdOut = strings.TrimSpace(stdOut)
|
|
|
|
if stdOut != expectedGrpPath {
|
|
|
|
return fmt.Errorf("error unexpected group path. Found: %s", stdOut)
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-25 12:31:28 +00:00
|
|
|
func createCephfsStorageClass(
|
|
|
|
c kubernetes.Interface,
|
|
|
|
f *framework.Framework,
|
|
|
|
enablePool bool,
|
|
|
|
params map[string]string) error {
|
2021-09-20 10:16:55 +00:00
|
|
|
scPath := fmt.Sprintf("%s/%s", cephFSExamplePath, "storageclass.yaml")
|
2020-09-03 09:34:29 +00:00
|
|
|
sc, err := getStorageClass(scPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sc.Parameters["fsName"] = "myfs"
|
2021-02-22 11:13:40 +00:00
|
|
|
sc.Parameters["csi.storage.k8s.io/provisioner-secret-namespace"] = cephCSINamespace
|
|
|
|
sc.Parameters["csi.storage.k8s.io/provisioner-secret-name"] = cephFSProvisionerSecretName
|
2020-09-03 09:34:29 +00:00
|
|
|
|
2021-02-22 11:13:40 +00:00
|
|
|
sc.Parameters["csi.storage.k8s.io/controller-expand-secret-namespace"] = cephCSINamespace
|
|
|
|
sc.Parameters["csi.storage.k8s.io/controller-expand-secret-name"] = cephFSProvisionerSecretName
|
2020-09-03 09:34:29 +00:00
|
|
|
|
2021-02-22 11:13:40 +00:00
|
|
|
sc.Parameters["csi.storage.k8s.io/node-stage-secret-namespace"] = cephCSINamespace
|
|
|
|
sc.Parameters["csi.storage.k8s.io/node-stage-secret-name"] = cephFSNodePluginSecretName
|
2020-09-03 09:34:29 +00:00
|
|
|
|
|
|
|
if enablePool {
|
2022-01-25 07:53:04 +00:00
|
|
|
sc.Parameters["pool"] = "myfs-replicated"
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
2020-09-21 14:56:15 +00:00
|
|
|
|
|
|
|
// overload any parameters that were passed
|
|
|
|
if params == nil {
|
|
|
|
// create an empty params, so that params["clusterID"] below
|
|
|
|
// does not panic
|
|
|
|
params = map[string]string{}
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
2020-09-21 14:56:15 +00:00
|
|
|
for param, value := range params {
|
|
|
|
sc.Parameters[param] = value
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
2020-09-21 14:56:15 +00:00
|
|
|
|
|
|
|
// fetch and set fsID from the cluster if not set in params
|
|
|
|
if _, found := params["clusterID"]; !found {
|
|
|
|
fsID, stdErr, failErr := execCommandInToolBoxPod(f, "ceph fsid", rookNamespace)
|
|
|
|
if failErr != nil {
|
|
|
|
return failErr
|
|
|
|
}
|
|
|
|
if stdErr != "" {
|
|
|
|
return fmt.Errorf("error getting fsid %v", stdErr)
|
|
|
|
}
|
|
|
|
sc.Parameters["clusterID"] = strings.Trim(fsID, "\n")
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
|
|
|
sc.Namespace = cephCSINamespace
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2022-02-14 15:55:43 +00:00
|
|
|
timeout := time.Duration(deployTimeout) * time.Minute
|
2022-02-14 15:56:15 +00:00
|
|
|
|
2022-02-14 15:55:43 +00:00
|
|
|
return wait.PollImmediate(poll, timeout, func() (bool, error) {
|
|
|
|
_, err = c.StorageV1().StorageClasses().Create(context.TODO(), &sc, metav1.CreateOptions{})
|
|
|
|
if err != nil {
|
|
|
|
e2elog.Logf("error creating StorageClass %q in namespace %q: %v", sc.Name, sc.Namespace, err)
|
|
|
|
if isRetryableAPIError(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if apierrs.IsNotFound(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, fmt.Errorf("failed to create StorageClass %q: %w", sc.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
})
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 11:13:40 +00:00
|
|
|
func createCephfsSecret(f *framework.Framework, secretName, userName, userKey string) error {
|
2021-09-20 10:16:55 +00:00
|
|
|
scPath := fmt.Sprintf("%s/%s", cephFSExamplePath, "secret.yaml")
|
2020-09-03 09:34:29 +00:00
|
|
|
sc, err := getSecret(scPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-22 11:13:40 +00:00
|
|
|
if secretName != "" {
|
|
|
|
sc.Name = secretName
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
2021-02-22 11:13:40 +00:00
|
|
|
sc.StringData["adminID"] = userName
|
|
|
|
sc.StringData["adminKey"] = userKey
|
2020-09-03 09:34:29 +00:00
|
|
|
delete(sc.StringData, "userID")
|
|
|
|
delete(sc.StringData, "userKey")
|
|
|
|
sc.Namespace = cephCSINamespace
|
2021-02-22 11:13:40 +00:00
|
|
|
_, err = f.ClientSet.CoreV1().Secrets(cephCSINamespace).Create(context.TODO(), &sc, metav1.CreateOptions{})
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-22 06:17:42 +00:00
|
|
|
// unmountCephFSVolume unmounts a cephFS volume mounted on a pod.
|
|
|
|
func unmountCephFSVolume(f *framework.Framework, appName, pvcName string) error {
|
|
|
|
pod, err := f.ClientSet.CoreV1().Pods(f.UniqueName).Get(context.TODO(), appName, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
e2elog.Logf("Error occurred getting pod %s in namespace %s", appName, f.UniqueName)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-05-11 09:28:56 +00:00
|
|
|
return fmt.Errorf("failed to get pod: %w", err)
|
2021-03-22 06:17:42 +00:00
|
|
|
}
|
2021-06-25 12:31:28 +00:00
|
|
|
pvc, err := f.ClientSet.CoreV1().
|
|
|
|
PersistentVolumeClaims(f.UniqueName).
|
|
|
|
Get(context.TODO(), pvcName, metav1.GetOptions{})
|
2021-03-22 06:17:42 +00:00
|
|
|
if err != nil {
|
|
|
|
e2elog.Logf("Error occurred getting PVC %s in namespace %s", pvcName, f.UniqueName)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-05-11 09:28:56 +00:00
|
|
|
return fmt.Errorf("failed to get pvc: %w", err)
|
2021-03-22 06:17:42 +00:00
|
|
|
}
|
2021-06-25 12:31:28 +00:00
|
|
|
cmd := fmt.Sprintf(
|
|
|
|
"umount /var/lib/kubelet/pods/%s/volumes/kubernetes.io~csi/%s/mount",
|
|
|
|
pod.UID,
|
|
|
|
pvc.Spec.VolumeName)
|
|
|
|
_, stdErr, err := execCommandInDaemonsetPod(
|
|
|
|
f,
|
|
|
|
cmd,
|
2021-09-20 10:16:55 +00:00
|
|
|
cephFSDeamonSetName,
|
2021-06-25 12:31:28 +00:00
|
|
|
pod.Spec.NodeName,
|
2021-09-20 10:16:55 +00:00
|
|
|
cephFSContainerName,
|
2021-06-25 12:31:28 +00:00
|
|
|
cephCSINamespace)
|
2021-03-22 06:17:42 +00:00
|
|
|
if stdErr != "" {
|
|
|
|
e2elog.Logf("StdErr occurred: %s", stdErr)
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-22 06:17:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
func deleteBackingCephFSVolume(f *framework.Framework, pvc *v1.PersistentVolumeClaim) error {
|
|
|
|
imageData, err := getImageInfoFromPVC(pvc.Namespace, pvc.Name, f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-10 08:19:40 +00:00
|
|
|
cmd := fmt.Sprintf("ceph fs subvolume rm %s %s %s", fileSystemName, imageData.imageName, subvolumegroup)
|
|
|
|
_, stdErr, err := execCommandInToolBoxPod(f, cmd, rookNamespace)
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if stdErr != "" {
|
|
|
|
return fmt.Errorf("error deleting backing volume %s %v", imageData.imageName, stdErr)
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type cephfsSubVolume struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func listCephFSSubVolumes(f *framework.Framework, filesystem, groupname string) ([]cephfsSubVolume, error) {
|
|
|
|
var subVols []cephfsSubVolume
|
2021-06-25 12:31:28 +00:00
|
|
|
stdout, stdErr, err := execCommandInToolBoxPod(
|
|
|
|
f,
|
|
|
|
fmt.Sprintf("ceph fs subvolume ls %s --group_name=%s --format=json", filesystem, groupname),
|
|
|
|
rookNamespace)
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return subVols, err
|
|
|
|
}
|
|
|
|
if stdErr != "" {
|
|
|
|
return subVols, fmt.Errorf("error listing subolumes %v", stdErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(stdout), &subVols)
|
|
|
|
if err != nil {
|
|
|
|
return subVols, err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return subVols, nil
|
|
|
|
}
|
2021-02-09 10:32:40 +00:00
|
|
|
|
|
|
|
// getSubvolumepath validates whether subvolumegroup is present.
|
|
|
|
func getSubvolumePath(f *framework.Framework, filesystem, subvolgrp, subvolume string) (string, error) {
|
|
|
|
cmd := fmt.Sprintf("ceph fs subvolume getpath %s %s --group_name=%s", filesystem, subvolume, subvolgrp)
|
|
|
|
stdOut, stdErr, err := execCommandInToolBoxPod(f, cmd, rookNamespace)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if stdErr != "" {
|
2021-11-22 06:56:06 +00:00
|
|
|
return "", fmt.Errorf("failed to getpath for subvolume %s : %s", subvolume, stdErr)
|
2021-02-09 10:32:40 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-09 10:32:40 +00:00
|
|
|
return strings.TrimSpace(stdOut), nil
|
|
|
|
}
|
2021-02-10 08:19:40 +00:00
|
|
|
|
|
|
|
func getSnapName(snapNamespace, snapName string) (string, error) {
|
|
|
|
sclient, err := newSnapshotClient()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-06-25 12:31:28 +00:00
|
|
|
snap, err := sclient.
|
|
|
|
VolumeSnapshots(snapNamespace).
|
|
|
|
Get(context.TODO(), snapName, metav1.GetOptions{})
|
2021-02-10 08:19:40 +00:00
|
|
|
if err != nil {
|
2021-05-11 09:28:56 +00:00
|
|
|
return "", fmt.Errorf("failed to get volumesnapshot: %w", err)
|
2021-02-10 08:19:40 +00:00
|
|
|
}
|
2021-06-25 12:31:28 +00:00
|
|
|
sc, err := sclient.
|
|
|
|
VolumeSnapshotContents().
|
|
|
|
Get(context.TODO(), *snap.Status.BoundVolumeSnapshotContentName, metav1.GetOptions{})
|
2021-02-10 08:19:40 +00:00
|
|
|
if err != nil {
|
2021-05-11 09:28:56 +00:00
|
|
|
return "", fmt.Errorf("failed to get volumesnapshotcontent: %w", err)
|
2021-02-10 08:19:40 +00:00
|
|
|
}
|
|
|
|
snapIDRegex := regexp.MustCompile(`(\w+\-?){5}$`)
|
|
|
|
snapID := snapIDRegex.FindString(*sc.Status.SnapshotHandle)
|
|
|
|
snapshotName := fmt.Sprintf("csi-snap-%s", snapID)
|
|
|
|
e2elog.Logf("snapshotName= %s", snapshotName)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-10 08:19:40 +00:00
|
|
|
return snapshotName, nil
|
|
|
|
}
|
|
|
|
|
2021-06-25 12:31:28 +00:00
|
|
|
func deleteBackingCephFSSubvolumeSnapshot(
|
|
|
|
f *framework.Framework,
|
|
|
|
pvc *v1.PersistentVolumeClaim,
|
|
|
|
snap *snapapi.VolumeSnapshot) error {
|
2021-02-10 08:19:40 +00:00
|
|
|
snapshotName, err := getSnapName(snap.Namespace, snap.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
imageData, err := getImageInfoFromPVC(pvc.Namespace, pvc.Name, f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-25 12:31:28 +00:00
|
|
|
cmd := fmt.Sprintf(
|
|
|
|
"ceph fs subvolume snapshot rm %s %s %s %s",
|
|
|
|
fileSystemName,
|
|
|
|
imageData.imageName,
|
|
|
|
snapshotName,
|
|
|
|
subvolumegroup)
|
2021-02-10 08:19:40 +00:00
|
|
|
_, stdErr, err := execCommandInToolBoxPod(f, cmd, rookNamespace)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if stdErr != "" {
|
|
|
|
return fmt.Errorf("error deleting backing snapshot %s %v", snapshotName, stdErr)
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-10 08:19:40 +00:00
|
|
|
return nil
|
|
|
|
}
|