Skip to content

Commit

Permalink
Merge pull request #458 from k8s-infra-cherrypick-robot/cherry-pick-4…
Browse files Browse the repository at this point in the history
…57-to-release-1.4

[release-1.4] Fix pointer issue for lock release controller
  • Loading branch information
k8s-ci-robot authored Mar 22, 2023
2 parents ce426e2 + 7c3d302 commit 79690b7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/releaselock/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (c *LockReleaseController) listNodes(ctx context.Context) (map[string]*core
}
nodeMap := map[string]*corev1.Node{}
for _, node := range nodeList.Items {
nodeMap[node.Name] = &node
nodeMap[node.Name] = node.DeepCopy()
}
return nodeMap, nil
}
48 changes: 48 additions & 0 deletions pkg/releaselock/controller_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package rpc

import (
"context"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
)

func TestVerifyNodeExists(t *testing.T) {
Expand Down Expand Up @@ -117,6 +120,51 @@ func TestVerifyNodeExists(t *testing.T) {
}
}

func TestListNodes(t *testing.T) {
node1 := &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
Annotations: map[string]string{
gceInstanceIDKey: "node1-id",
},
},
}
node2 := &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node2",
Annotations: map[string]string{
gceInstanceIDKey: "node2-id",
},
},
}
controller := LockReleaseController{client: fake.NewSimpleClientset(node1, node2)}
expectedMap := map[string]*corev1.Node{
"node1": {
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
Annotations: map[string]string{
gceInstanceIDKey: "node1-id",
},
},
},
"node2": {
ObjectMeta: metav1.ObjectMeta{
Name: "node2",
Annotations: map[string]string{
gceInstanceIDKey: "node2-id",
},
},
},
}
nodes, err := controller.listNodes(context.Background())
if err != nil {
t.Fatalf("test listNodes failed: unexpected error: %v", err)
}
if diff := cmp.Diff(expectedMap, nodes); diff != "" {
t.Errorf("test listNodes failed: unexpected diff (-want +got):%s", diff)
}
}

func gotExpectedError(testFunc string, wantErr bool, err error) error {
if err != nil && !wantErr {
return fmt.Errorf("%s got error %v, want nil", testFunc, err)
Expand Down

0 comments on commit 79690b7

Please sign in to comment.