Skip to content

Commit

Permalink
[controller] Fix sds-health-watcher controller's incorrect used nodes…
Browse files Browse the repository at this point in the history
… count (#92)

Signed-off-by: Viktor Kramarenko <[email protected]>
  • Loading branch information
ViktorKram authored Sep 5, 2024
1 parent f525d7d commit 64f17b7
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 9 deletions.
5 changes: 3 additions & 2 deletions images/agent/src/pkg/controller/lvm_volume_group_discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
{
Expand Down

0 comments on commit 64f17b7

Please sign in to comment.