Skip to content

Commit 1d75266

Browse files
committed
../..
Signed-off-by: Kexin2000 <3299133282@qq.com>
1 parent 12a7281 commit 1d75266

File tree

2 files changed

+92
-59
lines changed

2 files changed

+92
-59
lines changed

pkg/dependenciesdistributor/dependencies_distributor.go

Lines changed: 17 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,6 @@ func (d *DependenciesDistributor) createOrUpdateAttachedBinding(attachedBinding
589589

590590
mergedRequiredBy := mergeBindingSnapshot(existBinding.Spec.RequiredBy, attachedBinding.Spec.RequiredBy)
591591

592-
conflictDetected := false
593-
var conflictReasons []string
594-
595592
// If the spec.Placement is nil, this means that existBinding is generated by the dependency mechanism.
596593
// If the spec.Placement is not nil, then it must be generated by PropagationPolicy.
597594
if existBinding.Spec.Placement == nil {
@@ -600,20 +597,14 @@ func (d *DependenciesDistributor) createOrUpdateAttachedBinding(attachedBinding
600597
klog.Errorf("Failed to check conflict for resourceBinding(%s): %v", bindingKey, err)
601598
return err
602599
}
603-
if crConflictDetected {
604-
conflictDetected = true
605-
conflictReasons = append(conflictReasons, "ConflictResolution mismatch (Overwrite vs Abort)")
606-
}
607600

608601
preserveConflictDetected, err := d.conflictDetectedForPreserveOnDeletion(mergedRequiredBy)
609602
if err != nil {
610603
klog.Errorf("Failed to check preserveOnDeletion conflict for resourceBinding(%s): %v", bindingKey, err)
611604
return err
612605
}
613-
if preserveConflictDetected {
614-
conflictDetected = true
615-
conflictReasons = append(conflictReasons, "PreserveResourcesOnDeletion mismatch (true vs false)")
616-
}
606+
607+
d.recordEventIfPolicyConflict(existBinding, crConflictDetected, preserveConflictDetected)
617608

618609
existBinding.Spec.ConflictResolution = attachedBinding.Spec.ConflictResolution
619610
}
@@ -623,13 +614,6 @@ func (d *DependenciesDistributor) createOrUpdateAttachedBinding(attachedBinding
623614
existBinding.Spec.Resource = attachedBinding.Spec.Resource
624615
existBinding.Spec.PreserveResourcesOnDeletion = attachedBinding.Spec.PreserveResourcesOnDeletion
625616

626-
if conflictDetected && d.EventRecorder != nil {
627-
// Emit a warning event indicating which policies conflicted.
628-
// Use placeholders to include the concrete conflicting dimensions in the message.
629-
message := "Dependency policy conflict detected: %s."
630-
d.EventRecorder.Eventf(existBinding, corev1.EventTypeWarning, events.EventReasonDependencyPolicyConflict, message, strings.Join(conflictReasons, "; "))
631-
}
632-
633617
if err := d.Client.Update(context.TODO(), existBinding); err != nil {
634618
klog.Errorf("Failed to update resourceBinding(%s): %v", bindingKey, err)
635619
return err
@@ -645,52 +629,26 @@ func (d *DependenciesDistributor) createOrUpdateAttachedBinding(attachedBinding
645629
return d.Client.Create(context.TODO(), attachedBinding)
646630
}
647631

648-
// conflictDetectedForConflictResolution checks if there is a conflict in ConflictResolution from referencing ResourceBindings.
649-
// Rules: any Overwrite -> Overwrite; else Abort. Returns whether a conflict was detected.
650-
func (d *DependenciesDistributor) conflictDetectedForConflictResolution(requiredBy []workv1alpha2.BindingSnapshot) (hasConflict bool, err error) {
651-
hasOverwrite := false
652-
hasAbort := false
653-
654-
for _, snap := range requiredBy {
655-
refRB := &workv1alpha2.ResourceBinding{}
656-
if err := d.Client.Get(context.TODO(), client.ObjectKey{Namespace: snap.Namespace, Name: snap.Name}, refRB); err != nil {
657-
return false, err
658-
}
632+
func (d *DependenciesDistributor) recordEventIfPolicyConflict(existBinding *workv1alpha2.ResourceBinding, crConflictDetected, preserveConflictDetected bool) {
633+
conflictDetected := false
634+
var conflictReasons []string
659635

660-
cr := effectiveConflictResolution(refRB.Spec.ConflictResolution)
661-
if cr == policyv1alpha1.ConflictOverwrite {
662-
hasOverwrite = true
663-
} else {
664-
hasAbort = true
665-
}
636+
if crConflictDetected {
637+
conflictDetected = true
638+
conflictReasons = append(conflictReasons, "ConflictResolution mismatch (Overwrite vs Abort)")
666639
}
667640

668-
hasConflict = hasOverwrite && hasAbort
669-
return hasConflict, nil
670-
}
671-
672-
// conflictDetectedForPreserveOnDeletion checks if there is a conflict in PreserveResourcesOnDeletion from referencing ResourceBindings.
673-
// Rules: any true -> true; else false. Returns whether a conflict was detected.
674-
func (d *DependenciesDistributor) conflictDetectedForPreserveOnDeletion(requiredBy []workv1alpha2.BindingSnapshot) (hasConflict bool, err error) {
675-
seenTrue := false
676-
seenFalse := false
677-
678-
for _, snap := range requiredBy {
679-
refRB := &workv1alpha2.ResourceBinding{}
680-
if err := d.Client.Get(context.TODO(), client.ObjectKey{Namespace: snap.Namespace, Name: snap.Name}, refRB); err != nil {
681-
return false, err
682-
}
683-
684-
pres := ptr.Deref(refRB.Spec.PreserveResourcesOnDeletion, false)
685-
if pres {
686-
seenTrue = true
687-
} else {
688-
seenFalse = true
689-
}
641+
if preserveConflictDetected {
642+
conflictDetected = true
643+
conflictReasons = append(conflictReasons, "PreserveResourcesOnDeletion mismatch (true vs false)")
690644
}
691645

692-
hasConflict = seenTrue && seenFalse
693-
return hasConflict, nil
646+
if conflictDetected {
647+
// Emit a warning event indicating which policies conflicted.
648+
// Use placeholders to include the concrete conflicting dimensions in the message.
649+
message := "Dependency policy conflict detected: %s."
650+
d.EventRecorder.Eventf(existBinding, corev1.EventTypeWarning, events.EventReasonDependencyPolicyConflict, message, strings.Join(conflictReasons, "; "))
651+
}
694652
}
695653

696654
// Start runs the distributor, never stop until context canceled.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright 2025 The Karmada Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package dependenciesdistributor
18+
19+
import (
20+
"context"
21+
22+
"k8s.io/utils/ptr"
23+
"sigs.k8s.io/controller-runtime/pkg/client"
24+
25+
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
26+
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
27+
)
28+
29+
// conflictDetectedForConflictResolution checks if there is a conflict in ConflictResolution from referencing ResourceBindings.
30+
func (d *DependenciesDistributor) conflictDetectedForConflictResolution(requiredBy []workv1alpha2.BindingSnapshot) (hasConflict bool, err error) {
31+
hasOverwrite := false
32+
hasAbort := false
33+
34+
for _, snap := range requiredBy {
35+
refRB := &workv1alpha2.ResourceBinding{}
36+
if err := d.Client.Get(context.TODO(), client.ObjectKey{Namespace: snap.Namespace, Name: snap.Name}, refRB); err != nil {
37+
return false, err
38+
}
39+
40+
cr := effectiveConflictResolution(refRB.Spec.ConflictResolution)
41+
if cr == policyv1alpha1.ConflictOverwrite {
42+
hasOverwrite = true
43+
} else {
44+
hasAbort = true
45+
}
46+
}
47+
48+
hasConflict = hasOverwrite && hasAbort
49+
return hasConflict, nil
50+
}
51+
52+
// conflictDetectedForPreserveOnDeletion checks if there is a conflict in PreserveResourcesOnDeletion from referencing ResourceBindings.
53+
func (d *DependenciesDistributor) conflictDetectedForPreserveOnDeletion(requiredBy []workv1alpha2.BindingSnapshot) (hasConflict bool, err error) {
54+
// seenTrue indicates at least one referencing ResourceBinding has PreserveResourcesOnDeletion set to true.
55+
// seenFalse indicates at least one referencing ResourceBinding has PreserveResourcesOnDeletion set to false.
56+
seenTrue := false
57+
seenFalse := false
58+
59+
for _, snap := range requiredBy {
60+
refRB := &workv1alpha2.ResourceBinding{}
61+
if err := d.Client.Get(context.TODO(), client.ObjectKey{Namespace: snap.Namespace, Name: snap.Name}, refRB); err != nil {
62+
return false, err
63+
}
64+
65+
pres := ptr.Deref(refRB.Spec.PreserveResourcesOnDeletion, false)
66+
if pres {
67+
seenTrue = true
68+
} else {
69+
seenFalse = true
70+
}
71+
}
72+
73+
hasConflict = seenTrue && seenFalse
74+
return hasConflict, nil
75+
}

0 commit comments

Comments
 (0)