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"
|
2023-12-18 11:04:31 +00:00
|
|
|
|
|
|
|
"github.com/ceph/ceph-csi/api/deploy/kubernetes"
|
2019-04-22 21:35:39 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2024-06-05 05:34:58 +00:00
|
|
|
// defaultCsiCephFSRadosNamespace defines the default RADOS namespace used for storing
|
|
|
|
// CSI-specific objects and keys for CephFS volumes.
|
|
|
|
defaultCsiCephFSRadosNamespace = "csi"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// 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>"
|
2024-04-18 12:02:29 +00:00
|
|
|
"mirrorDaemonCount": 1
|
2022-04-18 06:18:54 +00:00
|
|
|
},
|
|
|
|
"monitors": [
|
|
|
|
"<monitor-value>",
|
|
|
|
"<monitor-value>"
|
|
|
|
],
|
|
|
|
"cephFS": {
|
|
|
|
"subvolumeGroup": "<subvolumegroup for cephfs volumes>"
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
*/
|
2023-12-18 11:04:31 +00:00
|
|
|
func readClusterInfo(pathToConfig, clusterID string) (*kubernetes.ClusterInfo, error) {
|
|
|
|
var config []kubernetes.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
|
|
|
|
}
|
|
|
|
|
2024-06-05 05:34:58 +00:00
|
|
|
// GetRBDRadosNamespace returns the namespace for the given clusterID.
|
|
|
|
func GetRBDRadosNamespace(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
|
|
|
}
|
|
|
|
|
2024-06-05 05:34:58 +00:00
|
|
|
// GetCephFSRadosNamespace returns the namespace for the given clusterID.
|
|
|
|
// If not set, it returns the default value "csi".
|
|
|
|
func GetCephFSRadosNamespace(pathToConfig, clusterID string) (string, error) {
|
|
|
|
cluster, err := readClusterInfo(pathToConfig, clusterID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cluster.CephFS.RadosNamespace == "" {
|
|
|
|
return defaultCsiCephFSRadosNamespace, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return cluster.CephFS.RadosNamespace, nil
|
|
|
|
}
|
|
|
|
|
2024-04-18 12:02:29 +00:00
|
|
|
// GetRBDMirrorDaemonCount returns the number of mirror daemon count for the
|
|
|
|
// given clusterID.
|
|
|
|
func GetRBDMirrorDaemonCount(pathToConfig, clusterID string) (int, error) {
|
|
|
|
cluster, err := readClusterInfo(pathToConfig, clusterID)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it is empty, set the default to 1 which is most common in a cluster.
|
|
|
|
if cluster.RBD.MirrorDaemonCount == 0 {
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return cluster.RBD.MirrorDaemonCount, 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
|
|
|
|
}
|
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
|
|
|
|
}
|
2023-10-30 08:23:12 +00:00
|
|
|
|
|
|
|
// GetCrushLocationLabels returns the `readAffinity.enabled` and `readAffinity.crushLocationLabels`
|
|
|
|
// values from the CSI config for the given `clusterID`. If `readAffinity.enabled` is set to true
|
|
|
|
// it returns `true` and `crushLocationLabels`, else returns `false` and an empty string.
|
|
|
|
func GetCrushLocationLabels(pathToConfig, clusterID string) (bool, string, error) {
|
|
|
|
cluster, err := readClusterInfo(pathToConfig, clusterID)
|
|
|
|
if err != nil {
|
|
|
|
return false, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cluster.ReadAffinity.Enabled {
|
|
|
|
return false, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
crushLocationLabels := strings.Join(cluster.ReadAffinity.CrushLocationLabels, ",")
|
|
|
|
|
|
|
|
return true, crushLocationLabels, nil
|
|
|
|
}
|
2023-11-07 09:57:10 +00:00
|
|
|
|
|
|
|
// GetCephFSMountOptions returns the `kernelMountOptions` and `fuseMountOptions` for CephFS volumes.
|
|
|
|
func GetCephFSMountOptions(pathToConfig, clusterID string) (string, string, error) {
|
|
|
|
cluster, err := readClusterInfo(pathToConfig, clusterID)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cluster.CephFS.KernelMountOptions, cluster.CephFS.FuseMountOptions, nil
|
|
|
|
}
|