Skip to content

Commit

Permalink
sort drift/diff placement and fix UT
Browse files Browse the repository at this point in the history
  • Loading branch information
britaniar committed Dec 16, 2024
1 parent 3bb2656 commit ad4e7a4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
8 changes: 8 additions & 0 deletions pkg/controllers/workgenerator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,10 @@ func setBindingStatus(works map[string]*fleetv1beta1.Work, resourceBinding *flee

// cut the list to keep only the max limit
if len(diffedResourcePlacements) > maxDiffedResourcePlacementLimit {
// Sort the slice
sort.Slice(diffedResourcePlacements, func(i, j int) bool {
return utils.LessFuncDiffedResourcePlacements(diffedResourcePlacements[i], diffedResourcePlacements[j])
})
diffedResourcePlacements = diffedResourcePlacements[0:maxDiffedResourcePlacementLimit]
}
if len(diffedResourcePlacements) > 0 {
Expand All @@ -814,6 +818,10 @@ func setBindingStatus(works map[string]*fleetv1beta1.Work, resourceBinding *flee

// cut the list to keep only the max limit
if len(driftedResourcePlacements) > maxDriftedResourcePlacementLimit {
// Sort the slice
sort.Slice(driftedResourcePlacements, func(i, j int) bool {
return utils.LessFuncDriftedResourcePlacements(driftedResourcePlacements[i], driftedResourcePlacements[j])
})
driftedResourcePlacements = driftedResourcePlacements[0:maxDriftedResourcePlacementLimit]
}
if len(driftedResourcePlacements) > 0 {
Expand Down
42 changes: 27 additions & 15 deletions pkg/controllers/workgenerator/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1323,18 +1323,18 @@ func TestSetBindingStatus(t *testing.T) {
ResourceIdentifier: fleetv1beta1.ResourceIdentifier{
Group: "",
Version: "v1",
Kind: "Service",
Name: "svc-name",
Namespace: "svc-namespace",
Kind: "ConfigMap",
Name: "config-name-1",
Namespace: "config-namespace",
},
ObservationTime: metav1.NewTime(timeNow),
TargetClusterObservedGeneration: 2,
FirstDriftedObservedTime: metav1.NewTime(timeNow.Add(-time.Hour)),
FirstDriftedObservedTime: metav1.NewTime(timeNow.Add(-time.Second)),
ObservedDrifts: []fleetv1beta1.PatchDetail{
{
Path: "/spec/ports/0/port",
ValueInHub: "80",
ValueInMember: "90",
Path: "/metadata/labels/label1",
ValueInHub: "key1",
ValueInMember: "key2",
},
},
},
Expand Down Expand Up @@ -1437,13 +1437,25 @@ func TestSetBindingStatus(t *testing.T) {
Conditions: []metav1.Condition{
{
Type: fleetv1beta1.WorkConditionTypeApplied,
Status: metav1.ConditionTrue,
Status: metav1.ConditionFalse,
},
},
},
},
},
wantFailedResourcePlacements: []fleetv1beta1.FailedResourcePlacement{
{
ResourceIdentifier: fleetv1beta1.ResourceIdentifier{
Version: "v1",
Kind: "ConfigMap",
Name: "config-name-1",
Namespace: "config-namespace",
},
Condition: metav1.Condition{
Type: fleetv1beta1.WorkConditionTypeApplied,
Status: metav1.ConditionFalse,
},
},
{
ResourceIdentifier: fleetv1beta1.ResourceIdentifier{
Version: "v1",
Expand All @@ -1463,18 +1475,18 @@ func TestSetBindingStatus(t *testing.T) {
ResourceIdentifier: fleetv1beta1.ResourceIdentifier{
Group: "",
Version: "v1",
Kind: "Service",
Name: "svc-name",
Namespace: "svc-namespace",
Kind: "ConfigMap",
Name: "config-name-1",
Namespace: "config-namespace",
},
ObservationTime: metav1.NewTime(timeNow),
TargetClusterObservedGeneration: ptr.To(int64(2)),
FirstDiffedObservedTime: metav1.NewTime(timeNow.Add(-time.Hour)),
FirstDiffedObservedTime: metav1.NewTime(timeNow.Add(-time.Second)),
ObservedDiffs: []fleetv1beta1.PatchDetail{
{
Path: "/spec/ports/1/port",
ValueInHub: "80",
ValueInMember: "90",
Path: "/metadata/labels/label1",
ValueInHub: "key1",
ValueInMember: "key2",
},
},
},
Expand Down

0 comments on commit ad4e7a4

Please sign in to comment.