-
Notifications
You must be signed in to change notification settings - Fork 1k
Optimize binding controllers with fine-grained event filtering to reduce redundant reconciliations #6784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rohan-019
wants to merge
1
commit into
karmada-io:master
Choose a base branch
from
rohan-019:optimization_binding-controller
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Optimize binding controllers with fine-grained event filtering to reduce redundant reconciliations #6784
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
Copyright 2025 The Karmada Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package binding | ||
|
||
import ( | ||
"reflect" | ||
|
||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
|
||
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2" | ||
) | ||
|
||
// hasSignificantSpecFieldChanged checks if any significant spec fields have changed | ||
func hasSignificantSpecFieldChanged(oldObj, newObj interface{}) bool { | ||
var oldSpec, newSpec workv1alpha2.ResourceBindingSpec | ||
|
||
switch oldBinding := oldObj.(type) { | ||
case *workv1alpha2.ResourceBinding: | ||
oldSpec = oldBinding.Spec | ||
case *workv1alpha2.ClusterResourceBinding: | ||
oldSpec = oldBinding.Spec | ||
default: | ||
klog.V(4).InfoS("Unknown object type in predicate", "type", reflect.TypeOf(oldObj)) | ||
return false | ||
} | ||
|
||
switch newBinding := newObj.(type) { | ||
case *workv1alpha2.ResourceBinding: | ||
newSpec = newBinding.Spec | ||
case *workv1alpha2.ClusterResourceBinding: | ||
newSpec = newBinding.Spec | ||
default: | ||
klog.V(4).InfoS("Unknown object type in predicate", "type", reflect.TypeOf(newObj)) | ||
return false | ||
} | ||
|
||
// Check critical fields that require reconciliation | ||
return !reflect.DeepEqual(oldSpec.RequiredBy, newSpec.RequiredBy) || | ||
!reflect.DeepEqual(oldSpec.Clusters, newSpec.Clusters) || | ||
!reflect.DeepEqual(oldSpec.GracefulEvictionTasks, newSpec.GracefulEvictionTasks) || | ||
!reflect.DeepEqual(oldSpec.Resource, newSpec.Resource) || | ||
!reflect.DeepEqual(oldSpec.Suspension, newSpec.Suspension) || | ||
!reflect.DeepEqual(oldSpec.PreserveResourcesOnDeletion, newSpec.PreserveResourcesOnDeletion) || | ||
oldSpec.ConflictResolution != newSpec.ConflictResolution | ||
} | ||
|
||
// newBindingPredicate returns an inline predicate for ResourceBinding and ClusterResourceBinding | ||
// to optimize event filtering and reduce unnecessary reconciliations | ||
func newBindingPredicate() predicate.Predicate { | ||
return predicate.Funcs{ | ||
CreateFunc: func(event.CreateEvent) bool { | ||
return true | ||
}, | ||
UpdateFunc: func(e event.UpdateEvent) bool { | ||
// Trigger reconciliation when deletion starts to ensure timely finalizer/cleanup handling | ||
if e.ObjectOld.GetDeletionTimestamp() != e.ObjectNew.GetDeletionTimestamp() { | ||
return true | ||
} | ||
// Check if any critical spec fields have changed | ||
return hasSignificantSpecFieldChanged(e.ObjectOld, e.ObjectNew) | ||
}, | ||
DeleteFunc: func(event.DeleteEvent) bool { | ||
// Ignore delete events; reconciliation is driven by generation bump and finalizers. | ||
// Cleanup is handled by controller logic (e.g., work deletion). | ||
return false | ||
}, | ||
GenericFunc: func(event.GenericEvent) bool { | ||
// Process generic events (rarely used) | ||
return true | ||
}, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,288 @@ | ||
/* | ||
Copyright 2025 The Karmada Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package binding | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
|
||
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2" | ||
) | ||
|
||
func TestBindingPredicate_Create(t *testing.T) { | ||
predicate := newBindingPredicate() | ||
|
||
// Test ResourceBinding create event | ||
rb := &workv1alpha2.ResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-rb", | ||
Namespace: "default", | ||
}, | ||
} | ||
|
||
createEvent := event.CreateEvent{Object: rb} | ||
if !predicate.Create(createEvent) { | ||
t.Error("Expected Create event to return true") | ||
} | ||
|
||
// Test ClusterResourceBinding create event | ||
crb := &workv1alpha2.ClusterResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-crb", | ||
}, | ||
} | ||
|
||
createEventCRB := event.CreateEvent{Object: crb} | ||
if !predicate.Create(createEventCRB) { | ||
t.Error("Expected Create event for ClusterResourceBinding to return true") | ||
} | ||
} | ||
|
||
func TestBindingPredicate_Delete(t *testing.T) { | ||
rohan-019 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
predicate := newBindingPredicate() | ||
|
||
// Test ResourceBinding delete event | ||
rb := &workv1alpha2.ResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-rb", | ||
Namespace: "default", | ||
}, | ||
} | ||
|
||
deleteEvent := event.DeleteEvent{Object: rb} | ||
if predicate.Delete(deleteEvent) { | ||
t.Error("Expected Delete event to return false") | ||
} | ||
|
||
// Test ClusterResourceBinding delete event | ||
crb := &workv1alpha2.ClusterResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-crb", | ||
}, | ||
} | ||
|
||
deleteEventCRB := event.DeleteEvent{Object: crb} | ||
if predicate.Delete(deleteEventCRB) { | ||
t.Error("Expected Delete event for ClusterResourceBinding to return false") | ||
} | ||
} | ||
|
||
func TestBindingPredicate_Update(t *testing.T) { | ||
predicate := newBindingPredicate() | ||
|
||
tests := []struct { | ||
name string | ||
oldObj *workv1alpha2.ResourceBinding | ||
newObj *workv1alpha2.ResourceBinding | ||
expected bool | ||
}{ | ||
{ | ||
name: "deletion timestamp set", | ||
oldObj: &workv1alpha2.ResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-rb", | ||
Namespace: "default", | ||
}, | ||
}, | ||
newObj: &workv1alpha2.ResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-rb", | ||
Namespace: "default", | ||
DeletionTimestamp: &metav1.Time{Time: time.Now()}, | ||
}, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "clusters field change", | ||
oldObj: &workv1alpha2.ResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-rb", | ||
Namespace: "default", | ||
}, | ||
Spec: workv1alpha2.ResourceBindingSpec{ | ||
Clusters: []workv1alpha2.TargetCluster{ | ||
{Name: "cluster1"}, | ||
}, | ||
}, | ||
}, | ||
newObj: &workv1alpha2.ResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-rb", | ||
Namespace: "default", | ||
}, | ||
Spec: workv1alpha2.ResourceBindingSpec{ | ||
Clusters: []workv1alpha2.TargetCluster{ | ||
{Name: "cluster1"}, | ||
{Name: "cluster2"}, | ||
}, | ||
}, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "resource field change", | ||
oldObj: &workv1alpha2.ResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-rb", | ||
Namespace: "default", | ||
}, | ||
Spec: workv1alpha2.ResourceBindingSpec{ | ||
Resource: workv1alpha2.ObjectReference{ | ||
APIVersion: "apps/v1", | ||
Kind: "Deployment", | ||
Name: "test-deployment", | ||
Namespace: "default", | ||
}, | ||
}, | ||
}, | ||
newObj: &workv1alpha2.ResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-rb", | ||
Namespace: "default", | ||
}, | ||
Spec: workv1alpha2.ResourceBindingSpec{ | ||
Resource: workv1alpha2.ObjectReference{ | ||
APIVersion: "apps/v1", | ||
Kind: "Deployment", | ||
Name: "test-deployment-v2", | ||
Namespace: "default", | ||
}, | ||
}, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "conflict resolution change", | ||
oldObj: &workv1alpha2.ResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-rb", | ||
Namespace: "default", | ||
}, | ||
Spec: workv1alpha2.ResourceBindingSpec{ | ||
ConflictResolution: "Abort", | ||
}, | ||
}, | ||
newObj: &workv1alpha2.ResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-rb", | ||
Namespace: "default", | ||
}, | ||
Spec: workv1alpha2.ResourceBindingSpec{ | ||
ConflictResolution: "Overwrite", | ||
}, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "no significant change", | ||
oldObj: &workv1alpha2.ResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-rb", | ||
Namespace: "default", | ||
}, | ||
Spec: workv1alpha2.ResourceBindingSpec{ | ||
Resource: workv1alpha2.ObjectReference{ | ||
APIVersion: "apps/v1", | ||
Kind: "Deployment", | ||
Name: "test-deployment", | ||
Namespace: "default", | ||
}, | ||
}, | ||
}, | ||
newObj: &workv1alpha2.ResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-rb", | ||
Namespace: "default", | ||
Labels: map[string]string{"test": "label"}, | ||
}, | ||
Spec: workv1alpha2.ResourceBindingSpec{ | ||
Resource: workv1alpha2.ObjectReference{ | ||
APIVersion: "apps/v1", | ||
Kind: "Deployment", | ||
Name: "test-deployment", | ||
Namespace: "default", | ||
}, | ||
}, | ||
}, | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
updateEvent := event.UpdateEvent{ | ||
ObjectOld: tt.oldObj, | ||
ObjectNew: tt.newObj, | ||
} | ||
|
||
result := predicate.Update(updateEvent) | ||
if result != tt.expected { | ||
t.Errorf("Expected %v, got %v", tt.expected, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestResourceBindingPredicate_Generic(t *testing.T) { | ||
predicate := newBindingPredicate() | ||
|
||
genericEvent := event.GenericEvent{Object: &workv1alpha2.ResourceBinding{}} | ||
if !predicate.Generic(genericEvent) { | ||
t.Error("Expected Generic event to return true") | ||
} | ||
} | ||
|
||
func TestResourceBindingPredicate_ClusterResourceBinding(t *testing.T) { | ||
predicate := newBindingPredicate() | ||
|
||
// Test ClusterResourceBinding update with clusters change | ||
oldCRB := &workv1alpha2.ClusterResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-crb", | ||
}, | ||
Spec: workv1alpha2.ResourceBindingSpec{ | ||
Clusters: []workv1alpha2.TargetCluster{ | ||
{Name: "cluster1"}, | ||
}, | ||
}, | ||
} | ||
|
||
newCRB := &workv1alpha2.ClusterResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-crb", | ||
}, | ||
Spec: workv1alpha2.ResourceBindingSpec{ | ||
Clusters: []workv1alpha2.TargetCluster{ | ||
{Name: "cluster1"}, | ||
{Name: "cluster2"}, | ||
}, | ||
}, | ||
} | ||
|
||
updateEvent := event.UpdateEvent{ | ||
ObjectOld: oldCRB, | ||
ObjectNew: newCRB, | ||
} | ||
|
||
if !predicate.Update(updateEvent) { | ||
t.Error("Expected ClusterResourceBinding update with clusters change to return true") | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.