From 3f435f5eb23abeda86fce84c83d8c479c75ac26e Mon Sep 17 00:00:00 2001 From: Rakshith R Date: Tue, 7 Sep 2021 11:35:11 +0530 Subject: [PATCH] util: modify GetMonsAndClusterID() to take clusterID instead of options This commit: - modifies GetMonsAndClusterID() to take clusterID instead of options. - moves out validation of clusterID is set or not out of GetMonsAndClusterID(). - defines ErrClusterIDNotSet new error for reusability. - add GetClusterID() to obtain clusterID from options. Signed-off-by: Rakshith R (cherry picked from commit 82d09d81cfba97eab88ce2dca6fdca71c69437be) --- internal/cephfs/util.go | 6 +++++- internal/rbd/rbd_util.go | 30 +++++++++++++++--------------- internal/util/csiconfig.go | 20 ++++++++++++++------ internal/util/errors.go | 2 ++ 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/internal/cephfs/util.go b/internal/cephfs/util.go index d53fc2ef2..e65245fc1 100644 --- a/internal/cephfs/util.go +++ b/internal/cephfs/util.go @@ -128,7 +128,11 @@ func genSnapFromOptions(ctx context.Context, req *csi.CreateSnapshotRequest) (sn cephfsSnap.RequestName = req.GetName() snapOptions := req.GetParameters() - cephfsSnap.Monitors, cephfsSnap.ClusterID, err = util.GetMonsAndClusterID(ctx, snapOptions, false) + clusterID, err := util.GetClusterID(snapOptions) + if err != nil { + return nil, err + } + cephfsSnap.Monitors, cephfsSnap.ClusterID, err = util.GetMonsAndClusterID(ctx, clusterID, false) if err != nil { util.ErrorLog(ctx, "failed getting mons (%s)", err) diff --git a/internal/rbd/rbd_util.go b/internal/rbd/rbd_util.go index 9e97f907f..5b2ad7aab 100644 --- a/internal/rbd/rbd_util.go +++ b/internal/rbd/rbd_util.go @@ -844,11 +844,7 @@ func genSnapFromSnapID( snapshotID string, cr *util.Credentials, secrets map[string]string) error { - var ( - options map[string]string - vi util.CSIIdentifier - ) - options = make(map[string]string) + var vi util.CSIIdentifier rbdSnap.VolID = snapshotID @@ -860,9 +856,8 @@ func genSnapFromSnapID( } rbdSnap.ClusterID = vi.ClusterID - options["clusterID"] = rbdSnap.ClusterID - rbdSnap.Monitors, _, err = util.GetMonsAndClusterID(ctx, options, false) + rbdSnap.Monitors, _, err = util.GetMonsAndClusterID(ctx, rbdSnap.ClusterID, false) if err != nil { util.ErrorLog(ctx, "failed getting mons (%s)", err) @@ -936,11 +931,9 @@ func generateVolumeFromVolumeID( cr *util.Credentials, secrets map[string]string) (*rbdVolume, error) { var ( - options map[string]string - rbdVol *rbdVolume - err error + rbdVol *rbdVolume + err error ) - options = make(map[string]string) // rbdVolume fields that are not filled up in this function are: // Mounter, MultiNodeWritable @@ -948,9 +941,8 @@ func generateVolumeFromVolumeID( rbdVol.VolID = volumeID rbdVol.ClusterID = vi.ClusterID - options["clusterID"] = rbdVol.ClusterID - rbdVol.Monitors, _, err = util.GetMonsAndClusterID(ctx, options, false) + rbdVol.Monitors, _, err = util.GetMonsAndClusterID(ctx, rbdVol.ClusterID, false) if err != nil { util.ErrorLog(ctx, "failed getting mons (%s)", err) @@ -1166,7 +1158,11 @@ func genVolFromVolumeOptions( rbdVol.NamePrefix = namePrefix } - rbdVol.Monitors, rbdVol.ClusterID, err = util.GetMonsAndClusterID(ctx, volOptions, checkClusterIDMapping) + clusterID, err := util.GetClusterID(volOptions) + if err != nil { + return nil, err + } + rbdVol.Monitors, rbdVol.ClusterID, err = util.GetMonsAndClusterID(ctx, clusterID, checkClusterIDMapping) if err != nil { util.ErrorLog(ctx, "failed getting mons (%s)", err) @@ -1243,7 +1239,11 @@ func genSnapFromOptions(ctx context.Context, rbdVol *rbdVolume, snapOptions map[ rbdSnap.JournalPool = rbdVol.JournalPool rbdSnap.RadosNamespace = rbdVol.RadosNamespace - rbdSnap.Monitors, rbdSnap.ClusterID, err = util.GetMonsAndClusterID(ctx, snapOptions, false) + clusterID, err := util.GetClusterID(snapOptions) + if err != nil { + return nil, err + } + rbdSnap.Monitors, rbdSnap.ClusterID, err = util.GetMonsAndClusterID(ctx, clusterID, false) if err != nil { util.ErrorLog(ctx, "failed getting mons (%s)", err) diff --git a/internal/util/csiconfig.go b/internal/util/csiconfig.go index 7c9002d91..c19525e3c 100644 --- a/internal/util/csiconfig.go +++ b/internal/util/csiconfig.go @@ -19,7 +19,6 @@ package util import ( "context" "encoding/json" - "errors" "fmt" "io/ioutil" "strings" @@ -32,6 +31,9 @@ const ( // CsiConfigFile is the location of the CSI config file. CsiConfigFile = "/etc/ceph-csi-config/config.json" + + // ClusterIDKey is the name of the key containing clusterID. + clusterIDKey = "clusterID" ) // ClusterInfo strongly typed JSON spec for the below JSON structure. @@ -132,11 +134,7 @@ func CephFSSubvolumeGroup(pathToConfig, clusterID string) (string, error) { // GetMonsAndClusterID returns monitors and clusterID information read from // configfile. -func GetMonsAndClusterID(ctx context.Context, options map[string]string, checkClusterIDMapping bool) (string, string, error) { - clusterID, ok := options["clusterID"] - if !ok { - return "", "", errors.New("clusterID must be set") - } +func GetMonsAndClusterID(ctx context.Context, clusterID string, checkClusterIDMapping bool) (string, string, error) { if checkClusterIDMapping { monitors, mappedClusterID, err := FetchMappedClusterIDAndMons(ctx, clusterID) if err != nil { @@ -153,3 +151,13 @@ func GetMonsAndClusterID(ctx context.Context, options map[string]string, checkCl return monitors, clusterID, nil } + +// GetClusterID fetches clusterID from given options map. +func GetClusterID(options map[string]string) (string, error) { + clusterID, ok := options[clusterIDKey] + if !ok { + return "", ErrClusterIDNotSet + } + + return clusterID, nil +} diff --git a/internal/util/errors.go b/internal/util/errors.go index a40d63b93..7e66a9ad3 100644 --- a/internal/util/errors.go +++ b/internal/util/errors.go @@ -33,6 +33,8 @@ var ( ErrSnapNameConflict = errors.New("snapshot name conflict") // ErrPoolNotFound is returned when pool is not found. ErrPoolNotFound = errors.New("pool not found") + // ErrClusterIDNotSet is returned when cluster id is not set. + ErrClusterIDNotSet = errors.New("clusterID must be set") ) type errorPair struct {