2019-04-22 21:35:39 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2021-09-06 05:27:50 +00:00
|
|
|
"context"
|
2019-04-22 21:35:39 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-04-29 10:03:08 +00:00
|
|
|
const (
|
|
|
|
// defaultCsiSubvolumeGroup defines the default name for the CephFS CSI subvolumegroup.
|
|
|
|
// This was hardcoded once and defaults to the old value to keep backward compatibility.
|
|
|
|
defaultCsiSubvolumeGroup = "csi"
|
2020-08-10 06:19:38 +00:00
|
|
|
|
2021-07-08 14:59:34 +00:00
|
|
|
// CsiConfigFile is the location of the CSI config file.
|
2020-08-10 06:19:38 +00:00
|
|
|
CsiConfigFile = "/etc/ceph-csi-config/config.json"
|
2021-09-07 06:05:11 +00:00
|
|
|
|
|
|
|
// ClusterIDKey is the name of the key containing clusterID.
|
2021-09-16 08:26:06 +00:00
|
|
|
ClusterIDKey = "clusterID"
|
2020-04-29 10:03:08 +00:00
|
|
|
)
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// ClusterInfo strongly typed JSON spec for the below JSON structure.
|
2020-05-14 13:20:11 +00:00
|
|
|
type ClusterInfo struct {
|
2020-04-28 15:22:24 +00:00
|
|
|
// ClusterID is used for unique identification
|
|
|
|
ClusterID string `json:"clusterID"`
|
2020-06-01 14:15:04 +00:00
|
|
|
// RadosNamespace is a rados namespace in the pool
|
2020-06-01 13:57:51 +00:00
|
|
|
RadosNamespace string `json:"radosNamespace"`
|
2020-04-28 15:22:24 +00:00
|
|
|
// Monitors is monitor list for corresponding cluster ID
|
|
|
|
Monitors []string `json:"monitors"`
|
2020-04-29 10:03:08 +00:00
|
|
|
// CephFS contains CephFS specific options
|
|
|
|
CephFS struct {
|
|
|
|
// SubvolumeGroup contains the name of the SubvolumeGroup for CSI volumes
|
|
|
|
SubvolumeGroup string `json:"subvolumeGroup"`
|
|
|
|
} `json:"cephFS"`
|
2020-04-28 15:22:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 10:03:08 +00:00
|
|
|
// Expected JSON structure in the passed in config file is,
|
|
|
|
// [
|
|
|
|
// {
|
|
|
|
// "clusterID": "<cluster-id>",
|
2020-06-01 14:15:04 +00:00
|
|
|
// "radosNamespace": "<rados-namespace>",
|
2020-04-29 10:03:08 +00:00
|
|
|
// "monitors":
|
|
|
|
// [
|
|
|
|
// "<monitor-value>",
|
|
|
|
// "<monitor-value>",
|
|
|
|
// ...
|
|
|
|
// ],
|
|
|
|
// "cephFS": {
|
|
|
|
// "subvolumeGroup": "<subvolumegroup for cephfs volumes>"
|
|
|
|
// }
|
|
|
|
// },
|
|
|
|
// ...
|
2020-07-19 12:21:03 +00:00
|
|
|
// ].
|
2020-05-14 13:20:11 +00:00
|
|
|
func readClusterInfo(pathToConfig, clusterID string) (*ClusterInfo, error) {
|
|
|
|
var config []ClusterInfo
|
2019-04-22 21:35:39 +00:00
|
|
|
|
|
|
|
// #nosec
|
|
|
|
content, err := ioutil.ReadFile(pathToConfig)
|
|
|
|
if err != nil {
|
2020-12-08 14:20:13 +00:00
|
|
|
err = fmt.Errorf("error fetching configuration for cluster ID %q: %w", clusterID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-05-14 13:20:11 +00:00
|
|
|
return nil, err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(content, &config)
|
|
|
|
if err != nil {
|
2020-12-08 14:20:13 +00:00
|
|
|
return nil, fmt.Errorf("unmarshal failed (%w), raw buffer response: %s",
|
2019-04-22 21:35:39 +00:00
|
|
|
err, string(content))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cluster := range config {
|
|
|
|
if cluster.ClusterID == clusterID {
|
2020-05-14 13:20:11 +00:00
|
|
|
return &cluster, nil
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-29 10:03:08 +00:00
|
|
|
|
2020-12-08 14:20:13 +00:00
|
|
|
return nil, fmt.Errorf("missing configuration for cluster ID %q", clusterID)
|
2020-04-29 10:03:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// Mons returns a comma separated MON list from the csi config for the given clusterID.
|
2020-04-29 10:03:08 +00:00
|
|
|
func Mons(pathToConfig, clusterID string) (string, error) {
|
|
|
|
cluster, err := readClusterInfo(pathToConfig, clusterID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cluster.Monitors) == 0 {
|
|
|
|
return "", fmt.Errorf("empty monitor list for cluster ID (%s) in config", clusterID)
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-04-29 10:03:08 +00:00
|
|
|
return strings.Join(cluster.Monitors, ","), nil
|
|
|
|
}
|
|
|
|
|
2020-06-01 13:57:51 +00:00
|
|
|
// RadosNamespace returns the namespace for the given clusterID.
|
|
|
|
func RadosNamespace(pathToConfig, clusterID string) (string, error) {
|
|
|
|
cluster, err := readClusterInfo(pathToConfig, clusterID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-01 13:57:51 +00:00
|
|
|
return cluster.RadosNamespace, nil
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// CephFSSubvolumeGroup returns the subvolumeGroup for CephFS volumes. If not set, it returns the default value "csi".
|
2020-04-29 10:03:08 +00:00
|
|
|
func CephFSSubvolumeGroup(pathToConfig, clusterID string) (string, error) {
|
|
|
|
cluster, err := readClusterInfo(pathToConfig, clusterID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cluster.CephFS.SubvolumeGroup == "" {
|
|
|
|
return defaultCsiSubvolumeGroup, nil
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-04-29 10:03:08 +00:00
|
|
|
return cluster.CephFS.SubvolumeGroup, nil
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
2020-08-10 06:27:28 +00:00
|
|
|
|
|
|
|
// GetMonsAndClusterID returns monitors and clusterID information read from
|
|
|
|
// configfile.
|
2021-09-07 06:05:11 +00:00
|
|
|
func GetMonsAndClusterID(ctx context.Context, clusterID string, checkClusterIDMapping bool) (string, string, error) {
|
2021-09-06 05:27:50 +00:00
|
|
|
if checkClusterIDMapping {
|
|
|
|
monitors, mappedClusterID, err := FetchMappedClusterIDAndMons(ctx, clusterID)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return monitors, mappedClusterID, nil
|
|
|
|
}
|
2020-08-10 06:27:28 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2021-09-07 06:05:11 +00:00
|
|
|
|
|
|
|
// GetClusterID fetches clusterID from given options map.
|
|
|
|
func GetClusterID(options map[string]string) (string, error) {
|
2021-09-16 08:26:06 +00:00
|
|
|
clusterID, ok := options[ClusterIDKey]
|
2021-09-07 06:05:11 +00:00
|
|
|
if !ok {
|
|
|
|
return "", ErrClusterIDNotSet
|
|
|
|
}
|
|
|
|
|
|
|
|
return clusterID, nil
|
|
|
|
}
|
2021-09-16 05:34:06 +00:00
|
|
|
|
|
|
|
// 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(mon string) (string, error) {
|
|
|
|
clusterID, err := readClusterInfoWithMon(CsiConfigFile, mon)
|
|
|
|
|
2021-09-16 08:26:06 +00:00
|
|
|
return clusterID, err
|
2021-09-16 05:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-09-16 08:26:06 +00:00
|
|
|
// as the same mons can fall into different clusterIDs with
|
|
|
|
// different radosnamespace configurations, we are bailing out
|
|
|
|
// if radosnamespace configuration is found for this cluster
|
|
|
|
if cluster.RadosNamespace != "" {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-16 05:34:06 +00:00
|
|
|
for _, m := range cluster.Monitors {
|
|
|
|
if m == mon {
|
|
|
|
return cluster.ClusterID, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 08:26:06 +00:00
|
|
|
return "", ErrMissingConfigForMonitor
|
2021-09-16 05:34:06 +00:00
|
|
|
}
|