From ad4e7a4dc74c1f60914c54d6c1de204b2bf021a2 Mon Sep 17 00:00:00 2001 From: Britania Rodriguez Reyes Date: Mon, 16 Dec 2024 10:15:30 -0600 Subject: [PATCH] sort drift/diff placement and fix UT --- pkg/controllers/workgenerator/controller.go | 8 ++++ .../workgenerator/controller_test.go | 42 ++++++++++++------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/pkg/controllers/workgenerator/controller.go b/pkg/controllers/workgenerator/controller.go index a7c8a0cc5..81e2def8e 100644 --- a/pkg/controllers/workgenerator/controller.go +++ b/pkg/controllers/workgenerator/controller.go @@ -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 { @@ -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 { diff --git a/pkg/controllers/workgenerator/controller_test.go b/pkg/controllers/workgenerator/controller_test.go index 8c1795ee2..1fd87c7e2 100644 --- a/pkg/controllers/workgenerator/controller_test.go +++ b/pkg/controllers/workgenerator/controller_test.go @@ -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", }, }, }, @@ -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", @@ -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", }, }, },