From 993942b891e4c7f3ecebd3f7ea04d85fda115922 Mon Sep 17 00:00:00 2001 From: Avan Thakkar Date: Tue, 4 Oct 2022 20:50:28 +0530 Subject: [PATCH] operator: don't remove existing pools for mgr/prometheus/rbd_stats_pools Rook currently knows rbd pools only if they defined through CephBlockPool, so it overrides the config mgr/prometheus/rbd_stats_pools which may have already been set for pools external to Rook. Check config before running set command and if there some existing pools append them to enableStatsForCephBlockPools. Signed-off-by: Avan Thakkar (cherry picked from commit e8b74207dd051829f18b7924412b1d449cd71fb5) --- pkg/operator/ceph/pool/controller.go | 51 +++++++++++++++++++---- pkg/operator/ceph/pool/controller_test.go | 50 ++++++++++++++++++---- 2 files changed, 86 insertions(+), 15 deletions(-) diff --git a/pkg/operator/ceph/pool/controller.go b/pkg/operator/ceph/pool/controller.go index b9794ee30..4f157e9e2 100644 --- a/pkg/operator/ceph/pool/controller.go +++ b/pkg/operator/ceph/pool/controller.go @@ -245,7 +245,7 @@ func (r *ReconcileCephBlockPool) reconcile(request reconcile.Request) (reconcile } // disable RBD stats collection if cephBlockPool was deleted - if err := configureRBDStats(r.context, clusterInfo); err != nil { + if err := configureRBDStats(r.context, clusterInfo, cephBlockPool.Name); err != nil { logger.Errorf("failed to disable stats collection for pool(s). %v", err) } @@ -290,7 +290,7 @@ func (r *ReconcileCephBlockPool) reconcile(request reconcile.Request) (reconcile } // enable/disable RBD stats collection based on cephBlockPool spec - if err := configureRBDStats(r.context, clusterInfo); err != nil { + if err := configureRBDStats(r.context, clusterInfo, ""); err != nil { return reconcile.Result{}, *cephBlockPool, errors.Wrap(err, "failed to enable/disable stats collection for pool(s)") } @@ -418,11 +418,35 @@ func deletePool(context *clusterd.Context, clusterInfo *cephclient.ClusterInfo, return nil } -func configureRBDStats(clusterContext *clusterd.Context, clusterInfo *cephclient.ClusterInfo) error { +// remove removes any element from a list +func remove(list []string, s string) []string { + for i, v := range list { + if v == s { + list = append(list[:i], list[i+1:]...) + } + } + + return list +} + +// Remove duplicate entries from slice +func removeDuplicates(slice []string) []string { + inResult := make(map[string]bool) + var result []string + for _, str := range slice { + if _, ok := inResult[str]; !ok { + inResult[str] = true + result = append(result, str) + } + } + return result +} + +func configureRBDStats(clusterContext *clusterd.Context, clusterInfo *cephclient.ClusterInfo, deletedPool string) error { logger.Debug("configuring RBD per-image IO statistics collection") namespaceListOpt := client.InNamespace(clusterInfo.Namespace) cephBlockPoolList := &cephv1.CephBlockPoolList{} - var enableStatsForPools []string + var enableStatsForCephBlockPools []string err := clusterContext.Client.List(clusterInfo.Context, cephBlockPoolList, namespaceListOpt) if err != nil { return errors.Wrap(err, "failed to retrieve list of CephBlockPool") @@ -430,15 +454,28 @@ func configureRBDStats(clusterContext *clusterd.Context, clusterInfo *cephclient for _, cephBlockPool := range cephBlockPoolList.Items { if cephBlockPool.GetDeletionTimestamp() == nil && cephBlockPool.Spec.EnableRBDStats { // add to list of CephBlockPool with enableRBDStats set to true and not marked for deletion - enableStatsForPools = append(enableStatsForPools, cephBlockPool.ToNamedPoolSpec().Name) + enableStatsForCephBlockPools = append(enableStatsForCephBlockPools, cephBlockPool.ToNamedPoolSpec().Name) } } - logger.Debugf("RBD per-image IO statistics will be collected for pools: %v", enableStatsForPools) + enableStatsForCephBlockPools = remove(enableStatsForCephBlockPools, deletedPool) monStore := config.GetMonStore(clusterContext, clusterInfo) + // Check for existing rbd stats pools + existingRBDStatsPools, e := monStore.Get("mgr", "mgr/prometheus/rbd_stats_pools") + if e != nil { + return errors.Wrapf(e, "failed to get rbd_stats_pools") + } + + existingRBDStatsPoolsList := strings.Split(existingRBDStatsPools, ",") + enableStatsForPools := append(enableStatsForCephBlockPools, existingRBDStatsPoolsList...) + enableStatsForPools = removeDuplicates(enableStatsForPools) + + logger.Debugf("RBD per-image IO statistics will be collected for pools: %v", enableStatsForPools) + if len(enableStatsForPools) == 0 { err = monStore.Delete("mgr", "mgr/prometheus/rbd_stats_pools") } else { - err = monStore.Set("mgr", "mgr/prometheus/rbd_stats_pools", strings.Join(enableStatsForPools, ",")) + // appending existing rbd stats pools if any + err = monStore.Set("mgr", "mgr/prometheus/rbd_stats_pools", strings.Trim(strings.Join(enableStatsForPools, ","), ",")) } if err != nil { return errors.Wrapf(err, "failed to enable rbd_stats_pools") diff --git a/pkg/operator/ceph/pool/controller_test.go b/pkg/operator/ceph/pool/controller_test.go index dfef77c28..70610dda0 100644 --- a/pkg/operator/ceph/pool/controller_test.go +++ b/pkg/operator/ceph/pool/controller_test.go @@ -29,6 +29,7 @@ import ( rookclient "github.com/rook/rook/pkg/client/clientset/versioned/fake" "github.com/rook/rook/pkg/clusterd" cephclient "github.com/rook/rook/pkg/daemon/ceph/client" + "github.com/rook/rook/pkg/operator/ceph/config" opcontroller "github.com/rook/rook/pkg/operator/ceph/controller" "github.com/rook/rook/pkg/operator/k8sutil" testop "github.com/rook/rook/pkg/operator/test" @@ -554,15 +555,16 @@ func TestConfigureRBDStats(t *testing.T) { namespace = "rook-ceph" ) + mockedPools := "" executor := &exectest.MockExecutor{ MockExecuteCommandWithTimeout: func(timeout time.Duration, command string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if args[0] == "config" && args[2] == "mgr" && args[3] == "mgr/prometheus/rbd_stats_pools" { - if args[1] == "set" && args[4] != "" { + if args[1] == "set" { return "", nil } if args[1] == "get" { - return "", nil + return mockedPools, nil } if args[1] == "rm" { return "", nil @@ -579,13 +581,13 @@ func TestConfigureRBDStats(t *testing.T) { // Case 1: CephBlockPoolList is not registered in scheme. // So, an error is expected as List() operation would fail. - err := configureRBDStats(context, clusterInfo) + err := configureRBDStats(context, clusterInfo, "") assert.NotNil(t, err) s.AddKnownTypes(cephv1.SchemeGroupVersion, &cephv1.CephBlockPoolList{}) // Case 2: CephBlockPoolList is registered in schema. // So, no error is expected. - err = configureRBDStats(context, clusterInfo) + err = configureRBDStats(context, clusterInfo, "") assert.Nil(t, err) s.AddKnownTypes(cephv1.SchemeGroupVersion, &cephv1.CephBlockPool{}) @@ -609,7 +611,7 @@ func TestConfigureRBDStats(t *testing.T) { poolWithRBDStatsDisabled, } context.Client = fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build() - err = configureRBDStats(context, clusterInfo) + err = configureRBDStats(context, clusterInfo, "") assert.Nil(t, err) // Case 4: Two CephBlockPools with EnableRBDStats:false & EnableRBDStats:true. @@ -618,10 +620,41 @@ func TestConfigureRBDStats(t *testing.T) { poolWithRBDStatsEnabled.Spec.EnableRBDStats = true objects = append(objects, poolWithRBDStatsEnabled) context.Client = fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build() - err = configureRBDStats(context, clusterInfo) + err = configureRBDStats(context, clusterInfo, "") assert.Nil(t, err) - // Case 5: Two CephBlockPools with EnableRBDStats:false & EnableRBDStats:true. + // Case 5: Two external pools(non CephBlockPools) with rbd_stats_pools config already set + monStore := config.GetMonStore(context, clusterInfo) + e := monStore.Set("mgr", "mgr/prometheus/rbd_stats_pools", "pool1,pool2") + assert.Nil(t, e) + mockedPools = "my-pool-with-rbd-stats,pool1,pool2" + e = configureRBDStats(context, clusterInfo, "") + assert.Nil(t, e) + + rbdStatsPools, err := monStore.Get("mgr", "mgr/prometheus/rbd_stats_pools") + assert.Nil(t, err) + assert.Equal(t, "my-pool-with-rbd-stats,pool1,pool2", rbdStatsPools) + + // Case 6: Deleted CephBlockPool should be excluded from config + mockedPools = "pool1,pool2" + err = configureRBDStats(context, clusterInfo, "my-pool-with-rbd-stats") + assert.Nil(t, err) + + rbdStatsPools, err = monStore.Get("mgr", "mgr/prometheus/rbd_stats_pools") + assert.Nil(t, err) + assert.Equal(t, "pool1,pool2", rbdStatsPools) + + //Case 7: Duplicate entries should be removed from config + e = monStore.Set("mgr", "mgr/prometheus/rbd_stats_pools", "pool1,pool2,pool1") + assert.Nil(t, e) + err = configureRBDStats(context, clusterInfo, "") + assert.Nil(t, err) + + rbdStatsPools, err = monStore.Get("mgr", "mgr/prometheus/rbd_stats_pools") + assert.Nil(t, err) + assert.Equal(t, "pool1,pool2", rbdStatsPools) + + // Case 8: Two CephBlockPools with EnableRBDStats:false & EnableRBDStats:true. // SetConfig returns an error context.Executor = &exectest.MockExecutor{ MockExecuteCommandWithTimeout: func(timeout time.Duration, command string, args ...string) (string, error) { @@ -629,6 +662,7 @@ func TestConfigureRBDStats(t *testing.T) { return "", errors.New("mock error to simulate failure of mon store Set() function") }, } - err = configureRBDStats(context, clusterInfo) + err = configureRBDStats(context, clusterInfo, "") assert.NotNil(t, err) + }