util: make util.ClusterInfo usable outside util package

functions like getClusterInfo() returns struct
instead of a set of strings.

Fix: #998

Signed-off-by: Yug Gupta <ygupta@redhat.com>
This commit is contained in:
Yug Gupta 2020-05-14 18:50:11 +05:30 committed by mergify[bot]
parent 63c458bd63
commit 2cdf5c3b9f
3 changed files with 34 additions and 27 deletions

View File

@ -12,6 +12,8 @@ import (
// _ "github.com/kubernetes-csi/external-snapshotter/pkg/apis/volumesnapshot/v1alpha1" // nolint
// _ "github.com/kubernetes-csi/external-snapshotter/pkg/client/clientset/versioned/typed/volumesnapshot/v1alpha1" // nolint
"github.com/ceph/ceph-csi/internal/util"
. "github.com/onsi/ginkgo" // nolint
. "github.com/onsi/gomega" // nolint
apps "k8s.io/api/apps/v1"
@ -365,15 +367,6 @@ func deleteConfigMap(pluginPath string) {
}
}
// matches the definition in internal/util/csiconfig.go
type clusterInfo struct {
ClusterID string `json:"clusterID"`
Monitors []string `json:"monitors"`
CephFS struct {
SubvolumeGroup string `json:"subvolumeGroup"`
} `json:"cephFS"`
}
func createConfigMap(pluginPath string, c kubernetes.Interface, f *framework.Framework) {
path := pluginPath + configMap
cm := v1.ConfigMap{}
@ -388,7 +381,7 @@ func createConfigMap(pluginPath string, c kubernetes.Interface, f *framework.Fra
fsID = strings.Trim(fsID, "\n")
// get mon list
mons := getMons(rookNamespace, c)
conmap := []clusterInfo{{
conmap := []util.ClusterInfo{{
ClusterID: fsID,
Monitors: mons,
}}

View File

@ -20,6 +20,7 @@ import (
"context"
"fmt"
"strconv"
"strings"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/pkg/errors"
@ -110,30 +111,34 @@ func extractMounter(dest *string, options map[string]string) error {
return nil
}
func getClusterInformation(options map[string]string) (string, string, string, error) {
func getClusterInformation(options map[string]string) (*util.ClusterInfo, error) {
clusterID, ok := options["clusterID"]
if !ok {
err := fmt.Errorf("clusterID must be set")
return "", "", "", err
return nil, err
}
if err := validateNonEmptyField(clusterID, "clusterID"); err != nil {
return "", "", "", err
return nil, err
}
monitors, err := util.Mons(csiConfigFile, clusterID)
if err != nil {
err = errors.Wrapf(err, "failed to fetch monitor list using clusterID (%s)", clusterID)
return "", "", "", err
return nil, err
}
subvolumeGroup, err := util.CephFSSubvolumeGroup(csiConfigFile, clusterID)
if err != nil {
err = errors.Wrapf(err, "failed to fetch subvolumegroup using clusterID (%s)", clusterID)
return "", "", "", err
return nil, err
}
return clusterID, monitors, subvolumeGroup, err
clusterData := &util.ClusterInfo{
ClusterID: clusterID,
Monitors: strings.Split(monitors, ","),
}
clusterData.CephFS.SubvolumeGroup = subvolumeGroup
return clusterData, nil
}
// newVolumeOptions generates a new instance of volumeOptions from the provided
@ -146,12 +151,16 @@ func newVolumeOptions(ctx context.Context, requestName string, req *csi.CreateVo
)
volOptions := req.GetParameters()
clusterData, err := getClusterInformation(volOptions)
opts.ClusterID, opts.Monitors, opts.SubvolumeGroup, err = getClusterInformation(volOptions)
if err != nil {
return nil, err
}
opts.ClusterID = clusterData.ClusterID
opts.Monitors = strings.Join(clusterData.Monitors, ",")
opts.SubvolumeGroup = clusterData.CephFS.SubvolumeGroup
if err = extractOptionalOption(&opts.Pool, "pool", volOptions); err != nil {
return nil, err
}
@ -373,11 +382,16 @@ func newVolumeOptionsFromStaticVolume(volID string, options map[string]string) (
// store NOT of static boolean
opts.ProvisionVolume = !staticVol
opts.ClusterID, opts.Monitors, opts.SubvolumeGroup, err = getClusterInformation(options)
clusterData, err := getClusterInformation(options)
if err != nil {
return nil, nil, err
}
opts.ClusterID = clusterData.ClusterID
opts.Monitors = strings.Join(clusterData.Monitors, ",")
opts.SubvolumeGroup = clusterData.CephFS.SubvolumeGroup
if err = extractOption(&opts.RootPath, "rootPath", options); err != nil {
return nil, nil, err
}

View File

@ -29,8 +29,8 @@ const (
defaultCsiSubvolumeGroup = "csi"
)
// clusterInfo strongly typed JSON spec for the above JSON structure
type clusterInfo struct {
// ClusterInfo strongly typed JSON spec for the below JSON structure
type ClusterInfo struct {
// ClusterID is used for unique identification
ClusterID string `json:"clusterID"`
// Monitors is monitor list for corresponding cluster ID
@ -58,29 +58,29 @@ type clusterInfo struct {
// },
// ...
// ]
func readClusterInfo(pathToConfig, clusterID string) (clusterInfo, error) {
var config []clusterInfo
func readClusterInfo(pathToConfig, clusterID string) (*ClusterInfo, error) {
var config []ClusterInfo
// #nosec
content, err := ioutil.ReadFile(pathToConfig)
if err != nil {
err = fmt.Errorf("error fetching configuration for cluster ID (%s). (%s)", clusterID, err)
return clusterInfo{}, err
return nil, err
}
err = json.Unmarshal(content, &config)
if err != nil {
return clusterInfo{}, fmt.Errorf("unmarshal failed: %v. raw buffer response: %s",
return nil, fmt.Errorf("unmarshal failed: %v. raw buffer response: %s",
err, string(content))
}
for _, cluster := range config {
if cluster.ClusterID == clusterID {
return cluster, nil
return &cluster, nil
}
}
return clusterInfo{}, fmt.Errorf("missing configuration for cluster ID (%s)", clusterID)
return nil, fmt.Errorf("missing configuration for cluster ID (%s)", clusterID)
}
// Mons returns a comma separated MON list from the csi config for the given clusterID