From eb2584095bbf704223dd332efe4b040f010a3b19 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Fri, 2 Oct 2020 13:18:24 +0200 Subject: [PATCH] cephfs: implement getMetadataPool() with go-ceph Fixes: #1554 Signed-off-by: Niels de Vos --- internal/cephfs/cephfs_util.go | 38 +++++++++++--------------------- internal/cephfs/volumeoptions.go | 6 ++--- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/internal/cephfs/cephfs_util.go b/internal/cephfs/cephfs_util.go index 849c00fa2..ac4f3872d 100644 --- a/internal/cephfs/cephfs_util.go +++ b/internal/cephfs/cephfs_util.go @@ -46,38 +46,26 @@ func (vo *volumeOptions) getFscID(ctx context.Context) (int64, error) { return 0, ErrVolumeNotFound } -// CephFilesystem is a representation of the json structure returned by 'ceph fs ls'. -type CephFilesystem struct { - Name string `json:"name"` - MetadataPool string `json:"metadata_pool"` - MetadataPoolID int `json:"metadata_pool_id"` - DataPools []string `json:"data_pools"` - DataPoolIDs []int `json:"data_pool_ids"` -} - -func (vo *volumeOptions) getMetadataPool(ctx context.Context, cr *util.Credentials) (string, error) { - // ./tbox ceph fs ls --format=json - // [{"name":"myfs","metadata_pool":"myfs-metadata","metadata_pool_id":4,...},...] - var filesystems []CephFilesystem - err := execCommandJSON(ctx, &filesystems, - "ceph", - "-m", vo.Monitors, - "--id", cr.ID, - "--keyfile="+cr.KeyFile, - "-c", util.CephConfigPath, - "fs", "ls", "--format=json", - ) +func (vo *volumeOptions) getMetadataPool(ctx context.Context) (string, error) { + fsa, err := vo.conn.GetFSAdmin() if err != nil { + util.ErrorLog(ctx, "could not get FSAdmin, can not fetch metadata pool for %s:", vo.FsName, err) return "", err } - for _, fs := range filesystems { - if fs.Name == vo.FsName { - return fs.MetadataPool, nil + fsPoolInfos, err := fsa.ListFileSystems() + if err != nil { + util.ErrorLog(ctx, "could not list filesystems, can not fetch metadata pool for %s:", vo.FsName, err) + return "", err + } + + for _, fspi := range fsPoolInfos { + if fspi.Name == vo.FsName { + return fspi.MetadataPool, nil } } - return "", fmt.Errorf("%w: fsName (%s) not found in Ceph cluster", util.ErrPoolNotFound, vo.FsName) + return "", fmt.Errorf("%w: could not find metadata pool for %s", util.ErrPoolNotFound, vo.FsName) } func (vo *volumeOptions) getFsName(ctx context.Context) (string, error) { diff --git a/internal/cephfs/volumeoptions.go b/internal/cephfs/volumeoptions.go index ff51bfd99..2c1790e2b 100644 --- a/internal/cephfs/volumeoptions.go +++ b/internal/cephfs/volumeoptions.go @@ -220,7 +220,7 @@ func newVolumeOptions(ctx context.Context, requestName string, req *csi.CreateVo return nil, err } - opts.MetadataPool, err = opts.getMetadataPool(ctx, cr) + opts.MetadataPool, err = opts.getMetadataPool(ctx) if err != nil { return nil, err } @@ -295,7 +295,7 @@ func newVolumeOptionsFromVolID(ctx context.Context, volID string, volOpt, secret return nil, nil, err } - volOptions.MetadataPool, err = volOptions.getMetadataPool(ctx, cr) + volOptions.MetadataPool, err = volOptions.getMetadataPool(ctx) if err != nil { return nil, nil, err } @@ -503,7 +503,7 @@ func newSnapshotOptionsFromID(ctx context.Context, snapID string, cr *util.Crede return &volOptions, nil, &sid, err } - volOptions.MetadataPool, err = volOptions.getMetadataPool(ctx, cr) + volOptions.MetadataPool, err = volOptions.getMetadataPool(ctx) if err != nil { return &volOptions, nil, &sid, err }