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"
|
2022-01-21 09:32:34 +00:00
|
|
|
"os"
|
2019-04-22 21:35:39 +00:00
|
|
|
"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"`
|
|
|
|
// 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 {
|
2022-04-18 06:32:31 +00:00
|
|
|
// symlink filepath for the network namespace where we need to execute commands.
|
|
|
|
NetNamespaceFilePath string `json:"netNamespaceFilePath"`
|
2020-04-29 10:03:08 +00:00
|
|
|
// SubvolumeGroup contains the name of the SubvolumeGroup for CSI volumes
|
|
|
|
SubvolumeGroup string `json:"subvolumeGroup"`
|
|
|
|
} `json:"cephFS"`
|
2022-04-18 05:57:04 +00:00
|
|
|
|
|
|
|
// RBD Contains RBD specific options
|
|
|
|
RBD struct {
|
|
|
|
// symlink filepath for the network namespace where we need to execute commands.
|
|
|
|
NetNamespaceFilePath string `json:"netNamespaceFilePath"`
|
2022-04-18 06:08:52 +00:00
|
|
|
// RadosNamespace is a rados namespace in the pool
|
|
|
|
RadosNamespace string `json:"radosNamespace"`
|
2022-04-18 05:57:04 +00:00
|
|
|
} `json:"rbd"`
|
2022-07-26 10:04:57 +00:00
|
|
|
// NFS contains NFS specific options
|
|
|
|
NFS struct {
|
|
|
|
// symlink filepath for the network namespace where we need to execute commands.
|
|
|
|
NetNamespaceFilePath string `json:"netNamespaceFilePath"`
|
|
|
|
} `json:"nfs"`
|
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,
|
2023-06-02 08:59:52 +00:00
|
|
|
//nolint:godot // example json content should not contain unwanted dot.
|
2022-04-18 06:18:54 +00:00
|
|
|
/*
|
|
|
|
[{
|
|
|
|
"clusterID": "<cluster-id>",
|
|
|
|
"rbd": {
|
|
|
|
"radosNamespace": "<rados-namespace>"
|
|
|
|
},
|
|
|
|
"monitors": [
|
|
|
|
"<monitor-value>",
|
|
|
|
"<monitor-value>"
|
|
|
|
],
|
|
|
|
"cephFS": {
|
|
|
|
"subvolumeGroup": "<subvolumegroup for cephfs volumes>"
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
*/
|
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
|
2022-01-21 09:32:34 +00:00
|
|
|
content, err := os.ReadFile(pathToConfig)
|
2019-04-22 21:35:39 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2022-08-05 11:01:31 +00:00
|
|
|
for i := range config {
|
|
|
|
if config[i].ClusterID == clusterID {
|
|
|
|
return &config[i], 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
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
// GetRadosNamespace returns the namespace for the given clusterID.
|
|
|
|
func GetRadosNamespace(pathToConfig, clusterID string) (string, error) {
|
2020-06-01 13:57:51 +00:00
|
|
|
cluster, err := readClusterInfo(pathToConfig, clusterID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2023-11-03 08:45:30 +00:00
|
|
|
return cluster.RBD.RadosNamespace, nil
|
2020-06-01 13:57:51 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2022-03-31 13:29:33 +00:00
|
|
|
|
2022-04-18 05:57:04 +00:00
|
|
|
func GetRBDNetNamespaceFilePath(pathToConfig, clusterID string) (string, error) {
|
2022-03-31 13:29:33 +00:00
|
|
|
cluster, err := readClusterInfo(pathToConfig, clusterID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-04-18 05:57:04 +00:00
|
|
|
return cluster.RBD.NetNamespaceFilePath, nil
|
2022-03-31 13:29:33 +00:00
|
|
|
}
|
2022-04-18 06:32:31 +00:00
|
|
|
|
|
|
|
// GetCephFSNetNamespaceFilePath returns the netNamespaceFilePath for CephFS volumes.
|
|
|
|
func GetCephFSNetNamespaceFilePath(pathToConfig, clusterID string) (string, error) {
|
|
|
|
cluster, err := readClusterInfo(pathToConfig, clusterID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cluster.CephFS.NetNamespaceFilePath, nil
|
|
|
|
}
|
2022-07-26 10:04:57 +00:00
|
|
|
|
|
|
|
// GetNFSNetNamespaceFilePath returns the netNamespaceFilePath for NFS volumes.
|
|
|
|
func GetNFSNetNamespaceFilePath(pathToConfig, clusterID string) (string, error) {
|
|
|
|
cluster, err := readClusterInfo(pathToConfig, clusterID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cluster.NFS.NetNamespaceFilePath, nil
|
|
|
|
}
|