From 46c2bef9bc30cadd392a8a2478f913057ad63e8a Mon Sep 17 00:00:00 2001 From: zhucan Date: Mon, 20 Feb 2023 11:53:59 +0800 Subject: [PATCH] file: check if fs exists before checking dependencies Signed-off-by: zhucan --- pkg/daemon/ceph/client/filesystem.go | 14 ++++++----- pkg/operator/ceph/file/dependent.go | 30 +++++++++++++++++++++--- pkg/operator/ceph/file/dependent_test.go | 20 ++++++++++++++++ pkg/util/exec/error.go | 3 +++ 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/pkg/daemon/ceph/client/filesystem.go b/pkg/daemon/ceph/client/filesystem.go index 70849e5bf..48176d779 100644 --- a/pkg/daemon/ceph/client/filesystem.go +++ b/pkg/daemon/ceph/client/filesystem.go @@ -96,8 +96,10 @@ func ListFilesystems(context *clusterd.Context, clusterInfo *ClusterInfo) ([]Cep return filesystems, nil } -// GetFilesystem gets detailed status information about a Ceph filesystem. -func GetFilesystem(context *clusterd.Context, clusterInfo *ClusterInfo, fsName string) (*CephFilesystemDetails, error) { +var GetFilesystem = getFilesystem + +// getFilesystem gets detailed status information about a Ceph filesystem. +func getFilesystem(context *clusterd.Context, clusterInfo *ClusterInfo, fsName string) (*CephFilesystemDetails, error) { args := []string{"fs", "get", fsName} buf, err := NewCephCommand(context, clusterInfo, args).Run() if err != nil { @@ -183,7 +185,7 @@ func SetNumMDSRanks(context *clusterd.Context, clusterInfo *ClusterInfo, fsName // FailAllStandbyReplayMDS: fail all mds in up:standby-replay state func FailAllStandbyReplayMDS(context *clusterd.Context, clusterInfo *ClusterInfo, fsName string) error { - fs, err := GetFilesystem(context, clusterInfo, fsName) + fs, err := getFilesystem(context, clusterInfo, fsName) if err != nil { return errors.Wrapf(err, "failed to fail standby-replay MDSes for fs %q", fsName) } @@ -199,7 +201,7 @@ func FailAllStandbyReplayMDS(context *clusterd.Context, clusterInfo *ClusterInfo // GetMdsIdByRank get mds ID from the given rank func GetMdsIdByRank(context *clusterd.Context, clusterInfo *ClusterInfo, fsName string, rank int32) (string, error) { - fs, err := GetFilesystem(context, clusterInfo, fsName) + fs, err := getFilesystem(context, clusterInfo, fsName) if err != nil { return "", errors.Wrap(err, "failed to get ceph fs dump") } @@ -230,7 +232,7 @@ func WaitForActiveRanks( logger.Infof("waiting %.2f second(s) for number of active mds daemons for fs %s to become %s", float64(timeout/time.Second), fsName, countText) err := wait.Poll(3*time.Second, timeout, func() (bool, error) { - fs, err := GetFilesystem(context, clusterInfo, fsName) + fs, err := getFilesystem(context, clusterInfo, fsName) if err != nil { logger.Errorf( "Error getting filesystem %q details while waiting for num mds ranks to become %d. %v", @@ -295,7 +297,7 @@ func FailFilesystem(context *clusterd.Context, clusterInfo *ClusterInfo, fsName // RemoveFilesystem performs software configuration steps to remove a Ceph filesystem and its // backing pools. func RemoveFilesystem(context *clusterd.Context, clusterInfo *ClusterInfo, fsName string, preservePoolsOnDelete bool) error { - fs, err := GetFilesystem(context, clusterInfo, fsName) + fs, err := getFilesystem(context, clusterInfo, fsName) if err != nil { return errors.Wrapf(err, "filesystem %s not found", fsName) } diff --git a/pkg/operator/ceph/file/dependent.go b/pkg/operator/ceph/file/dependent.go index ebbe62402..7578a41fd 100644 --- a/pkg/operator/ceph/file/dependent.go +++ b/pkg/operator/ceph/file/dependent.go @@ -18,13 +18,16 @@ package file import ( "fmt" + "syscall" "github.com/pkg/errors" v1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1" "github.com/rook/rook/pkg/clusterd" "github.com/rook/rook/pkg/daemon/ceph/client" + cephclient "github.com/rook/rook/pkg/daemon/ceph/client" "github.com/rook/rook/pkg/util" "github.com/rook/rook/pkg/util/dependents" + kexec "github.com/rook/rook/pkg/util/exec" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -50,15 +53,36 @@ var ignoredDependentSubvolumeGroups = []string{"_nogroup", "_index", "_legacy", // groups with no subvolumes don't block deletion. var CephFilesystemDependents = cephFilesystemDependents +// check filesystem whether it exists +func filesystemExists(clusterdCtx *clusterd.Context, clusterInfo *client.ClusterInfo, name, nsName string) (bool, error) { + _, err := cephclient.GetFilesystem(clusterdCtx, clusterInfo, name) + if err != nil { + if code, ok := kexec.ExitStatus(err); ok && code == int(syscall.ENOENT) { + logger.Infof("filesystem %q deletion will continue without checking for dependencies since the the filesystem does not exist within Ceph", nsName) + return false, nil + } + return false, errors.Wrapf(err, "failed to check for existence of CephFilesystem %q", nsName) + } + return true, nil +} + // with above, allow this to be overridden for unit testing func cephFilesystemDependents(clusterdCtx *clusterd.Context, clusterInfo *client.ClusterInfo, filesystem *v1.CephFilesystem) (*dependents.DependentList, error) { nsName := fmt.Sprintf("%s/%s", filesystem.Namespace, filesystem.Name) baseErrMsg := fmt.Sprintf("failed to get dependents of CephFilesystem %q", nsName) - // subvolume groups that contain subvolumes - deps, err := subvolumeGroupDependents(clusterdCtx, clusterInfo, filesystem) + deps := dependents.NewDependentList() + fsExists, err := filesystemExists(clusterdCtx, clusterInfo, filesystem.Name, nsName) if err != nil { - return deps, errors.Wrapf(err, baseErrMsg) + return deps, nil + } + + // subvolume groups that contain subvolumes + if fsExists { + deps, err = subvolumeGroupDependents(clusterdCtx, clusterInfo, filesystem) + if err != nil { + return deps, errors.Wrapf(err, baseErrMsg) + } } // CephFilesystemSubVolumeGroups diff --git a/pkg/operator/ceph/file/dependent_test.go b/pkg/operator/ceph/file/dependent_test.go index 41a44b2e7..ab88151ac 100644 --- a/pkg/operator/ceph/file/dependent_test.go +++ b/pkg/operator/ceph/file/dependent_test.go @@ -19,6 +19,7 @@ package file import ( "context" "fmt" + "syscall" "testing" "github.com/pkg/errors" @@ -75,8 +76,27 @@ func TestCephFilesystemDependents(t *testing.T) { ) (client.SubvolumeList, error) { return client.SubvolumeList{}, nil } + noCephFilesystem := func( + context *clusterd.Context, clusterInfo *client.ClusterInfo, fsName string, + ) (*client.CephFilesystemDetails, error) { + return nil, syscall.Errno(2) + } + existsCephFilesystem := func( + context *clusterd.Context, clusterInfo *client.ClusterInfo, fsName string, + ) (*client.CephFilesystemDetails, error) { + return &client.CephFilesystemDetails{}, nil + } + + t.Run("no CephFilesystem found", func(t *testing.T) { + client.GetFilesystem = noCephFilesystem + c := newClusterdCtx() + deps, err := CephFilesystemDependents(c, clusterInfo, fs) + assert.NoError(t, err) + assert.True(t, deps.Empty()) + }) t.Run("no blocking dependents", func(t *testing.T) { + client.GetFilesystem = existsCephFilesystem client.ListSubvolumeGroups = noSubvolumeGroups client.ListSubvolumesInGroup = noSubvolumes diff --git a/pkg/util/exec/error.go b/pkg/util/exec/error.go index 41945b50e..55cebc599 100644 --- a/pkg/util/exec/error.go +++ b/pkg/util/exec/error.go @@ -44,6 +44,9 @@ func ExitStatus(err error) (int, bool) { case *CephCLIError: return ExitStatus(e.err) + case syscall.Errno: + return int(e), true + } return 0, false }