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

Detect Zone info in node labels #811

Merged
merged 8 commits into from
Dec 6, 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
10 changes: 10 additions & 0 deletions controllers/csm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1517,3 +1517,13 @@ func (r *ContainerStorageModuleReconciler) GetUpdateCount() int32 {
func (r *ContainerStorageModuleReconciler) GetK8sClient() kubernetes.Interface {
return r.K8sClient
}

func (r *ContainerStorageModuleReconciler) GetMatchingNodes(ctx context.Context, labelKey string, labelValue string) (*corev1.NodeList, error) {
nodeList := &corev1.NodeList{}
opts := []client.ListOption{
client.MatchingLabels{labelKey: labelValue},
}
err := r.List(ctx, nodeList, opts...)

return nodeList, err
}
54 changes: 54 additions & 0 deletions controllers/csm_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2382,3 +2382,57 @@ func (suite *CSMControllerTestSuite) makeFakeRevProxyCSM(name string, ns string,
err = suite.fakeClient.Create(ctx, &csm)
assert.Nil(suite.T(), err)
}

func (suite *CSMControllerTestSuite) TestGetNodeLabels() {
// TBD: crclient/client.go needs to be augmented to filter on labels during
// the List return for a viable thorough test. Since this functionality is
// missing, this test is quite elementary as a result.

csm := shared.MakeCSM(csmName, suite.namespace, configVersion)
csm.Spec.Driver.CSIDriverType = csmv1.PowerScale
csm.Spec.Driver.Common.Image = "image"
csm.Annotations[configVersionKey] = configVersion

sec := shared.MakeSecret(csmName+"-creds", suite.namespace, configVersion)
err := suite.fakeClient.Create(ctx, sec)
if err != nil {
panic(err)
}

csm.ObjectMeta.Finalizers = []string{CSMFinalizerName}
err = suite.fakeClient.Create(ctx, &csm)
if err != nil {
panic(err)
}

reconciler := suite.createReconciler()

// create node object, add to fakeclient, reconcile.GetMatchingNodes
node := shared.MakeNode("node1", suite.namespace)
node.Labels["topology.kubernetes.io/zone"] = "US-EAST"

err = suite.fakeClient.Create(ctx, &node)
assert.Nil(suite.T(), err)

nodeList := &corev1.NodeList{}
err = suite.fakeClient.List(ctx, nodeList, nil)
assert.Nil(suite.T(), err)

nodeListMatching, err := reconciler.GetMatchingNodes(ctx, "topology.kubernetes.io/zone", "US-EAST")
ctrl.Log.Info("node list response (1)", "number of nodes is: ", len(nodeListMatching.Items))

// Check the len to be 1 else fail
if len(nodeListMatching.Items) != 1 {
ctrl.Log.Error(err, "Unexpected length on node list.", "length", len(nodeListMatching.Items))
panic(err)
}

for _, node := range nodeListMatching.Items {
ctrl.Log.Info("Matching node item", "name ", node.ObjectMeta.GetName())
}
if node.ObjectMeta.GetName() != "node1" {
ctrl.Log.Error(err, "Unmatched label on node.")
panic(err)
}
assert.Nil(suite.T(), err)
}
13 changes: 13 additions & 0 deletions tests/shared/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ func MakePod(name, ns string) corev1.Pod {
return podObj
}

// MakeNode returns a node object
func MakeNode(name, ns string) corev1.Node {
nodeObj := corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ns,
Labels: map[string]string{},
},
}

return nodeObj
}

// MakeReverseProxyModule returns a csireverseproxy object
func MakeReverseProxyModule(_ string) csmv1.Module {
revproxy := csmv1.Module{
Expand Down
14 changes: 14 additions & 0 deletions tests/shared/crclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ func (f Client) List(ctx context.Context, list client.ObjectList, _ ...client.Li
switch l := list.(type) {
case *corev1.PodList:
return f.listPodList(l)
case *corev1.NodeList:
return f.listNodeList(l)
case *appsv1.DeploymentList:
return f.listDeploymentList(ctx, &appsv1.DeploymentList{})
default:
Expand All @@ -122,6 +124,18 @@ func (f Client) listPodList(list *corev1.PodList) error {
return nil
}

func (f Client) listNodeList(list *corev1.NodeList) error {
for k, v := range f.Objects {
if k.Kind == "Node" {
node, ok := v.(*corev1.Node)
if ok {
list.Items = append(list.Items, *node)
}
}
}
return nil
}

func (f Client) listDeploymentList(_ context.Context, list *appsv1.DeploymentList) error {
for k, v := range f.Objects {
if k.Kind == "Deployment" {
Expand Down
Loading