Repository: clusternet/clusternet
Pull request #413 (2022-07-12 05:02:53)

fix bug of aggregate single node labels with no filter
Signed-off-by: lmxia <xialingming@gmail.com>

<!--  Thanks for sending a pull request!

-->

#### What type of PR is this?
kind/bug

#### What this PR does / why we need it:
When cluster has only one node, the labels aggregated from sub-cluster does not filter specific prefixed label.
#### Which issue(s) this PR fixes:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
A follow-up fix of #396 

#### Special notes for your reviewer:


codecov-commenter: # [Codecov](https://codecov.io/gh/clusternet/clusternet/pull/413?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet) Report
> Merging [#413](https://codecov.io/gh/clusternet/clusternet/pull/413?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet) (2a41f72) into [main](https://codecov.io/gh/clusternet/clusternet/commit/34c49d1ff094feaf06a36e03364aa2a05b0445ab?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet) (34c49d1) will **increase** coverage by `0.00%`.
> The diff coverage is `n/a`.

```diff
@@           Coverage Diff           @@
##             main     #413   +/-   ##
=======================================
  Coverage   13.59%   13.59%           
=======================================
  Files          60       60           
  Lines        6577     6574    -3     
=======================================
  Hits          894      894           
+ Misses       5615     5613    -2     
+ Partials       68       67    -1     
```


| [Impacted Files](https://codecov.io/gh/clusternet/clusternet/pull/413?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet) | Coverage Δ | |
|---|---|---|
| [...clusters/clusterstatus/clusterstatus\_controller.go](https://codecov.io/gh/clusternet/clusternet/pull/413/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet#diff-c3JjL2dpdGh1Yi5jb20vY2x1c3Rlcm5ldC9jbHVzdGVybmV0L3BrZy9jb250cm9sbGVycy9jbHVzdGVycy9jbHVzdGVyc3RhdHVzL2NsdXN0ZXJzdGF0dXNfY29udHJvbGxlci5nbw==) | `5.30% <0.00%> (+0.06%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/clusternet/clusternet/pull/413?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/clusternet/clusternet/pull/413?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet). Last update [34c49d1...2a41f72](https://codecov.io/gh/clusternet/clusternet/pull/413?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet).


Patch:
diff --git a/pkg/controllers/clusters/clusterstatus/clusterstatus_controller.go b/pkg/controllers/clusters/clusterstatus/clusterstatus_controller.go
index d41166b..b8d13a2 100644
--- a/pkg/controllers/clusters/clusterstatus/clusterstatus_controller.go
+++ b/pkg/controllers/clusters/clusterstatus/clusterstatus_controller.go
@@ -348,11 +348,8 @@ func getCommonNodeLabels(nodes []*corev1.Node) map[string]string {
 	if len(nodes) == 0 {
 		return nil
 	}
-	if len(nodes) == 1 {
-		return nodes[0].Labels
-	}
 	initLabels := nodes[0].Labels
-	for _, node := range nodes[1:] {
+	for _, node := range nodes {
 		currentLabels := map[string]string{}
 		for k, v := range node.Labels {
 			c, ok := initLabels[k]


Test patch:
diff --git a/pkg/controllers/clusters/clusterstatus/clusterstatus_controller_test.go b/pkg/controllers/clusters/clusterstatus/clusterstatus_controller_test.go
index 7dbe3cd..fe727bd 100644
--- a/pkg/controllers/clusters/clusterstatus/clusterstatus_controller_test.go
+++ b/pkg/controllers/clusters/clusterstatus/clusterstatus_controller_test.go
@@ -33,11 +33,13 @@ func (suite *StatusSuite) SetupTest() {
 		0: BuildNode("node0", map[string]string{
 			"node.clusternet.io/k1": "v1",
 			"node.clusternet.io/k2": "v2",
+			"others.keys.io/k2":     "a1",
 		}),
 		1: BuildNode("node1", map[string]string{
 			"node.clusternet.io/k1": "v1",
 			"node.clusternet.io/k2": "v2",
 			"node.clusternet.io/k3": "v3",
+			"others.keys.io/k2":     "a1",
 		}),
 		2: BuildNode("node2", map[string]string{
 			"node.clusternet.io/k1": "v1",
@@ -56,6 +58,19 @@ func (suite *StatusSuite) SetupTest() {
 func (suite *StatusSuite) TestGetCommonNodeLabels() {
 	commonLabels := getCommonNodeLabels(suite.nodes)
 	suite.Equal(
-		map[string]string{"node.clusternet.io/k1": "v1", "node.clusternet.io/k2": "v2"},
+		map[string]string{
+			"node.clusternet.io/k1": "v1",
+			"node.clusternet.io/k2": "v2",
+		},
+		commonLabels, "failed get common labels from nodes")
+}
+
+func (suite *StatusSuite) TestGetSingleNodeCommonNodeLabels() {
+	commonLabels := getCommonNodeLabels(suite.nodes[0:1])
+	suite.Equal(
+		map[string]string{
+			"node.clusternet.io/k1": "v1",
+			"node.clusternet.io/k2": "v2",
+		},
 		commonLabels, "failed get common labels from nodes")
 }