Skip to content

Commit

Permalink
util: get clusterID for the passed in mon string
Browse files Browse the repository at this point in the history
as part of migration support, the clusterID has to be fetched
from passed in mon. Because the intree RBD storage class only
got monitor and not `clusterID` parameter support. However, in
CSI, SC has the `clusterID` parameter support but not mon. Due
to that we have to fetch the clusterID from config file for the
passed in mon and use it in our operations. This adds a helper
function to retrieve clusterID from passed in mon string.

Updates ceph#2509

Signed-off-by: Humble Chirammal <[email protected]>
  • Loading branch information
humblec committed Sep 16, 2021
1 parent 47dc9cf commit 0c95252
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
46 changes: 46 additions & 0 deletions internal/util/csiconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,49 @@ func GetClusterID(options map[string]string) (string, error) {

return clusterID, nil
}

// GetClusterIDFromMon will be called with a mon string to fetch
// clusterID based on the passed in mon string. If passed in 'mon'
// string has been found in the config the clusterID is returned,
// else error.
func GetClusterIDFromMon(pathToConfig, mon string) (string, error) {
clusterID := ""
clusterID, err := readClusterInfoWithMon(pathToConfig, mon)
if err != nil {
return "", err
}

if clusterID == "" {
return "", fmt.Errorf("empty clusterID for monitor (%s) in config", mon)
}
return clusterID, nil
}


func readClusterInfoWithMon(pathToConfig, mon string) (string, error) {
var config []ClusterInfo

// #nosec
content, err := ioutil.ReadFile(pathToConfig)
if err != nil {
err = fmt.Errorf("error fetching configuration file %q: %w", pathToConfig, err)

return "", err
}

err = json.Unmarshal(content, &config)
if err != nil {
return "", fmt.Errorf("unmarshal failed (%w), raw buffer response: %s",
err, string(content))
}

for _, cluster := range config {
for _, m := range cluster.Monitors {
if m == mon {
return cluster.ClusterID, nil
}
}
}

return "", fmt.Errorf( "missing configuration of cluster ID for mon %q", mon)
}
25 changes: 25 additions & 0 deletions internal/util/csiconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,29 @@ func TestCSIConfig(t *testing.T) {
if err != nil || content != "mon4,mon5,mon6" {
t.Errorf("Failed: want (%s), got (%s) (%v)", "mon4,mon5,mon6", content, err)
}

data = "[{\"clusterID\":\"" + clusterID2 + "\",\"monitors\":[\"mon1\",\"mon2\",\"mon3\"]}," +
"{\"clusterID\":\"" + clusterID1 + "\",\"monitors\":[\"mon4\",\"mon5\",\"mon6\"]}]"
err = ioutil.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600)
if err != nil {
t.Errorf("Test setup error %s", err)
}

// TEST: Should pass as clusterID is present in config
content, err = GetClusterIDFromMon(pathToConfig, "mon1")
if err != nil || content != "test2" {
t.Errorf("Failed: want (%s), got (%s) (%v)", "test2", content, err)
}

// TEST: Should pass as clusterID is present in config
content, err = GetClusterIDFromMon(pathToConfig, "mon5")
if err != nil || content != "test1" {
t.Errorf("Failed: want (%s), got (%s) (%v)", "test1", content, err)
}

// TEST: Should fail as clusterID is not present in config
content, err = GetClusterIDFromMon(pathToConfig, "mon8")
if err == nil {
t.Errorf("Failed: got (%s)", content)
}
}

0 comments on commit 0c95252

Please sign in to comment.