pool: fix CephBlockPool enableRBDStats not clearing rbd_stats_pools

This PR fixes an issue where disabling enableRBDStats in CephBlockPool did not remove
the pool from rbd_stats_pools, leading to unnecessary monitoring.
The update ensures that when enableRBDStats is set to false,
the pool is properly removed from rbd_stats_pools, optimizing resource tracking.

Signed-off-by: Oded Viner <oviner@redhat.com>
This commit is contained in:
Oded Viner committed 2024-11-05 15:03:21 +02:00
1 parent f8a820683e
commit 8dea77255b
2 files changed
+152 -29

No files matched your search

+43 -26
View File
@@ -21,6 +21,7 @@ import (
"context"
"fmt"
"reflect"
"sort"
"strings"
"github.com/coreos/pkg/capnslog"
@@ -419,35 +420,50 @@ func deletePool(context *clusterd.Context, clusterInfo *cephclient.ClusterInfo,
return nil
}
// 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:]...)
// generateStatsPoolList combines existingStatsPools and rookStatsPools, removes items in removePools,
// removes duplicates, ensures no empty strings, and returns a comma-separated string in a deterministic order.
func generateStatsPoolList(existingStatsPools []string, rookStatsPools []string, removePools []string) string {
poolList := []string{}
// Helper function to add a poolList if it's not in the removePools list and not already in poolList
addUniquePool := func(pool string) {
if pool == "" {
return
}
// Check if the pool should be removed or already exists in poolList
if contains(removePools, pool) || contains(poolList, pool) {
return
}
poolList = append(poolList, pool)
}
for _, pool := range existingStatsPools {
addUniquePool(pool)
}
for _, pool := range rookStatsPools {
addUniquePool(pool)
}
return list
sort.Strings(poolList) // Sort the list to ensure deterministic output
return strings.Join(poolList, ",")
}
// 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)
// Helper function to check if a slice contains a string
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return result
return false
}
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 enableStatsForCephBlockPools []string
var rookStatsPools []string
var removePools []string
err := clusterContext.Client.List(clusterInfo.Context, cephBlockPoolList, namespaceListOpt)
if err != nil {
return errors.Wrap(err, "failed to retrieve list of CephBlockPool")
@@ -455,28 +471,29 @@ 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
enableStatsForCephBlockPools = append(enableStatsForCephBlockPools, cephBlockPool.ToNamedPoolSpec().Name)
rookStatsPools = append(rookStatsPools, cephBlockPool.ToNamedPoolSpec().Name)
} else {
removePools = append(removePools, cephBlockPool.ToNamedPoolSpec().Name)
}
}
enableStatsForCephBlockPools = remove(enableStatsForCephBlockPools, deletedPool)
if deletedPool != "" {
removePools = append(removePools, deletedPool)
}
monStore := config.GetMonStore(clusterContext, clusterInfo)
// Check for existing rbd stats pools
existingRBDStatsPools, e := monStore.Get("mgr", "mgr/prometheus/rbd_stats_pools")
existingStatsPools, 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)
existingStatsPoolsList := strings.Split(existingStatsPools, ",")
enableStatsForPools := generateStatsPoolList(existingStatsPoolsList, rookStatsPools, removePools)
logger.Infof("enableStatsForPools=%q ", 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 {
// appending existing rbd stats pools if any
err = monStore.Set("mgr", "mgr/prometheus/rbd_stats_pools", strings.Trim(strings.Join(enableStatsForPools, ","), ","))
err = monStore.Set("mgr", "mgr/prometheus/rbd_stats_pools", enableStatsForPools)
}
if err != nil {
return errors.Wrapf(err, "failed to enable rbd_stats_pools")
+109 -3
View File
@@ -635,12 +635,14 @@ func TestConfigureRBDStats(t *testing.T) {
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" {
mockedPools = args[4]
return "", nil
}
if args[1] == "get" {
return mockedPools, nil
}
if args[1] == "rm" {
mockedPools = ""
return "", nil
}
}
@@ -701,7 +703,6 @@ func TestConfigureRBDStats(t *testing.T) {
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)
@@ -710,7 +711,6 @@ func TestConfigureRBDStats(t *testing.T) {
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)
@@ -726,7 +726,7 @@ func TestConfigureRBDStats(t *testing.T) {
rbdStatsPools, err = monStore.Get("mgr", "mgr/prometheus/rbd_stats_pools")
assert.Nil(t, err)
assert.Equal(t, "pool1,pool2", rbdStatsPools)
assert.Equal(t, "my-pool-with-rbd-stats,pool1,pool2", rbdStatsPools)
// Case 8: Two CephBlockPools with EnableRBDStats:false & EnableRBDStats:true.
// SetConfig returns an error
@@ -738,5 +738,111 @@ func TestConfigureRBDStats(t *testing.T) {
}
err = configureRBDStats(context, clusterInfo, "")
assert.NotNil(t, err)
}
func TestGenerateStatsPoolList(t *testing.T) {
tests := []struct {
name string
existingStatsPools []string
rookStatsPools []string
removePools []string
expectedOutput string
}{
// Basic cases
{
name: "Empty lists",
existingStatsPools: []string{},
rookStatsPools: []string{},
removePools: []string{},
expectedOutput: "",
},
{
name: "Single-item lists, no removal",
existingStatsPools: []string{"p1"},
rookStatsPools: []string{"p2"},
removePools: []string{},
expectedOutput: "p1,p2",
},
// Overlap and duplicates
{
name: "Overlapping pools, some to remove",
existingStatsPools: []string{"p1", "p2", "p3"},
rookStatsPools: []string{"p2", "p4", "p5"},
removePools: []string{"p1", "p5"},
expectedOutput: "p2,p3,p4",
},
{
name: "Non-overlapping lists",
existingStatsPools: []string{"p1", "p2"},
rookStatsPools: []string{"p3", "p4"},
removePools: []string{},
expectedOutput: "p1,p2,p3,p4",
},
// All pools removed
{
name: "All pools removed",
existingStatsPools: []string{"p1", "p2"},
rookStatsPools: []string{"p2", "p3"},
removePools: []string{"p1", "p2", "p3"},
expectedOutput: "",
},
// Mixed scenarios with edge cases
{
name: "Only removed pools",
existingStatsPools: []string{"p1", "p2"},
rookStatsPools: []string{"p2", "p3"},
removePools: []string{"p1", "p2", "p3", "p4"},
expectedOutput: "",
},
{
name: "Duplicate pools across lists",
existingStatsPools: []string{"p1", "p2", "p1"},
rookStatsPools: []string{"p2", "p3", "p3"},
removePools: []string{},
expectedOutput: "p1,p2,p3",
},
{
name: "Empty string in pools",
existingStatsPools: []string{"p1", ""},
rookStatsPools: []string{"p2", ""},
removePools: []string{""},
expectedOutput: "p1,p2",
},
{
name: "Empty string in remove pools",
existingStatsPools: []string{"p1", "p2"},
rookStatsPools: []string{"p3", "p4"},
removePools: []string{""},
expectedOutput: "p1,p2,p3,p4",
},
{
name: "All lists empty strings",
existingStatsPools: []string{""},
rookStatsPools: []string{""},
removePools: []string{""},
expectedOutput: "",
},
// Larger cases
{
name: "Large unique pool list",
existingStatsPools: []string{"p1", "p2", "p3", "p4"},
rookStatsPools: []string{"p5", "p6", "p7", "p8"},
removePools: []string{"p1", "p8"},
expectedOutput: "p2,p3,p4,p5,p6,p7",
},
{
name: "Large list with many duplicates",
existingStatsPools: []string{"p1", "p2", "p3", "p2", "p1"},
rookStatsPools: []string{"p2", "p3", "p3", "p4", "p1"},
removePools: []string{"p4"},
expectedOutput: "p1,p2,p3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := generateStatsPoolList(tt.existingStatsPools, tt.rookStatsPools, tt.removePools)
assert.Equal(t, tt.expectedOutput, result)
})
}
}