Skip to content

Commit 151221c

Browse files
committed
change update func
1 parent 35036ae commit 151221c

File tree

8 files changed

+58
-140
lines changed

8 files changed

+58
-140
lines changed

apis/cluster/v1beta1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apis/placement/v1alpha1/zz_generated.deepcopy.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apis/placement/v1beta1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apis/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/controllers/workgenerator/controller.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, req controllerruntime.Reques
151151
resourceBinding.RemoveCondition(string(i.ResourceBindingConditionType()))
152152
}
153153
resourceBinding.Status.FailedPlacements = nil
154+
resourceBinding.Status.DriftedPlacements = nil
155+
resourceBinding.Status.DiffedPlacements = nil
154156
if overrideSucceeded {
155157
overrideReason := condition.OverriddenSucceededReason
156158
overrideMessage := "Successfully applied the override rules on the resources"
@@ -1152,8 +1154,8 @@ func (r *Reconciler) SetupWithManager(mgr controllerruntime.Manager) error {
11521154
newAvailableCondition := meta.FindStatusCondition(newWork.Status.Conditions, fleetv1beta1.WorkConditionTypeAvailable)
11531155

11541156
// we try to filter out events, we only need to handle the updated event if the applied or available condition flip between true and false
1155-
// or the failed placements are changed.
1156-
if condition.EqualCondition(oldAppliedCondition, newAppliedCondition) && condition.EqualCondition(oldAvailableCondition, newAvailableCondition) {
1157+
// or the failed/diffed/drifted placements are changed.
1158+
if !changedDriftDetails(oldWork, newWork) && !changedDiffDetails(oldWork, newWork) && condition.EqualCondition(oldAppliedCondition, newAppliedCondition) && condition.EqualCondition(oldAvailableCondition, newAvailableCondition) {
11571159
if condition.IsConditionStatusFalse(newAppliedCondition, newWork.Generation) || condition.IsConditionStatusFalse(newAvailableCondition, newWork.Generation) {
11581160
// we need to compare the failed placement if the work is not applied or available
11591161
oldFailedPlacements := extractFailedResourcePlacementsFromWork(oldWork)
@@ -1182,21 +1184,6 @@ func (r *Reconciler) SetupWithManager(mgr controllerruntime.Manager) error {
11821184
}
11831185
}
11841186
}
1185-
// we need to compare the drift placement
1186-
oldDriftedPlacements := extractDriftedResourcePlacementsFromWork(oldWork)
1187-
newDriftedPlacements := extractDriftedResourcePlacementsFromWork(newWork)
1188-
if utils.IsDriftedResourcePlacementsEqual(oldDriftedPlacements, newDriftedPlacements) {
1189-
klog.V(2).InfoS("The drifted placement list didn't change, no need to reconcile", "oldWork", klog.KObj(oldWork), "newWork", klog.KObj(newWork))
1190-
return
1191-
}
1192-
1193-
// we need to compare the diffed placement
1194-
oldDiffedPlacements := extractDiffedResourcePlacementsFromWork(oldWork)
1195-
newDiffedPlacements := extractDiffedResourcePlacementsFromWork(newWork)
1196-
if utils.IsDiffedResourcePlacementsEqual(oldDiffedPlacements, newDiffedPlacements) {
1197-
klog.V(2).InfoS("The diffed placement list didn't change, no need to reconcile", "oldWork", klog.KObj(oldWork), "newWork", klog.KObj(newWork))
1198-
return
1199-
}
12001187
// We need to update the binding status in this case
12011188
klog.V(2).InfoS("Received a work update event that we need to handle", "work", klog.KObj(newWork), "parentBindingName", parentBindingName)
12021189
queue.Add(reconcile.Request{NamespacedName: types.NamespacedName{
@@ -1206,3 +1193,35 @@ func (r *Reconciler) SetupWithManager(mgr controllerruntime.Manager) error {
12061193
}).
12071194
Complete(r)
12081195
}
1196+
1197+
// changedDiffDetails checks if the drift details are changed between the old and new work
1198+
func changedDiffDetails(oldWork, newWork *fleetv1beta1.Work) bool {
1199+
for i, oldManifestCondition := range oldWork.Status.ManifestConditions {
1200+
if oldManifestCondition.DiffDetails == nil && newWork.Status.ManifestConditions[i].DiffDetails == nil {
1201+
continue
1202+
}
1203+
if oldManifestCondition.DiffDetails.ObservedInMemberClusterGeneration != newWork.Status.ManifestConditions[i].DiffDetails.ObservedInMemberClusterGeneration {
1204+
return true
1205+
} else if oldManifestCondition.DiffDetails.ObservationTime != newWork.Status.ManifestConditions[i].DiffDetails.ObservationTime {
1206+
return true
1207+
}
1208+
}
1209+
klog.V(2).InfoS("No change in the drift details", "oldWork", klog.KObj(oldWork), "newWork", klog.KObj(newWork))
1210+
return false
1211+
}
1212+
1213+
// changedDriftDetails checks if the drift details are changed between the old and new work
1214+
func changedDriftDetails(oldWork, newWork *fleetv1beta1.Work) bool {
1215+
for i, oldManifestCondition := range oldWork.Status.ManifestConditions {
1216+
if oldManifestCondition.DriftDetails == nil && newWork.Status.ManifestConditions[i].DriftDetails == nil {
1217+
continue
1218+
}
1219+
if oldManifestCondition.DriftDetails.ObservedInMemberClusterGeneration != newWork.Status.ManifestConditions[i].DriftDetails.ObservedInMemberClusterGeneration {
1220+
return true
1221+
} else if oldManifestCondition.DriftDetails.ObservationTime != newWork.Status.ManifestConditions[i].DriftDetails.ObservationTime {
1222+
return true
1223+
}
1224+
}
1225+
klog.V(2).InfoS("No change in the drift details", "oldWork", klog.KObj(oldWork), "newWork", klog.KObj(newWork))
1226+
return false
1227+
}

pkg/controllers/workgenerator/controller_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@ func TestSetBindingStatus(t *testing.T) {
12381238
FirstDriftedObservedTime: metav1.NewTime(timeNow.Add(-time.Hour)),
12391239
ObservedDrifts: []fleetv1beta1.PatchDetail{
12401240
{
1241-
Path: "/spec/ports/1/port",
1241+
Path: "/spec/ports/0/port",
12421242
ValueInHub: "80",
12431243
ValueInMember: "90",
12441244
},
@@ -1339,7 +1339,7 @@ func TestSetBindingStatus(t *testing.T) {
13391339
FirstDriftedObservedTime: metav1.NewTime(timeNow.Add(-time.Hour)),
13401340
ObservedDrifts: []fleetv1beta1.PatchDetail{
13411341
{
1342-
Path: "/spec/ports/1/port",
1342+
Path: "/spec/ports/0/port",
13431343
ValueInHub: "80",
13441344
ValueInMember: "90",
13451345
},
@@ -1442,7 +1442,7 @@ func TestSetBindingStatus(t *testing.T) {
14421442
FirstDiffedObservedTime: metav1.NewTime(timeNow.Add(-time.Second)),
14431443
ObservedDiffs: []fleetv1beta1.PatchDetail{
14441444
{
1445-
Path: "/spec/ports/1/port",
1445+
Path: "/spec/ports/0/port",
14461446
ValueInHub: "80",
14471447
ValueInMember: "90",
14481448
},
@@ -1472,12 +1472,12 @@ func TestSetBindingStatus(t *testing.T) {
14721472
Group: "",
14731473
Version: "v1",
14741474
Kind: "Service",
1475-
Name: "svc-name-1",
1475+
Name: "svc-name",
14761476
Namespace: "svc-namespace",
14771477
},
14781478
ObservationTime: metav1.NewTime(timeNow),
14791479
TargetClusterObservedGeneration: 2,
1480-
FirstDiffedObservedTime: metav1.NewTime(timeNow.Add(-time.Second)),
1480+
FirstDiffedObservedTime: metav1.NewTime(timeNow.Add(-time.Hour)),
14811481
ObservedDiffs: []fleetv1beta1.PatchDetail{
14821482
{
14831483
Path: "/spec/ports/1/port",
@@ -1694,9 +1694,9 @@ func TestSetBindingStatus(t *testing.T) {
16941694
binding := &fleetv1beta1.ClusterResourceBinding{}
16951695
setBindingStatus(tt.works, binding)
16961696
got := binding.Status.FailedPlacements
1697-
// setBindingStatus is using map to populate the failedResourcePlacement.
1697+
// setBindingStatus is using map to populate the placements.
16981698
// There is no default order in traversing the map.
1699-
// When the result of failedResourcePlacement exceeds the limit, the result will be truncated and cannot be
1699+
// When the result of Placements exceeds the limit, the result will be truncated and cannot be
17001700
// guaranteed.
17011701
if maxFailedResourcePlacementLimit == len(tt.wantFailedResourcePlacements) {
17021702
opt := cmp.Comparer(func(x, y fleetv1beta1.FailedResourcePlacement) bool {
@@ -1735,8 +1735,8 @@ func TestSetBindingStatus(t *testing.T) {
17351735
}
17361736
return n1.Namespace < n2.Namespace
17371737
}),
1738-
cmpopts.SortSlices(func(f1, f2 fleetv1beta1.DriftedResourcePlacement) bool {
1739-
return f1.ResourceIdentifier.Kind < f2.ResourceIdentifier.Kind
1738+
cmp.Comparer(func(f1, f2 fleetv1beta1.DriftedResourcePlacement) bool {
1739+
return f1.ResourceIdentifier.Kind == f2.ResourceIdentifier.Kind
17401740
}),
17411741
cmp.Comparer(func(t1, t2 metav1.Time) bool {
17421742
if t1.Time.IsZero() || t2.Time.IsZero() {
@@ -1757,13 +1757,13 @@ func TestSetBindingStatus(t *testing.T) {
17571757

17581758
resourceCmpOptions := []cmp.Option{
17591759
cmpopts.SortSlices(func(i, j fleetv1beta1.DriftedResourcePlacement) bool {
1760-
if i.ResourceIdentifier.Group < j.ResourceIdentifier.Group {
1760+
if i.Group < j.Group {
17611761
return true
17621762
}
1763-
if i.ResourceIdentifier.Kind < j.ResourceIdentifier.Kind {
1763+
if i.Kind < j.Kind {
17641764
return true
17651765
}
1766-
return i.ResourceIdentifier.Name < j.ResourceIdentifier.Name
1766+
return i.Name < j.Name
17671767
}),
17681768
}
17691769
if diff := cmp.Diff(gotDrifted, tt.wantDriftedResourcePlacements, resourceCmpOptions...); diff != "" {
@@ -1804,13 +1804,13 @@ func TestSetBindingStatus(t *testing.T) {
18041804

18051805
resourceCmpOptions = []cmp.Option{
18061806
cmpopts.SortSlices(func(i, j fleetv1beta1.DiffedResourcePlacement) bool {
1807-
if i.ResourceIdentifier.Group < j.ResourceIdentifier.Group {
1807+
if i.Group < j.Group {
18081808
return true
18091809
}
1810-
if i.ResourceIdentifier.Kind < j.ResourceIdentifier.Kind {
1810+
if i.Kind < j.Kind {
18111811
return true
18121812
}
1813-
return i.ResourceIdentifier.Name < j.ResourceIdentifier.Name
1813+
return i.Name < j.Name
18141814
}),
18151815
}
18161816
if diff := cmp.Diff(gotDiffed, tt.wantDiffedResourcePlacements, resourceCmpOptions...); diff != "" {

pkg/utils/common.go

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -555,108 +555,6 @@ func IsFailedResourcePlacementsEqual(oldFailedResourcePlacements, newFailedResou
555555
return true
556556
}
557557

558-
// LessFuncDriftedResourcePlacements is a less function for sorting drifted resource placements
559-
var LessFuncDriftedResourcePlacements = func(a, b placementv1beta1.DriftedResourcePlacement) bool {
560-
var aStr, bStr string
561-
if a.Envelope != nil {
562-
aStr = fmt.Sprintf(ResourceIdentifierWithEnvelopeIdentifierStringFormat, a.Group, a.Version, a.Kind, a.Namespace, a.Name, a.Envelope.Type, a.Envelope.Namespace, a.Envelope.Name)
563-
} else {
564-
aStr = fmt.Sprintf(ResourceIdentifierStringFormat, a.Group, a.Version, a.Kind, a.Namespace, a.Name)
565-
}
566-
if b.Envelope != nil {
567-
bStr = fmt.Sprintf(ResourceIdentifierWithEnvelopeIdentifierStringFormat, b.Group, b.Version, b.Kind, b.Namespace, b.Name, b.Envelope.Type, b.Envelope.Namespace, b.Envelope.Name)
568-
} else {
569-
bStr = fmt.Sprintf(ResourceIdentifierStringFormat, b.Group, b.Version, b.Kind, b.Namespace, b.Name)
570-
571-
}
572-
return aStr < bStr
573-
}
574-
575-
func IsDriftedResourcePlacementsEqual(oldDriftedResourcePlacements, newDriftedResourcePlacements []placementv1beta1.DriftedResourcePlacement) bool {
576-
if len(oldDriftedResourcePlacements) != len(newDriftedResourcePlacements) {
577-
return false
578-
}
579-
sort.Slice(oldDriftedResourcePlacements, func(i, j int) bool {
580-
return LessFuncDriftedResourcePlacements(oldDriftedResourcePlacements[i], oldDriftedResourcePlacements[j])
581-
})
582-
sort.Slice(newDriftedResourcePlacements, func(i, j int) bool {
583-
return LessFuncDriftedResourcePlacements(newDriftedResourcePlacements[i], newDriftedResourcePlacements[j])
584-
})
585-
for i := range oldDriftedResourcePlacements {
586-
oldDriftedResourcePlacement := oldDriftedResourcePlacements[i]
587-
newDriftedResourcePlacement := newDriftedResourcePlacements[i]
588-
if !equality.Semantic.DeepEqual(oldDriftedResourcePlacement.ResourceIdentifier, newDriftedResourcePlacement.ResourceIdentifier) {
589-
return false
590-
}
591-
if !equality.Semantic.DeepEqual(oldDriftedResourcePlacement.ObservationTime, newDriftedResourcePlacement.ObservationTime) {
592-
return false
593-
}
594-
if oldDriftedResourcePlacement.TargetClusterObservedGeneration != newDriftedResourcePlacement.TargetClusterObservedGeneration {
595-
return false
596-
}
597-
if !equality.Semantic.DeepEqual(oldDriftedResourcePlacement.FirstDriftedObservedTime, newDriftedResourcePlacement.FirstDriftedObservedTime) {
598-
return false
599-
}
600-
for j := range oldDriftedResourcePlacement.ObservedDrifts {
601-
if !equality.Semantic.DeepEqual(oldDriftedResourcePlacement.ObservedDrifts[j], newDriftedResourcePlacement.ObservedDrifts[j]) {
602-
return false
603-
}
604-
}
605-
}
606-
return true
607-
}
608-
609-
// LessFuncDiffedResourcePlacements is a less function for sorting diffed resource placements
610-
var LessFuncDiffedResourcePlacements = func(a, b placementv1beta1.DiffedResourcePlacement) bool {
611-
var aStr, bStr string
612-
if a.Envelope != nil {
613-
aStr = fmt.Sprintf(ResourceIdentifierWithEnvelopeIdentifierStringFormat, a.Group, a.Version, a.Kind, a.Namespace, a.Name, a.Envelope.Type, a.Envelope.Namespace, a.Envelope.Name)
614-
} else {
615-
aStr = fmt.Sprintf(ResourceIdentifierStringFormat, a.Group, a.Version, a.Kind, a.Namespace, a.Name)
616-
}
617-
if b.Envelope != nil {
618-
bStr = fmt.Sprintf(ResourceIdentifierWithEnvelopeIdentifierStringFormat, b.Group, b.Version, b.Kind, b.Namespace, b.Name, b.Envelope.Type, b.Envelope.Namespace, b.Envelope.Name)
619-
} else {
620-
bStr = fmt.Sprintf(ResourceIdentifierStringFormat, b.Group, b.Version, b.Kind, b.Namespace, b.Name)
621-
622-
}
623-
return aStr < bStr
624-
}
625-
626-
func IsDiffedResourcePlacementsEqual(oldDiffedResourcePlacements, newDiffedResourcePlacements []placementv1beta1.DiffedResourcePlacement) bool {
627-
if len(oldDiffedResourcePlacements) != len(newDiffedResourcePlacements) {
628-
return false
629-
}
630-
sort.Slice(oldDiffedResourcePlacements, func(i, j int) bool {
631-
return LessFuncDiffedResourcePlacements(oldDiffedResourcePlacements[i], oldDiffedResourcePlacements[j])
632-
})
633-
sort.Slice(newDiffedResourcePlacements, func(i, j int) bool {
634-
return LessFuncDiffedResourcePlacements(newDiffedResourcePlacements[i], newDiffedResourcePlacements[j])
635-
})
636-
for i := range oldDiffedResourcePlacements {
637-
oldDiffedResourcePlacement := oldDiffedResourcePlacements[i]
638-
newDiffedResourcePlacement := newDiffedResourcePlacements[i]
639-
if !equality.Semantic.DeepEqual(oldDiffedResourcePlacement.ResourceIdentifier, newDiffedResourcePlacement.ResourceIdentifier) {
640-
return false
641-
}
642-
if !equality.Semantic.DeepEqual(oldDiffedResourcePlacement.ObservationTime, newDiffedResourcePlacement.ObservationTime) {
643-
return false
644-
}
645-
if oldDiffedResourcePlacement.TargetClusterObservedGeneration != newDiffedResourcePlacement.TargetClusterObservedGeneration {
646-
return false
647-
}
648-
if !equality.Semantic.DeepEqual(oldDiffedResourcePlacement.FirstDiffedObservedTime, newDiffedResourcePlacement.FirstDiffedObservedTime) {
649-
return false
650-
}
651-
for j := range oldDiffedResourcePlacement.ObservedDiffs {
652-
if !equality.Semantic.DeepEqual(oldDiffedResourcePlacement.ObservedDiffs[j], newDiffedResourcePlacement.ObservedDiffs[j]) {
653-
return false
654-
}
655-
}
656-
}
657-
return true
658-
}
659-
660558
// IsFleetAnnotationPresent returns true if a key with fleet prefix is present in the annotations map.
661559
func IsFleetAnnotationPresent(annotations map[string]string) bool {
662560
for k := range annotations {

test/apis/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)