2019-05-28 19:03:18 +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.
|
|
|
|
*/
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
package core
|
2019-05-28 19:03:18 +00:00
|
|
|
|
|
|
|
import (
|
2019-08-22 17:19:06 +00:00
|
|
|
"context"
|
2019-05-28 19:03:18 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-08-25 06:46:03 +00:00
|
|
|
cerrors "github.com/ceph/ceph-csi/internal/cephfs/errors"
|
2020-04-17 09:23:49 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2021-08-24 15:03:25 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
2019-05-28 19:03:18 +00:00
|
|
|
)
|
|
|
|
|
2022-01-25 05:30:16 +00:00
|
|
|
// FileSystem is the interface that holds the signature of filesystem methods
|
|
|
|
// that interacts with CephFS filesystem API's.
|
|
|
|
type FileSystem interface {
|
|
|
|
// GetFscID returns the ID of the filesystem with the given name.
|
2024-04-04 08:50:20 +00:00
|
|
|
GetFscID(ctx context.Context, fsName string) (int64, error)
|
2022-01-25 05:30:16 +00:00
|
|
|
// GetMetadataPool returns the metadata pool name of the filesystem with the given name.
|
2024-04-04 08:50:20 +00:00
|
|
|
GetMetadataPool(ctx context.Context, fsName string) (string, error)
|
2022-01-25 05:30:16 +00:00
|
|
|
// GetFsName returns the name of the filesystem with the given ID.
|
2024-04-04 08:50:20 +00:00
|
|
|
GetFsName(ctx context.Context, fsID int64) (string, error)
|
2022-01-25 05:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fileSystem is the implementation of FileSystem interface.
|
|
|
|
type fileSystem struct {
|
|
|
|
conn *util.ClusterConnection
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFileSystem returns a new instance of fileSystem.
|
|
|
|
func NewFileSystem(conn *util.ClusterConnection) FileSystem {
|
|
|
|
return &fileSystem{
|
|
|
|
conn: conn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFscID returns the ID of the filesystem with the given name.
|
|
|
|
func (f *fileSystem) GetFscID(ctx context.Context, fsName string) (int64, error) {
|
|
|
|
fsa, err := f.conn.GetFSAdmin()
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
2022-01-25 09:03:37 +00:00
|
|
|
log.ErrorLog(ctx, "could not get FSAdmin, can not fetch filesystem ID for %s: %s", fsName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2020-10-02 08:14:17 +00:00
|
|
|
volumes, err := fsa.EnumerateVolumes()
|
|
|
|
if err != nil {
|
2022-01-25 09:03:37 +00:00
|
|
|
log.ErrorLog(ctx, "could not list volumes, can not fetch filesystem ID for %s: %s", fsName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-10-02 08:14:17 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, vol := range volumes {
|
2022-01-25 05:30:16 +00:00
|
|
|
if vol.Name == fsName {
|
2020-10-02 08:14:17 +00:00
|
|
|
return vol.ID, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 05:30:16 +00:00
|
|
|
log.ErrorLog(ctx, "failed to list volume %s", fsName)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-08-25 06:46:03 +00:00
|
|
|
return 0, cerrors.ErrVolumeNotFound
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
2022-01-25 05:30:16 +00:00
|
|
|
// GetMetadataPool returns the metadata pool name of the filesystem with the given name.
|
|
|
|
func (f *fileSystem) GetMetadataPool(ctx context.Context, fsName string) (string, error) {
|
|
|
|
fsa, err := f.conn.GetFSAdmin()
|
2020-10-02 11:18:24 +00:00
|
|
|
if err != nil {
|
2022-01-25 09:03:37 +00:00
|
|
|
log.ErrorLog(ctx, "could not get FSAdmin, can not fetch metadata pool for %s: %s", fsName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-10-02 11:18:24 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2019-05-28 19:03:18 +00:00
|
|
|
|
2020-10-02 11:18:24 +00:00
|
|
|
fsPoolInfos, err := fsa.ListFileSystems()
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
2022-01-25 09:03:37 +00:00
|
|
|
log.ErrorLog(ctx, "could not list filesystems, can not fetch metadata pool for %s: %s", fsName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-10-02 11:18:24 +00:00
|
|
|
for _, fspi := range fsPoolInfos {
|
2022-01-25 05:30:16 +00:00
|
|
|
if fspi.Name == fsName {
|
2020-10-02 11:18:24 +00:00
|
|
|
return fspi.MetadataPool, nil
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 05:30:16 +00:00
|
|
|
return "", fmt.Errorf("%w: could not find metadata pool for %s", util.ErrPoolNotFound, fsName)
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
2022-01-25 05:30:16 +00:00
|
|
|
// GetFsName returns the name of the filesystem with the given ID.
|
|
|
|
func (f *fileSystem) GetFsName(ctx context.Context, fscID int64) (string, error) {
|
|
|
|
fsa, err := f.conn.GetFSAdmin()
|
2020-10-02 08:44:40 +00:00
|
|
|
if err != nil {
|
2022-01-25 09:03:37 +00:00
|
|
|
log.ErrorLog(ctx, "could not get FSAdmin, can not fetch filesystem name for ID %d: %s", fscID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-10-02 08:44:40 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2019-05-28 19:03:18 +00:00
|
|
|
|
2020-10-02 08:44:40 +00:00
|
|
|
volumes, err := fsa.EnumerateVolumes()
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
2022-01-25 09:03:37 +00:00
|
|
|
log.ErrorLog(ctx, "could not list volumes, can not fetch filesystem name for ID %d: %s", fscID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-10-02 08:44:40 +00:00
|
|
|
for _, vol := range volumes {
|
2022-01-25 05:30:16 +00:00
|
|
|
if vol.ID == fscID {
|
2020-10-02 08:44:40 +00:00
|
|
|
return vol.Name, nil
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 05:30:16 +00:00
|
|
|
return "", fmt.Errorf("%w: fscID (%d) not found in Ceph cluster", util.ErrPoolNotFound, fscID)
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|