util: move getMonsAndClusterID to util

as we had duplicate functions in both cephfs
and rbd this commit moves the function to util.

Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
This commit is contained in:
Madhu Rajanna 2020-08-10 11:57:28 +05:30 committed by mergify[bot]
parent 18f4e9d519
commit bfde065f92
3 changed files with 27 additions and 40 deletions

View File

@ -19,7 +19,6 @@ package cephfs
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"time" "time"
@ -135,8 +134,9 @@ func genSnapFromOptions(ctx context.Context, req *csi.CreateSnapshotRequest) (sn
cephfsSnap.RequestName = req.GetName() cephfsSnap.RequestName = req.GetName()
snapOptions := req.GetParameters() snapOptions := req.GetParameters()
cephfsSnap.Monitors, cephfsSnap.ClusterID, err = getMonsAndClusterID(ctx, snapOptions) cephfsSnap.Monitors, cephfsSnap.ClusterID, err = util.GetMonsAndClusterID(snapOptions)
if err != nil { if err != nil {
klog.Errorf(util.Log(ctx, "failed getting mons (%s)"), err)
return nil, err return nil, err
} }
if namePrefix, ok := snapOptions["snapshotNamePrefix"]; ok { if namePrefix, ok := snapOptions["snapshotNamePrefix"]; ok {
@ -145,23 +145,6 @@ func genSnapFromOptions(ctx context.Context, req *csi.CreateSnapshotRequest) (sn
return cephfsSnap, nil 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) { func parseTime(ctx context.Context, createTime string) (*timestamp.Timestamp, error) {
tm := &timestamp.Timestamp{} tm := &timestamp.Timestamp{}
layout := "2006-01-02 15:04:05.000000" layout := "2006-01-02 15:04:05.000000"

View File

@ -531,8 +531,9 @@ func genSnapFromSnapID(ctx context.Context, rbdSnap *rbdSnapshot, snapshotID str
rbdSnap.ClusterID = vi.ClusterID rbdSnap.ClusterID = vi.ClusterID
options["clusterID"] = rbdSnap.ClusterID options["clusterID"] = rbdSnap.ClusterID
rbdSnap.Monitors, _, err = getMonsAndClusterID(ctx, options) rbdSnap.Monitors, _, err = util.GetMonsAndClusterID(options)
if err != nil { if err != nil {
klog.Errorf(util.Log(ctx, "failed getting mons (%s)"), err)
return err return err
} }
@ -593,8 +594,9 @@ func genVolFromVolID(ctx context.Context, volumeID string, cr *util.Credentials,
rbdVol.ClusterID = vi.ClusterID rbdVol.ClusterID = vi.ClusterID
options["clusterID"] = rbdVol.ClusterID options["clusterID"] = rbdVol.ClusterID
rbdVol.Monitors, _, err = getMonsAndClusterID(ctx, options) rbdVol.Monitors, _, err = util.GetMonsAndClusterID(options)
if err != nil { if err != nil {
klog.Errorf(util.Log(ctx, "failed getting mons (%s)"), err)
return rbdVol, err return rbdVol, err
} }
@ -663,23 +665,6 @@ func genVolFromVolID(ctx context.Context, volumeID string, cr *util.Credentials,
return rbdVol, err 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) { func genVolFromVolumeOptions(ctx context.Context, volOptions, credentials map[string]string, disableInUseChecks bool) (*rbdVolume, error) {
var ( var (
ok bool ok bool
@ -699,8 +684,9 @@ func genVolFromVolumeOptions(ctx context.Context, volOptions, credentials map[st
rbdVol.NamePrefix = namePrefix rbdVol.NamePrefix = namePrefix
} }
rbdVol.Monitors, rbdVol.ClusterID, err = getMonsAndClusterID(ctx, volOptions) rbdVol.Monitors, rbdVol.ClusterID, err = util.GetMonsAndClusterID(volOptions)
if err != nil { if err != nil {
klog.Errorf(util.Log(ctx, "failed getting mons (%s)"), err)
return nil, err return nil, err
} }
@ -757,8 +743,9 @@ func genSnapFromOptions(ctx context.Context, rbdVol *rbdVolume, snapOptions map[
rbdSnap.Pool = rbdVol.Pool rbdSnap.Pool = rbdVol.Pool
rbdSnap.JournalPool = rbdVol.JournalPool rbdSnap.JournalPool = rbdVol.JournalPool
rbdSnap.Monitors, rbdSnap.ClusterID, err = getMonsAndClusterID(ctx, snapOptions) rbdSnap.Monitors, rbdSnap.ClusterID, err = util.GetMonsAndClusterID(snapOptions)
if err != nil { if err != nil {
klog.Errorf(util.Log(ctx, "failed getting mons (%s)"), err)
return nil, err return nil, err
} }

View File

@ -18,6 +18,7 @@ package util
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"strings" "strings"
@ -111,3 +112,19 @@ func CephFSSubvolumeGroup(pathToConfig, clusterID string) (string, error) {
} }
return cluster.CephFS.SubvolumeGroup, nil 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
}