From 64f17b70f035f60a32952d1a7b952c244677449c Mon Sep 17 00:00:00 2001 From: Viktor Kram <92625690+ViktorKram@users.noreply.github.com> Date: Thu, 5 Sep 2024 15:33:34 +0300 Subject: [PATCH] [controller] Fix sds-health-watcher controller's incorrect used nodes count (#92) Signed-off-by: Viktor Kramarenko --- .../controller/lvm_volume_group_discover.go | 5 +- .../src/pkg/controller/sds_infra_watcher.go | 6 +- .../pkg/controller/sds_infra_watcher_funcs.go | 10 +- .../pkg/controller/sds_infra_watcher_test.go | 104 ++++++++++++++++++ 4 files changed, 116 insertions(+), 9 deletions(-) diff --git a/images/agent/src/pkg/controller/lvm_volume_group_discover.go b/images/agent/src/pkg/controller/lvm_volume_group_discover.go index f4f3d25d..6ae11e0c 100644 --- a/images/agent/src/pkg/controller/lvm_volume_group_discover.go +++ b/images/agent/src/pkg/controller/lvm_volume_group_discover.go @@ -960,8 +960,9 @@ func convertSpecThinPools(thinPools map[string]resource.Quantity) []v1alpha1.Lvm result := make([]v1alpha1.LvmVolumeGroupThinPoolSpec, 0, len(thinPools)) for name, size := range thinPools { result = append(result, v1alpha1.LvmVolumeGroupThinPoolSpec{ - Name: name, - Size: size.String(), + Name: name, + AllocationLimit: "150%", + Size: size.String(), }) } diff --git a/images/sds-health-watcher-controller/src/pkg/controller/sds_infra_watcher.go b/images/sds-health-watcher-controller/src/pkg/controller/sds_infra_watcher.go index 3c813043..177d9c05 100644 --- a/images/sds-health-watcher-controller/src/pkg/controller/sds_infra_watcher.go +++ b/images/sds-health-watcher-controller/src/pkg/controller/sds_infra_watcher.go @@ -73,11 +73,11 @@ func RunSdsInfraWatcher( log.Info("[RunSdsInfraWatcher] LVMVolumeGroups found. Starts to check their health") log.Info("[RunSdsInfraWatcher] check if every LVMVolumeGroup node does exist") - lvgNodes := getNodeNamesFromLVGs(lvgs) - log.Trace(fmt.Sprintf("[RunSdsInfraWatcher] used nodes %v", lvgNodes)) + lvgNodeNames := getNodeNamesFromLVGs(lvgs) + log.Trace(fmt.Sprintf("[RunSdsInfraWatcher] used nodes %v", lvgNodeNames)) log.Debug("[RunSdsInfraWatcher] tries to collect nodes used by LVMVolumeGroups") - usedNodes, missedNodes, err := getNodesByNames(ctx, cl, lvgNodes) + usedNodes, missedNodes, err := getNodesByNames(ctx, cl, lvgNodeNames) if err != nil { log.Error(err, "[RunSdsInfraWatcher] unable to get nodes") continue diff --git a/images/sds-health-watcher-controller/src/pkg/controller/sds_infra_watcher_funcs.go b/images/sds-health-watcher-controller/src/pkg/controller/sds_infra_watcher_funcs.go index 936e4055..0b269a43 100644 --- a/images/sds-health-watcher-controller/src/pkg/controller/sds_infra_watcher_funcs.go +++ b/images/sds-health-watcher-controller/src/pkg/controller/sds_infra_watcher_funcs.go @@ -116,7 +116,7 @@ func findLVMVolumeGroupsByNodeNames(lvgs map[string]v1alpha1.LvmVolumeGroup, nod return result } -func getNodesByNames(ctx context.Context, cl client.Client, names []string) (map[string]v1.Node, []string, error) { +func getNodesByNames(ctx context.Context, cl client.Client, lvgNodeNames []string) (map[string]v1.Node, []string, error) { nodeList := &v1.NodeList{} err := cl.List(ctx, nodeList) @@ -129,14 +129,16 @@ func getNodesByNames(ctx context.Context, cl client.Client, names []string) (map nodes[n.Name] = n } - missedNodes := make([]string, 0, len(names)) - for _, name := range names { + missedNodes := make([]string, 0, len(lvgNodeNames)) + usedNodes := make(map[string]v1.Node, len(lvgNodeNames)) + for _, name := range lvgNodeNames { if _, exist := nodes[name]; !exist { missedNodes = append(missedNodes, name) } + usedNodes[name] = nodes[name] } - return nodes, missedNodes, nil + return usedNodes, missedNodes, nil } func getNodeNamesFromLVGs(lvgs map[string]v1alpha1.LvmVolumeGroup) []string { diff --git a/images/sds-health-watcher-controller/src/pkg/controller/sds_infra_watcher_test.go b/images/sds-health-watcher-controller/src/pkg/controller/sds_infra_watcher_test.go index f33ddf7b..1894d8cd 100644 --- a/images/sds-health-watcher-controller/src/pkg/controller/sds_infra_watcher_test.go +++ b/images/sds-health-watcher-controller/src/pkg/controller/sds_infra_watcher_test.go @@ -6,6 +6,7 @@ import ( "github.com/deckhouse/sds-node-configurator/api/v1alpha1" "github.com/stretchr/testify/assert" + coreV1 "k8s.io/api/core/v1" v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/scheme" @@ -21,6 +22,109 @@ func TestHealthWatcher(t *testing.T) { ctx := context.Background() metrics := monitoring.GetMetrics("") + t.Run("getNodesByNames", func(t *testing.T) { + t.Run("returns_correct_used_nodes", func(t *testing.T) { + lvgNodeNames := []string{"test-node1", "test-node2"} + nodes := coreV1.NodeList{ + Items: []coreV1.Node{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "test-node1", + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "test-node2", + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "test-node3", + }, + }, + }, + } + + for _, n := range nodes.Items { + err := cl.Create(ctx, &n) + if err != nil { + t.Error() + } + } + + defer func() { + for _, n := range nodes.Items { + err := cl.Delete(ctx, &n) + if err != nil { + t.Error(err) + } + } + }() + + usedNodes, missedNodes, err := getNodesByNames(ctx, cl, lvgNodeNames) + if err != nil { + t.Error(err) + } + + if assert.Equal(t, 0, len(missedNodes)) { + assert.Equal(t, 2, len(usedNodes)) + + for _, name := range lvgNodeNames { + _, ok := usedNodes[name] + assert.True(t, ok) + } + } + }) + + t.Run("returns_correct_missed_nodes", func(t *testing.T) { + lvgNodeNames := []string{"test-node1", "test-node2"} + nodes := coreV1.NodeList{ + Items: []coreV1.Node{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "test-node1", + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "test-node4", + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "test-node3", + }, + }, + }, + } + + for _, n := range nodes.Items { + err := cl.Create(ctx, &n) + if err != nil { + t.Error() + } + } + + defer func() { + for _, n := range nodes.Items { + err := cl.Delete(ctx, &n) + if err != nil { + t.Error(err) + } + } + }() + + expectedMissed := []string{"test-node2"} + + _, missedNodes, err := getNodesByNames(ctx, cl, lvgNodeNames) + if err != nil { + t.Error(err) + } + + assert.ElementsMatch(t, expectedMissed, missedNodes) + }) + }) + t.Run("GetLVMVolumeGroups_returns_lvgs", func(t *testing.T) { lvgsToCreate := []v1alpha1.LvmVolumeGroup{ {