e2e: introduce getClusterID() helper

There are many locations where the cluster-id (`ceph fsid`) is obtained
from the Rook Toolbox. Instead of duplicating the code everywhere, use a
new helper function getClusterID().

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2022-04-08 13:08:32 +02:00 committed by mergify[bot]
parent b20e3fa784
commit e30364cb4d
6 changed files with 34 additions and 51 deletions

View File

@ -92,14 +92,12 @@ func createCephfsStorageClass(
// fetch and set fsID from the cluster if not set in params // fetch and set fsID from the cluster if not set in params
if _, found := params["clusterID"]; !found { if _, found := params["clusterID"]; !found {
fsID, stdErr, failErr := execCommandInToolBoxPod(f, "ceph fsid", rookNamespace) var fsID string
if failErr != nil { fsID, err = getClusterID(f)
return failErr if err != nil {
return fmt.Errorf("failed to get clusterID: %w", err)
} }
if stdErr != "" { sc.Parameters["clusterID"] = fsID
return fmt.Errorf("error getting fsid %v", stdErr)
}
sc.Parameters["clusterID"] = strings.Trim(fsID, "\n")
} }
sc.Namespace = cephCSINamespace sc.Namespace = cephCSINamespace

View File

@ -20,7 +20,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"strings"
"github.com/ceph/ceph-csi/internal/util" "github.com/ceph/ceph-csi/internal/util"
@ -45,15 +44,11 @@ func createConfigMap(pluginPath string, c kubernetes.Interface, f *framework.Fra
return err return err
} }
fsID, stdErr, err := execCommandInToolBoxPod(f, "ceph fsid", rookNamespace) fsID, err := getClusterID(f)
if err != nil { if err != nil {
return fmt.Errorf("failed to exec command in toolbox: %w", err) return fmt.Errorf("failed to get clusterID: %w", err)
} }
if stdErr != "" {
return fmt.Errorf("error getting fsid %v", stdErr)
}
// remove new line present in fsID
fsID = strings.Trim(fsID, "\n")
// get mon list // get mon list
mons, err := getMons(rookNamespace, c) mons, err := getMons(rookNamespace, c)
if err != nil { if err != nil {

View File

@ -136,15 +136,10 @@ func createRBDStorageClass(
sc.Parameters["csi.storage.k8s.io/node-stage-secret-namespace"] = cephCSINamespace sc.Parameters["csi.storage.k8s.io/node-stage-secret-namespace"] = cephCSINamespace
sc.Parameters["csi.storage.k8s.io/node-stage-secret-name"] = rbdNodePluginSecretName sc.Parameters["csi.storage.k8s.io/node-stage-secret-name"] = rbdNodePluginSecretName
fsID, stdErr, err := execCommandInToolBoxPod(f, "ceph fsid", rookNamespace) fsID, err := getClusterID(f)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to get clusterID: %w", err)
} }
if stdErr != "" {
return fmt.Errorf("error getting fsid %v", stdErr)
}
// remove new line present in fsID
fsID = strings.Trim(fsID, "\n")
sc.Parameters["clusterID"] = fsID sc.Parameters["clusterID"] = fsID
for k, v := range parameters { for k, v := range parameters {

View File

@ -19,7 +19,6 @@ package e2e
import ( import (
"context" "context"
"fmt" "fmt"
"strings"
"time" "time"
snapapi "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1" snapapi "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
@ -158,14 +157,10 @@ func createRBDSnapshotClass(f *framework.Framework) error {
sc.Parameters["csi.storage.k8s.io/snapshotter-secret-namespace"] = cephCSINamespace sc.Parameters["csi.storage.k8s.io/snapshotter-secret-namespace"] = cephCSINamespace
sc.Parameters["csi.storage.k8s.io/snapshotter-secret-name"] = rbdProvisionerSecretName sc.Parameters["csi.storage.k8s.io/snapshotter-secret-name"] = rbdProvisionerSecretName
fsID, stdErr, err := execCommandInToolBoxPod(f, "ceph fsid", rookNamespace) fsID, err := getClusterID(f)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to get clusterID: %w", err)
} }
if stdErr != "" {
return fmt.Errorf("failed to get fsid from ceph cluster %s", stdErr)
}
fsID = strings.Trim(fsID, "\n")
sc.Parameters["clusterID"] = fsID sc.Parameters["clusterID"] = fsID
sclient, err := newSnapshotClient() sclient, err := newSnapshotClient()
if err != nil { if err != nil {
@ -193,14 +188,11 @@ func createCephFSSnapshotClass(f *framework.Framework) error {
sc := getSnapshotClass(scPath) sc := getSnapshotClass(scPath)
sc.Parameters["csi.storage.k8s.io/snapshotter-secret-namespace"] = cephCSINamespace sc.Parameters["csi.storage.k8s.io/snapshotter-secret-namespace"] = cephCSINamespace
sc.Parameters["csi.storage.k8s.io/snapshotter-secret-name"] = cephFSProvisionerSecretName sc.Parameters["csi.storage.k8s.io/snapshotter-secret-name"] = cephFSProvisionerSecretName
fsID, stdErr, err := execCommandInToolBoxPod(f, "ceph fsid", rookNamespace)
fsID, err := getClusterID(f)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to get clusterID: %w", err)
} }
if stdErr != "" {
return fmt.Errorf("failed to get fsid from ceph cluster %s", stdErr)
}
fsID = strings.Trim(fsID, "\n")
sc.Parameters["clusterID"] = fsID sc.Parameters["clusterID"] = fsID
sclient, err := newSnapshotClient() sclient, err := newSnapshotClient()
if err != nil { if err != nil {

View File

@ -127,15 +127,11 @@ func validateRBDStaticPV(f *framework.Framework, appPath string, isBlock, checkI
c := f.ClientSet c := f.ClientSet
fsID, e, err := execCommandInToolBoxPod(f, "ceph fsid", rookNamespace) fsID, err := getClusterID(f)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to get clusterID: %w", err)
} }
if e != "" {
return fmt.Errorf("failed to get fsid from ceph cluster %s", e)
}
// remove new line present in fsID
fsID = strings.Trim(fsID, "\n")
size := staticPVSize size := staticPVSize
// create rbd image // create rbd image
cmd := fmt.Sprintf( cmd := fmt.Sprintf(
@ -144,7 +140,7 @@ func validateRBDStaticPV(f *framework.Framework, appPath string, isBlock, checkI
staticPVSize, staticPVSize,
rbdOptions(defaultRBDPool)) rbdOptions(defaultRBDPool))
_, e, err = execCommandInToolBoxPod(f, cmd, rookNamespace) _, e, err := execCommandInToolBoxPod(f, cmd, rookNamespace)
if err != nil { if err != nil {
return err return err
} }
@ -346,15 +342,10 @@ func validateCephFsStaticPV(f *framework.Framework, appPath, scPath string) erro
LabelSelector: "app=rook-ceph-tools", LabelSelector: "app=rook-ceph-tools",
} }
fsID, e, err := execCommandInPod(f, "ceph fsid", rookNamespace, &listOpt) fsID, err := getClusterID(f)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to get clusterID: %w", err)
} }
if e != "" {
return fmt.Errorf("failed to get fsid from ceph cluster %s", e)
}
// remove new line present in fsID
fsID = strings.Trim(fsID, "\n")
// 4GiB in bytes // 4GiB in bytes
size := "4294967296" size := "4294967296"
@ -362,7 +353,7 @@ func validateCephFsStaticPV(f *framework.Framework, appPath, scPath string) erro
// create subvolumegroup, command will work even if group is already present. // create subvolumegroup, command will work even if group is already present.
cmd := fmt.Sprintf("ceph fs subvolumegroup create %s %s", fileSystemName, groupName) cmd := fmt.Sprintf("ceph fs subvolumegroup create %s %s", fileSystemName, groupName)
_, e, err = execCommandInPod(f, cmd, rookNamespace, &listOpt) _, e, err := execCommandInPod(f, cmd, rookNamespace, &listOpt)
if err != nil { if err != nil {
return err return err
} }

View File

@ -121,6 +121,18 @@ func getMonsHash(mons string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(mons))) //nolint:gosec // hash generation return fmt.Sprintf("%x", md5.Sum([]byte(mons))) //nolint:gosec // hash generation
} }
func getClusterID(f *framework.Framework) (string, error) {
fsID, stdErr, err := execCommandInToolBoxPod(f, "ceph fsid", rookNamespace)
if err != nil {
return "", fmt.Errorf("failed getting clusterID through toolbox: %w", err)
}
if stdErr != "" {
return "", fmt.Errorf("error getting fsid: %s", stdErr)
}
// remove new line present in fsID
return strings.Trim(fsID, "\n"), nil
}
func getStorageClass(path string) (scv1.StorageClass, error) { func getStorageClass(path string) (scv1.StorageClass, error) {
sc := scv1.StorageClass{} sc := scv1.StorageClass{}
err := unmarshal(path, &sc) err := unmarshal(path, &sc)