Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[controller] Fix sds-health-watcher controller's incorrect used nodes count #92

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading