diff --git a/internal/cephfs/util.go b/internal/cephfs/util.go index 7db083f8f..c0e6a5a25 100644 --- a/internal/cephfs/util.go +++ b/internal/cephfs/util.go @@ -19,7 +19,6 @@ package cephfs import ( "context" "encoding/json" - "errors" "fmt" "time" @@ -135,8 +134,9 @@ func genSnapFromOptions(ctx context.Context, req *csi.CreateSnapshotRequest) (sn cephfsSnap.RequestName = req.GetName() snapOptions := req.GetParameters() - cephfsSnap.Monitors, cephfsSnap.ClusterID, err = getMonsAndClusterID(ctx, snapOptions) + cephfsSnap.Monitors, cephfsSnap.ClusterID, err = util.GetMonsAndClusterID(snapOptions) if err != nil { + klog.Errorf(util.Log(ctx, "failed getting mons (%s)"), err) return nil, err } if namePrefix, ok := snapOptions["snapshotNamePrefix"]; ok { @@ -145,23 +145,6 @@ func genSnapFromOptions(ctx context.Context, req *csi.CreateSnapshotRequest) (sn return cephfsSnap, nil } -func getMonsAndClusterID(ctx context.Context, options map[string]string) (monitors, clusterID string, err error) { - var ok bool - - if clusterID, ok = options["clusterID"]; !ok { - err = errors.New("clusterID must be set") - return - } - - if monitors, err = util.Mons(util.CsiConfigFile, clusterID); err != nil { - klog.Errorf(util.Log(ctx, "failed getting mons (%s)"), err) - err = fmt.Errorf("failed to fetch monitor list using clusterID (%s): %w", clusterID, err) - return - } - - return -} - func parseTime(ctx context.Context, createTime string) (*timestamp.Timestamp, error) { tm := ×tamp.Timestamp{} layout := "2006-01-02 15:04:05.000000" diff --git a/internal/rbd/rbd_util.go b/internal/rbd/rbd_util.go index d06681b8e..268cfd1f8 100644 --- a/internal/rbd/rbd_util.go +++ b/internal/rbd/rbd_util.go @@ -531,8 +531,9 @@ func genSnapFromSnapID(ctx context.Context, rbdSnap *rbdSnapshot, snapshotID str rbdSnap.ClusterID = vi.ClusterID options["clusterID"] = rbdSnap.ClusterID - rbdSnap.Monitors, _, err = getMonsAndClusterID(ctx, options) + rbdSnap.Monitors, _, err = util.GetMonsAndClusterID(options) if err != nil { + klog.Errorf(util.Log(ctx, "failed getting mons (%s)"), err) return err } @@ -593,8 +594,9 @@ func genVolFromVolID(ctx context.Context, volumeID string, cr *util.Credentials, rbdVol.ClusterID = vi.ClusterID options["clusterID"] = rbdVol.ClusterID - rbdVol.Monitors, _, err = getMonsAndClusterID(ctx, options) + rbdVol.Monitors, _, err = util.GetMonsAndClusterID(options) if err != nil { + klog.Errorf(util.Log(ctx, "failed getting mons (%s)"), err) return rbdVol, err } @@ -663,23 +665,6 @@ func genVolFromVolID(ctx context.Context, volumeID string, cr *util.Credentials, return rbdVol, err } -func getMonsAndClusterID(ctx context.Context, options map[string]string) (monitors, clusterID string, err error) { - var ok bool - - if clusterID, ok = options["clusterID"]; !ok { - err = errors.New("clusterID must be set") - return - } - - if monitors, err = util.Mons(util.CsiConfigFile, clusterID); err != nil { - klog.Errorf(util.Log(ctx, "failed getting mons (%s)"), err) - err = fmt.Errorf("failed to fetch monitor list using clusterID (%s): %w", clusterID, err) - return - } - - return -} - func genVolFromVolumeOptions(ctx context.Context, volOptions, credentials map[string]string, disableInUseChecks bool) (*rbdVolume, error) { var ( ok bool @@ -699,8 +684,9 @@ func genVolFromVolumeOptions(ctx context.Context, volOptions, credentials map[st rbdVol.NamePrefix = namePrefix } - rbdVol.Monitors, rbdVol.ClusterID, err = getMonsAndClusterID(ctx, volOptions) + rbdVol.Monitors, rbdVol.ClusterID, err = util.GetMonsAndClusterID(volOptions) if err != nil { + klog.Errorf(util.Log(ctx, "failed getting mons (%s)"), err) return nil, err } @@ -757,8 +743,9 @@ func genSnapFromOptions(ctx context.Context, rbdVol *rbdVolume, snapOptions map[ rbdSnap.Pool = rbdVol.Pool rbdSnap.JournalPool = rbdVol.JournalPool - rbdSnap.Monitors, rbdSnap.ClusterID, err = getMonsAndClusterID(ctx, snapOptions) + rbdSnap.Monitors, rbdSnap.ClusterID, err = util.GetMonsAndClusterID(snapOptions) if err != nil { + klog.Errorf(util.Log(ctx, "failed getting mons (%s)"), err) return nil, err } diff --git a/internal/util/csiconfig.go b/internal/util/csiconfig.go index d4cfec608..4b3d16ae7 100644 --- a/internal/util/csiconfig.go +++ b/internal/util/csiconfig.go @@ -18,6 +18,7 @@ package util import ( "encoding/json" + "errors" "fmt" "io/ioutil" "strings" @@ -111,3 +112,19 @@ func CephFSSubvolumeGroup(pathToConfig, clusterID string) (string, error) { } return cluster.CephFS.SubvolumeGroup, nil } + +// GetMonsAndClusterID returns monitors and clusterID information read from +// configfile. +func GetMonsAndClusterID(options map[string]string) (string, string, error) { + clusterID, ok := options["clusterID"] + if !ok { + return "", "", errors.New("clusterID must be set") + } + + monitors, err := Mons(CsiConfigFile, clusterID) + if err != nil { + return "", "", fmt.Errorf("failed to fetch monitor list using clusterID (%s): %w", clusterID, err) + } + + return monitors, clusterID, nil +}