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 <rar@redhat.com>
(cherry picked from commit 82d09d81cf)
This commit is contained in:
Rakshith R 2021-09-07 11:35:11 +05:30 committed by mergify[bot]
parent b7505c29e2
commit 3f435f5eb2
4 changed files with 36 additions and 22 deletions

View File

@ -128,7 +128,11 @@ 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 = 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 { if err != nil {
util.ErrorLog(ctx, "failed getting mons (%s)", err) util.ErrorLog(ctx, "failed getting mons (%s)", err)

View File

@ -844,11 +844,7 @@ func genSnapFromSnapID(
snapshotID string, snapshotID string,
cr *util.Credentials, cr *util.Credentials,
secrets map[string]string) error { secrets map[string]string) error {
var ( var vi util.CSIIdentifier
options map[string]string
vi util.CSIIdentifier
)
options = make(map[string]string)
rbdSnap.VolID = snapshotID rbdSnap.VolID = snapshotID
@ -860,9 +856,8 @@ func genSnapFromSnapID(
} }
rbdSnap.ClusterID = vi.ClusterID 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 { if err != nil {
util.ErrorLog(ctx, "failed getting mons (%s)", err) util.ErrorLog(ctx, "failed getting mons (%s)", err)
@ -936,11 +931,9 @@ func generateVolumeFromVolumeID(
cr *util.Credentials, cr *util.Credentials,
secrets map[string]string) (*rbdVolume, error) { secrets map[string]string) (*rbdVolume, error) {
var ( var (
options map[string]string rbdVol *rbdVolume
rbdVol *rbdVolume err error
err error
) )
options = make(map[string]string)
// rbdVolume fields that are not filled up in this function are: // rbdVolume fields that are not filled up in this function are:
// Mounter, MultiNodeWritable // Mounter, MultiNodeWritable
@ -948,9 +941,8 @@ func generateVolumeFromVolumeID(
rbdVol.VolID = volumeID rbdVol.VolID = volumeID
rbdVol.ClusterID = vi.ClusterID 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 { if err != nil {
util.ErrorLog(ctx, "failed getting mons (%s)", err) util.ErrorLog(ctx, "failed getting mons (%s)", err)
@ -1166,7 +1158,11 @@ func genVolFromVolumeOptions(
rbdVol.NamePrefix = namePrefix 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 { if err != nil {
util.ErrorLog(ctx, "failed getting mons (%s)", err) 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.JournalPool = rbdVol.JournalPool
rbdSnap.RadosNamespace = rbdVol.RadosNamespace 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 { if err != nil {
util.ErrorLog(ctx, "failed getting mons (%s)", err) util.ErrorLog(ctx, "failed getting mons (%s)", err)

View File

@ -19,7 +19,6 @@ package util
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"strings" "strings"
@ -32,6 +31,9 @@ const (
// CsiConfigFile is the location of the CSI config file. // CsiConfigFile is the location of the CSI config file.
CsiConfigFile = "/etc/ceph-csi-config/config.json" 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. // 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 // GetMonsAndClusterID returns monitors and clusterID information read from
// configfile. // configfile.
func GetMonsAndClusterID(ctx context.Context, options map[string]string, checkClusterIDMapping bool) (string, string, error) { func GetMonsAndClusterID(ctx context.Context, clusterID string, checkClusterIDMapping bool) (string, string, error) {
clusterID, ok := options["clusterID"]
if !ok {
return "", "", errors.New("clusterID must be set")
}
if checkClusterIDMapping { if checkClusterIDMapping {
monitors, mappedClusterID, err := FetchMappedClusterIDAndMons(ctx, clusterID) monitors, mappedClusterID, err := FetchMappedClusterIDAndMons(ctx, clusterID)
if err != nil { if err != nil {
@ -153,3 +151,13 @@ func GetMonsAndClusterID(ctx context.Context, options map[string]string, checkCl
return monitors, clusterID, nil 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
}

View File

@ -33,6 +33,8 @@ var (
ErrSnapNameConflict = errors.New("snapshot name conflict") ErrSnapNameConflict = errors.New("snapshot name conflict")
// ErrPoolNotFound is returned when pool is not found. // ErrPoolNotFound is returned when pool is not found.
ErrPoolNotFound = errors.New("pool 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 { type errorPair struct {