Repository: clusternet/clusternet
Pull request #487 (2022-09-19 11:26:31)

fix merging TargetClusters
same describe with #486 and fix it


Garrybest: LGTM
codecov-commenter: # [Codecov](https://codecov.io/gh/clusternet/clusternet/pull/487?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet) Report
> Merging [#487](https://codecov.io/gh/clusternet/clusternet/pull/487?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet) (f5f3321) into [main](https://codecov.io/gh/clusternet/clusternet/commit/b3725c3440621e87b24b969ec0418737f901e4b5?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet) (b3725c3) will **increase** coverage by `0.03%`.
> The diff coverage is `n/a`.

```diff
@@            Coverage Diff             @@
##             main     #487      +/-   ##
==========================================
+ Coverage   13.44%   13.47%   +0.03%     
==========================================
  Files          62       62              
  Lines        6767     6773       +6     
==========================================
+ Hits          910      913       +3     
- Misses       5783     5785       +2     
- Partials       74       75       +1     
```


| [Impacted Files](https://codecov.io/gh/clusternet/clusternet/pull/487?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet) | Coverage Δ | |
|---|---|---|
| [...ernet/clusternet/pkg/hub/apiserver/shadow/utils.go](https://codecov.io/gh/clusternet/clusternet/pull/487/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet#diff-c3JjL2dpdGh1Yi5jb20vY2x1c3Rlcm5ldC9jbHVzdGVybmV0L3BrZy9odWIvYXBpc2VydmVyL3NoYWRvdy91dGlscy5nbw==) | `89.28% <0.00%> (-10.72%)` | :arrow_down: |
| [...ternet/pkg/scheduler/framework/interfaces/types.go](https://codecov.io/gh/clusternet/clusternet/pull/487/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet#diff-c3JjL2dpdGh1Yi5jb20vY2x1c3Rlcm5ldC9jbHVzdGVybmV0L3BrZy9zY2hlZHVsZXIvZnJhbWV3b3JrL2ludGVyZmFjZXMvdHlwZXMuZ28=) | `57.14% <0.00%> (+3.02%)` | :arrow_up: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clusternet)


Patch:
diff --git a/pkg/scheduler/framework/interfaces/types.go b/pkg/scheduler/framework/interfaces/types.go
index 5f13647..dfc6cf6 100644
--- a/pkg/scheduler/framework/interfaces/types.go
+++ b/pkg/scheduler/framework/interfaces/types.go
@@ -98,6 +98,10 @@ func (t *TargetClusters) DeepCopy() *TargetClusters {
 }
 
 func (t *TargetClusters) Merge(b *TargetClusters) {
+	// TODO: use matrix addition to merge
+	// 1. Align t and b using the union of clusters and feeds , no value use 0 fill
+	// 2. Addition t and b which filled
+	// 3. Remove all 0 rows and columns
 	if b == nil || len(b.BindingClusters) == 0 {
 		return
 	}
@@ -137,6 +141,9 @@ func (t *TargetClusters) Merge(b *TargetClusters) {
 		}
 		for feed, bReplicas := range b.Replicas {
 			if len(bReplicas) != 0 {
+				if len(t.Replicas[feed]) == 0 {
+					t.Replicas[feed] = append(t.Replicas[feed], 0)
+				}
 				t.Replicas[feed][ti] += bReplicas[bi]
 			}
 		}


Test patch:
diff --git a/pkg/scheduler/framework/interfaces/types_test.go b/pkg/scheduler/framework/interfaces/types_test.go
index d65f41f..60a88f8 100644
--- a/pkg/scheduler/framework/interfaces/types_test.go
+++ b/pkg/scheduler/framework/interfaces/types_test.go
@@ -5,6 +5,47 @@ import (
 	"testing"
 )
 
+func TestTargetClustersWithFirstScheduling_Merge(t *testing.T) {
+	source := &TargetClusters{
+		BindingClusters: []string{},
+		Replicas:        map[string][]int32{},
+	}
+
+	tests := []struct {
+		name   string
+		b      *TargetClusters
+		result *TargetClusters
+	}{
+		{
+			name: "f1 and f2 in c1 and c2",
+			b: &TargetClusters{
+				BindingClusters: []string{"c1", "c2"},
+				Replicas: map[string][]int32{
+					"f1": {1, 1},
+					"f2": {1, 2},
+				},
+			},
+			result: &TargetClusters{
+				BindingClusters: []string{"c1", "c2"},
+				Replicas: map[string][]int32{
+					"f1": {1, 1},
+					"f2": {1, 2},
+				},
+			},
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t1 *testing.T) {
+			tcp := source.DeepCopy()
+			tcp.Merge(tt.b)
+			if !reflect.DeepEqual(tcp, tt.result) {
+				t.Errorf("Merge() %s gotResponse = %v, want %v", tt.name, tcp, tt.result)
+			}
+		})
+	}
+}
+
 func TestTargetClusters_Merge(t *testing.T) {
 	template := &TargetClusters{
 		BindingClusters: []string{"c1", "c2", "c3"},
@@ -103,7 +144,7 @@ func TestTargetClusters_Merge(t *testing.T) {
 			tcp := template.DeepCopy()
 			tcp.Merge(tt.b)
 			if !reflect.DeepEqual(tcp, tt.result) {
-				t.Errorf("Merge() gotResponse = %v, want %v", tcp, tt.result)
+				t.Errorf("Merge() %s gotResponse = %v, want %v", tt.name, tcp, tt.result)
 			}
 		})
 	}