mirror of
https://github.com/ceph/ceph-csi.git
synced 2024-11-27 16:50:23 +00:00
daea5177e5
added helper function to read the clusterID mapping
from the mounted file.
The clusterID mapping contains below mappings
* ClusterID mappings (to cluster to which we are failingover
and from which cluster failover happened)
* RBD PoolID mapping of between the clusters.
* CephFS FscID mapping between the clusters.
Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
(cherry picked from commit ac11d71e19
)
117 lines
3.1 KiB
Go
117 lines
3.1 KiB
Go
/*
|
|
Copyright 2021 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 (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
)
|
|
|
|
// clusterMappingConfigFile is the location of the cluster mapping config file.
|
|
var clusterMappingConfigFile = "/etc/ceph-csi-config/cluster-mapping.json"
|
|
|
|
// ClusterMappingInfo holds the details of clusterID mapping and poolID mapping.
|
|
type ClusterMappingInfo struct {
|
|
// ClusterIDMapping holds the details of clusterID mapping
|
|
ClusterIDMapping map[string]string `json:"clusterIDMapping"`
|
|
// rbdpoolIDMappingInfo holds the details of RBD poolID mapping.
|
|
RBDpoolIDMappingInfo []map[string]string `json:"RBDPoolIDMapping"`
|
|
// cephFSpoolIDMappingInfo holds the details of CephFS Fscid mapping.
|
|
CephFSFscIDMappingInfo []map[string]string `json:"CephFSFscIDMapping"`
|
|
}
|
|
|
|
// Expected JSON structure in the passed in config file is,
|
|
// [{
|
|
// "clusterIDMapping": {
|
|
// "site1-storage": "site2-storage"
|
|
// },
|
|
// "RBDPoolIDMapping": [{
|
|
// "1": "2",
|
|
// "11": "12"
|
|
// }],
|
|
// "CephFSFscIDMapping": [{
|
|
// "13": "34",
|
|
// "3": "4"
|
|
// }]
|
|
// }, {
|
|
// "clusterIDMapping": {
|
|
// "site3-storage": "site2-storage"
|
|
// },
|
|
// "RBDPoolIDMapping": [{
|
|
// "5": "2",
|
|
// "16": "12"
|
|
// }],
|
|
// "CephFSFscIDMapping": [{
|
|
// "3": "34",
|
|
// "4": "4"
|
|
// }]
|
|
// ...
|
|
// }]
|
|
|
|
func readClusterMappingInfo() (*[]ClusterMappingInfo, error) {
|
|
var info []ClusterMappingInfo
|
|
content, err := ioutil.ReadFile(clusterMappingConfigFile)
|
|
if err != nil {
|
|
err = fmt.Errorf("error fetching clusterID mapping %w", err)
|
|
|
|
return nil, err
|
|
}
|
|
|
|
err = json.Unmarshal(content, &info)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unmarshal failed (%w), raw buffer response: %s",
|
|
err, string(content))
|
|
}
|
|
|
|
return &info, nil
|
|
}
|
|
|
|
// GetClusterMappingInfo returns corresponding cluster details like clusterID's
|
|
// poolID,fscID lists read from configfile.
|
|
func GetClusterMappingInfo(clusterID string) (*[]ClusterMappingInfo, error) {
|
|
var mappingInfo []ClusterMappingInfo
|
|
info, err := readClusterMappingInfo()
|
|
if err != nil {
|
|
// discard not found error as this file is expected to be created by
|
|
// the admin in case of failover.
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return nil, nil
|
|
}
|
|
|
|
return nil, fmt.Errorf("failed to fetch cluster mapping: %w", err)
|
|
}
|
|
for _, i := range *info {
|
|
for key, val := range i.ClusterIDMapping {
|
|
// Same file will be copied to the failover cluster check for both
|
|
// key and value to check clusterID mapping exists
|
|
if key == clusterID || val == clusterID {
|
|
mappingInfo = append(mappingInfo, i)
|
|
}
|
|
}
|
|
}
|
|
|
|
// if the mapping is not found return response as nil
|
|
if len(mappingInfo) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
return &mappingInfo, nil
|
|
}
|