Skip to content

Commit ad02e82

Browse files
authored
rename placement decision tracker get function (#278)
Signed-off-by: haoqing0110 <[email protected]>
1 parent 1b45f76 commit ad02e82

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

cluster/v1beta1/helpers.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ func NewPlacementDecisionClustersTrackerWithGroups(placement *Placement, pdl Pla
5959
return pdct
6060
}
6161

62-
// Get updates the tracker's decisionClusters and returns added and deleted cluster names.
63-
func (pdct *PlacementDecisionClustersTracker) Get() (sets.Set[string], sets.Set[string], error) {
62+
// Refresh refreshes the tracker's decisionClusters.
63+
func (pdct *PlacementDecisionClustersTracker) Refresh() error {
6464
pdct.lock.Lock()
6565
defer pdct.lock.Unlock()
6666

6767
if pdct.placement == nil || pdct.placementDecisionGetter == nil {
68-
return nil, nil, nil
68+
return nil
6969
}
7070

7171
// Get the generated PlacementDecisions
@@ -74,37 +74,49 @@ func (pdct *PlacementDecisionClustersTracker) Get() (sets.Set[string], sets.Set[
7474
})
7575
decisions, err := pdct.placementDecisionGetter.List(decisionSelector, pdct.placement.Namespace)
7676
if err != nil {
77-
return nil, nil, fmt.Errorf("failed to list PlacementDecisions: %w", err)
77+
return fmt.Errorf("failed to list PlacementDecisions: %w", err)
7878
}
7979

8080
// Get the decision cluster names and groups
81-
newScheduledClusters := sets.New[string]()
8281
newScheduledClusterGroups := map[GroupKey]sets.Set[string]{}
8382
for _, d := range decisions {
8483
groupKey, err := parseGroupKeyFromDecision(d)
8584
if err != nil {
86-
return nil, nil, err
85+
return err
8786
}
8887

8988
if _, exist := newScheduledClusterGroups[groupKey]; !exist {
9089
newScheduledClusterGroups[groupKey] = sets.New[string]()
9190
}
9291

9392
for _, sd := range d.Status.Decisions {
94-
newScheduledClusters.Insert(sd.ClusterName)
9593
newScheduledClusterGroups[groupKey].Insert(sd.ClusterName)
9694
}
9795
}
9896

99-
// Compare the difference
100-
existingScheduledClusters := pdct.existingScheduledClusterGroups.GetClusters()
101-
added := newScheduledClusters.Difference(existingScheduledClusters)
102-
deleted := existingScheduledClusters.Difference(newScheduledClusters)
103-
10497
// Update the existing decision cluster groups
10598
pdct.existingScheduledClusterGroups = newScheduledClusterGroups
10699
pdct.generateGroupsNameIndex()
107100

101+
return nil
102+
}
103+
104+
// GetClusterChanges updates the tracker's decisionClusters and returns added and deleted cluster names.
105+
func (pdct *PlacementDecisionClustersTracker) GetClusterChanges() (sets.Set[string], sets.Set[string], error) {
106+
// Get existing clusters
107+
existingScheduledClusters := pdct.existingScheduledClusterGroups.GetClusters()
108+
109+
// Refresh clusters
110+
err := pdct.Refresh()
111+
if err != nil {
112+
return nil, nil, err
113+
}
114+
newScheduledClusters := pdct.existingScheduledClusterGroups.GetClusters()
115+
116+
// Compare the difference
117+
added := newScheduledClusters.Difference(existingScheduledClusters)
118+
deleted := existingScheduledClusters.Difference(newScheduledClusters)
119+
108120
return added, deleted, nil
109121
}
110122

cluster/v1beta1/helpers_test.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func newFakePlacementDecision(placementName, groupName string, groupIndex int, c
4343
}
4444
}
4545

46-
func TestPlacementDecisionClustersTracker_Get(t *testing.T) {
46+
func TestPlacementDecisionClustersTracker_GetClusterChanges(t *testing.T) {
4747
tests := []struct {
4848
name string
4949
placement Placement
@@ -81,6 +81,19 @@ func TestPlacementDecisionClustersTracker_Get(t *testing.T) {
8181
expectAddedScheduledClusters: sets.New[string]("cluster1", "cluster2"),
8282
expectDeletedScheduledClusters: sets.New[string](),
8383
},
84+
{
85+
name: "test nil exist cluster groups",
86+
placement: Placement{
87+
ObjectMeta: metav1.ObjectMeta{Name: "placement2", Namespace: "default"},
88+
Spec: PlacementSpec{},
89+
},
90+
existingScheduledClusterGroups: nil,
91+
updateDecisions: []*PlacementDecision{
92+
newFakePlacementDecision("placement2", "", 0, "cluster1", "cluster2"),
93+
},
94+
expectAddedScheduledClusters: sets.New[string]("cluster1", "cluster2"),
95+
expectDeletedScheduledClusters: sets.New[string](),
96+
},
8497
}
8598

8699
for _, test := range tests {
@@ -92,7 +105,7 @@ func TestPlacementDecisionClustersTracker_Get(t *testing.T) {
92105
tracker := NewPlacementDecisionClustersTrackerWithGroups(&test.placement, &fakeGetter, test.existingScheduledClusterGroups)
93106

94107
// check changed decision clusters
95-
addedClusters, deletedClusters, err := tracker.Get()
108+
addedClusters, deletedClusters, err := tracker.GetClusterChanges()
96109
if err != nil {
97110
t.Errorf("Case: %v, Failed to run Get(): %v", test.name, err)
98111
}
@@ -173,9 +186,9 @@ func TestPlacementDecisionClustersTracker_Existing(t *testing.T) {
173186
}
174187
// init tracker
175188
tracker := NewPlacementDecisionClustersTrackerWithGroups(&test.placement, &fakeGetter, nil)
176-
_, _, err := tracker.Get()
189+
err := tracker.Refresh()
177190
if err != nil {
178-
t.Errorf("Case: %v, Failed to run Get(): %v", test.name, err)
191+
t.Errorf("Case: %v, Failed to run Refresh(): %v", test.name, err)
179192
}
180193

181194
// Call the Existing method with different groupKeys inputs.
@@ -295,9 +308,9 @@ func TestPlacementDecisionClustersTracker_ExistingClusterGroups(t *testing.T) {
295308
}
296309
// init tracker
297310
tracker := NewPlacementDecisionClustersTracker(&test.placement, &fakeGetter, nil)
298-
_, _, err := tracker.Get()
311+
err := tracker.Refresh()
299312
if err != nil {
300-
t.Errorf("Case: %v, Failed to run Get(): %v", test.name, err)
313+
t.Errorf("Case: %v, Failed to run Refresh(): %v", test.name, err)
301314
}
302315

303316
// Call the Existing method with different groupKeys inputs.

0 commit comments

Comments
 (0)