Skip to content

Commit 2974524

Browse files
k8s-infra-cherrypick-robotfabriziopandinichrischdisbueringer
authored
[release-1.9] 🌱 add TypedAll, ResourceIsUnchanged and TypedResourceIsUnchanged predicates (#11603)
* add TypedAll, ResourceIsUnchanged and TypedResourceIsUnchanged predicates * Add ResourceIsUnchanged predicate to secondary objects/Watches * also use the predicate for external or clustercache based watches * Fix predicate name, add logging and add it to external and owns watches * Fix godoc * use correct logger * add scheme and gvk pair to predicate logger * fix predicate logger name * fix * fixes * revert not necessary r.predicateLog * Fix k/v pair leak Signed-off-by: Stefan Büringer [email protected] --------- Signed-off-by: Stefan Büringer [email protected] Co-authored-by: fabriziopandini <[email protected]> Co-authored-by: Christian Schlotter <[email protected]> Co-authored-by: Stefan Bueringer <[email protected]>
1 parent 6c909a0 commit 2974524

File tree

24 files changed

+164
-54
lines changed

24 files changed

+164
-54
lines changed

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go

+3
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,14 @@ func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl
122122
Watches(
123123
&clusterv1.Machine{},
124124
handler.EnqueueRequestsFromMapFunc(r.MachineToBootstrapMapFunc),
125+
builder.WithPredicates(predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog)),
125126
).WithEventFilter(predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue))
126127

127128
if feature.Gates.Enabled(feature.MachinePool) {
128129
b = b.Watches(
129130
&expv1.MachinePool{},
130131
handler.EnqueueRequestsFromMapFunc(r.MachinePoolToBootstrapMapFunc),
132+
builder.WithPredicates(predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog)),
131133
)
132134
}
133135

@@ -136,6 +138,7 @@ func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl
136138
handler.EnqueueRequestsFromMapFunc(r.ClusterToKubeadmConfigs),
137139
builder.WithPredicates(
138140
predicates.All(mgr.GetScheme(), predicateLog,
141+
predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog),
139142
predicates.ClusterPausedTransitionsOrInfrastructureReady(mgr.GetScheme(), predicateLog),
140143
predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue),
141144
),

controlplane/kubeadm/internal/controllers/controller.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,15 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
115115
predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "kubeadmcontrolplane")
116116
c, err := ctrl.NewControllerManagedBy(mgr).
117117
For(&controlplanev1.KubeadmControlPlane{}).
118-
Owns(&clusterv1.Machine{}).
118+
Owns(&clusterv1.Machine{}, builder.WithPredicates(predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog))).
119119
WithOptions(options).
120120
WithEventFilter(predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue)).
121121
Watches(
122122
&clusterv1.Cluster{},
123123
handler.EnqueueRequestsFromMapFunc(r.ClusterToKubeadmControlPlane),
124124
builder.WithPredicates(
125125
predicates.All(mgr.GetScheme(), predicateLog,
126+
predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog),
126127
predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue),
127128
predicates.ClusterPausedTransitionsOrInfrastructureReady(mgr.GetScheme(), predicateLog),
128129
),

exp/addons/internal/controllers/clusterresourceset_controller.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,20 @@ func (r *ClusterResourceSetReconciler) SetupWithManager(ctx context.Context, mgr
8181
Watches(
8282
&clusterv1.Cluster{},
8383
handler.EnqueueRequestsFromMapFunc(r.clusterToClusterResourceSet),
84+
builder.WithPredicates(predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog)),
8485
).
8586
WatchesRawSource(r.ClusterCache.GetClusterSource("clusterresourceset", r.clusterToClusterResourceSet)).
8687
WatchesMetadata(
8788
&corev1.ConfigMap{},
8889
handler.EnqueueRequestsFromMapFunc(
8990
resourceToClusterResourceSetFunc[client.Object](r.Client),
9091
),
91-
builder.WithPredicates(resourcepredicates.TypedResourceCreateOrUpdate[client.Object](predicateLog)),
92+
builder.WithPredicates(
93+
predicates.All(mgr.GetScheme(), predicateLog,
94+
predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog),
95+
resourcepredicates.TypedResourceCreateOrUpdate[client.Object](predicateLog),
96+
),
97+
),
9298
).
9399
WatchesRawSource(source.Kind(
94100
partialSecretCache,
@@ -101,7 +107,10 @@ func (r *ClusterResourceSetReconciler) SetupWithManager(ctx context.Context, mgr
101107
handler.TypedEnqueueRequestsFromMapFunc(
102108
resourceToClusterResourceSetFunc[*metav1.PartialObjectMetadata](r.Client),
103109
),
104-
resourcepredicates.TypedResourceCreateOrUpdate[*metav1.PartialObjectMetadata](predicateLog),
110+
predicates.TypedAll(mgr.GetScheme(), predicateLog,
111+
predicates.TypedResourceIsChanged[*metav1.PartialObjectMetadata](mgr.GetScheme(), predicateLog),
112+
resourcepredicates.TypedResourceCreateOrUpdate[*metav1.PartialObjectMetadata](predicateLog),
113+
),
105114
)).
106115
WithOptions(options).
107116
WithEventFilter(predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue)).

exp/addons/internal/controllers/clusterresourcesetbinding_controller.go

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
apierrors "k8s.io/apimachinery/pkg/api/errors"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525
ctrl "sigs.k8s.io/controller-runtime"
26+
"sigs.k8s.io/controller-runtime/pkg/builder"
2627
"sigs.k8s.io/controller-runtime/pkg/client"
2728
"sigs.k8s.io/controller-runtime/pkg/controller"
2829
"sigs.k8s.io/controller-runtime/pkg/handler"
@@ -58,6 +59,7 @@ func (r *ClusterResourceSetBindingReconciler) SetupWithManager(ctx context.Conte
5859
Watches(
5960
&clusterv1.Cluster{},
6061
handler.EnqueueRequestsFromMapFunc(r.clusterToClusterResourceSetBinding),
62+
builder.WithPredicates(predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog)),
6163
).
6264
WithOptions(options).
6365
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue)).

exp/internal/controllers/machinepool_controller.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"time"
2323

24+
"github.com/go-logr/logr"
2425
"github.com/pkg/errors"
2526
corev1 "k8s.io/api/core/v1"
2627
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -29,12 +30,14 @@ import (
2930
kerrors "k8s.io/apimachinery/pkg/util/errors"
3031
"k8s.io/client-go/tools/record"
3132
"k8s.io/klog/v2"
33+
"k8s.io/utils/ptr"
3234
ctrl "sigs.k8s.io/controller-runtime"
3335
"sigs.k8s.io/controller-runtime/pkg/builder"
3436
"sigs.k8s.io/controller-runtime/pkg/client"
3537
"sigs.k8s.io/controller-runtime/pkg/controller"
3638
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3739
"sigs.k8s.io/controller-runtime/pkg/handler"
40+
"sigs.k8s.io/controller-runtime/pkg/predicate"
3841
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3942

4043
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -82,6 +85,8 @@ type MachinePoolReconciler struct {
8285
ssaCache ssa.Cache
8386
recorder record.EventRecorder
8487
externalTracker external.ObjectTracker
88+
89+
predicateLog *logr.Logger
8590
}
8691

8792
// scope holds the different objects that are read and used during the reconcile.
@@ -104,7 +109,7 @@ func (r *MachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.M
104109
return errors.New("Client, APIReader and ClusterCache must not be nil")
105110
}
106111

107-
predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "machinepool")
112+
r.predicateLog = ptr.To(ctrl.LoggerFrom(ctx).WithValues("controller", "machinepool"))
108113
clusterToMachinePools, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &expv1.MachinePoolList{}, mgr.GetScheme())
109114
if err != nil {
110115
return err
@@ -113,15 +118,16 @@ func (r *MachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.M
113118
c, err := ctrl.NewControllerManagedBy(mgr).
114119
For(&expv1.MachinePool{}).
115120
WithOptions(options).
116-
WithEventFilter(predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue)).
121+
WithEventFilter(predicates.ResourceHasFilterLabel(mgr.GetScheme(), *r.predicateLog, r.WatchFilterValue)).
117122
Watches(
118123
&clusterv1.Cluster{},
119124
handler.EnqueueRequestsFromMapFunc(clusterToMachinePools),
120125
// TODO: should this wait for Cluster.Status.InfrastructureReady similar to Infra Machine resources?
121126
builder.WithPredicates(
122-
predicates.All(mgr.GetScheme(), predicateLog,
123-
predicates.ClusterPausedTransitions(mgr.GetScheme(), predicateLog),
124-
predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue),
127+
predicates.All(mgr.GetScheme(), *r.predicateLog,
128+
predicates.ResourceIsChanged(mgr.GetScheme(), *r.predicateLog),
129+
predicates.ClusterPausedTransitions(mgr.GetScheme(), *r.predicateLog),
130+
predicates.ResourceHasFilterLabel(mgr.GetScheme(), *r.predicateLog, r.WatchFilterValue),
125131
),
126132
),
127133
).
@@ -137,7 +143,7 @@ func (r *MachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.M
137143
Controller: c,
138144
Cache: mgr.GetCache(),
139145
Scheme: mgr.GetScheme(),
140-
PredicateLogger: &predicateLog,
146+
PredicateLogger: r.predicateLog,
141147
}
142148
r.ssaCache = ssa.NewCache()
143149

@@ -377,6 +383,7 @@ func (r *MachinePoolReconciler) watchClusterNodes(ctx context.Context, cluster *
377383
Watcher: r.controller,
378384
Kind: &corev1.Node{},
379385
EventHandler: handler.EnqueueRequestsFromMapFunc(r.nodeToMachinePool),
386+
Predicates: []predicate.TypedPredicate[client.Object]{predicates.TypedResourceIsChanged[client.Object](r.Client.Scheme(), *r.predicateLog)},
380387
}))
381388
}
382389

exp/internal/controllers/machinepool_controller_phases.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import (
4949
"sigs.k8s.io/cluster-api/util/labels"
5050
"sigs.k8s.io/cluster-api/util/labels/format"
5151
"sigs.k8s.io/cluster-api/util/patch"
52+
"sigs.k8s.io/cluster-api/util/predicates"
5253
)
5354

5455
func (r *MachinePoolReconciler) reconcilePhase(mp *expv1.MachinePool) {
@@ -123,7 +124,7 @@ func (r *MachinePoolReconciler) reconcileExternal(ctx context.Context, m *expv1.
123124
}
124125

125126
// Ensure we add a watch to the external object, if there isn't one already.
126-
if err := r.externalTracker.Watch(log, obj, handler.EnqueueRequestForOwner(r.Client.Scheme(), r.Client.RESTMapper(), &expv1.MachinePool{})); err != nil {
127+
if err := r.externalTracker.Watch(log, obj, handler.EnqueueRequestForOwner(r.Client.Scheme(), r.Client.RESTMapper(), &expv1.MachinePool{}), predicates.ResourceIsChanged(r.Client.Scheme(), *r.externalTracker.PredicateLogger)); err != nil {
127128
return external.ReconcileOutput{}, err
128129
}
129130

@@ -364,7 +365,7 @@ func (r *MachinePoolReconciler) reconcileMachines(ctx context.Context, s *scope,
364365
sampleInfraMachine.SetKind(infraMachineKind)
365366

366367
// Add watcher for infraMachine, if there isn't one already.
367-
if err := r.externalTracker.Watch(log, sampleInfraMachine, handler.EnqueueRequestsFromMapFunc(r.infraMachineToMachinePoolMapper)); err != nil {
368+
if err := r.externalTracker.Watch(log, sampleInfraMachine, handler.EnqueueRequestsFromMapFunc(r.infraMachineToMachinePoolMapper), predicates.ResourceIsChanged(r.Client.Scheme(), *r.externalTracker.PredicateLogger)); err != nil {
368369
return err
369370
}
370371

exp/runtime/internal/controllers/extensionconfig_controller.go

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
8181
handler.TypedEnqueueRequestsFromMapFunc(
8282
r.secretToExtensionConfig,
8383
),
84+
predicates.TypedResourceIsChanged[*metav1.PartialObjectMetadata](mgr.GetScheme(), predicateLog),
8485
)).
8586
WithOptions(options).
8687
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue)).

internal/controllers/cluster/cluster_controller.go

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"k8s.io/client-go/tools/record"
3636
"k8s.io/klog/v2"
3737
ctrl "sigs.k8s.io/controller-runtime"
38+
"sigs.k8s.io/controller-runtime/pkg/builder"
3839
"sigs.k8s.io/controller-runtime/pkg/client"
3940
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
4041
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -103,14 +104,17 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
103104
Watches(
104105
&clusterv1.Machine{},
105106
handler.EnqueueRequestsFromMapFunc(r.controlPlaneMachineToCluster),
107+
builder.WithPredicates(predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog)),
106108
).
107109
Watches(
108110
&clusterv1.MachineDeployment{},
109111
handler.EnqueueRequestsFromMapFunc(r.machineDeploymentToCluster),
112+
builder.WithPredicates(predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog)),
110113
).
111114
Watches(
112115
&expv1.MachinePool{},
113116
handler.EnqueueRequestsFromMapFunc(r.machinePoolToCluster),
117+
builder.WithPredicates(predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog)),
114118
).
115119
WithOptions(options).
116120
WithEventFilter(predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue)).

internal/controllers/cluster/cluster_controller_phases.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
4040
"sigs.k8s.io/cluster-api/util/kubeconfig"
4141
"sigs.k8s.io/cluster-api/util/patch"
42+
"sigs.k8s.io/cluster-api/util/predicates"
4243
"sigs.k8s.io/cluster-api/util/secret"
4344
)
4445

@@ -96,7 +97,7 @@ func (r *Reconciler) reconcileExternal(ctx context.Context, cluster *clusterv1.C
9697
}
9798

9899
// Ensure we add a watcher to the external object.
99-
if err := r.externalTracker.Watch(log, obj, handler.EnqueueRequestForOwner(r.Client.Scheme(), r.Client.RESTMapper(), &clusterv1.Cluster{})); err != nil {
100+
if err := r.externalTracker.Watch(log, obj, handler.EnqueueRequestForOwner(r.Client.Scheme(), r.Client.RESTMapper(), &clusterv1.Cluster{}), predicates.ResourceIsChanged(r.Client.Scheme(), *r.externalTracker.PredicateLogger)); err != nil {
100101
return nil, err
101102
}
102103

internal/controllers/clusterclass/clusterclass_controller.go

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"k8s.io/apimachinery/pkg/util/validation/field"
3535
"k8s.io/klog/v2"
3636
ctrl "sigs.k8s.io/controller-runtime"
37+
"sigs.k8s.io/controller-runtime/pkg/builder"
3738
"sigs.k8s.io/controller-runtime/pkg/client"
3839
"sigs.k8s.io/controller-runtime/pkg/controller"
3940
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -90,6 +91,7 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
9091
Watches(
9192
&runtimev1.ExtensionConfig{},
9293
handler.EnqueueRequestsFromMapFunc(r.extensionConfigToClusterClass),
94+
builder.WithPredicates(predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog)),
9395
).
9496
WithEventFilter(predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue)).
9597
Complete(r)

internal/controllers/machine/machine_controller.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strings"
2424
"time"
2525

26+
"github.com/go-logr/logr"
2627
"github.com/pkg/errors"
2728
corev1 "k8s.io/api/core/v1"
2829
storagev1 "k8s.io/api/storage/v1"
@@ -42,6 +43,7 @@ import (
4243
"sigs.k8s.io/controller-runtime/pkg/controller"
4344
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
4445
"sigs.k8s.io/controller-runtime/pkg/handler"
46+
"sigs.k8s.io/controller-runtime/pkg/predicate"
4547
"sigs.k8s.io/controller-runtime/pkg/reconcile"
4648

4749
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -111,6 +113,8 @@ type Reconciler struct {
111113
// specific time for a specific Request. This is used to implement rate-limiting to avoid
112114
// e.g. spamming workload clusters with eviction requests during Node drain.
113115
reconcileDeleteCache cache.Cache[cache.ReconcileEntry]
116+
117+
predicateLog *logr.Logger
114118
}
115119

116120
func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
@@ -122,7 +126,7 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
122126
return errors.New("Client, APIReader and ClusterCache must not be nil and RemoteConditionsGracePeriod must not be < 2m")
123127
}
124128

125-
predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "machine")
129+
r.predicateLog = ptr.To(ctrl.LoggerFrom(ctx).WithValues("controller", "machine"))
126130
clusterToMachines, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &clusterv1.MachineList{}, mgr.GetScheme())
127131
if err != nil {
128132
return err
@@ -139,29 +143,31 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
139143
if r.nodeDeletionRetryTimeout.Nanoseconds() == 0 {
140144
r.nodeDeletionRetryTimeout = 10 * time.Second
141145
}
142-
143146
c, err := ctrl.NewControllerManagedBy(mgr).
144147
For(&clusterv1.Machine{}).
145148
WithOptions(options).
146-
WithEventFilter(predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue)).
149+
WithEventFilter(predicates.ResourceHasFilterLabel(mgr.GetScheme(), *r.predicateLog, r.WatchFilterValue)).
147150
Watches(
148151
&clusterv1.Cluster{},
149152
handler.EnqueueRequestsFromMapFunc(clusterToMachines),
150153
builder.WithPredicates(
151154
// TODO: should this wait for Cluster.Status.InfrastructureReady similar to Infra Machine resources?
152-
predicates.All(mgr.GetScheme(), predicateLog,
153-
predicates.ClusterControlPlaneInitialized(mgr.GetScheme(), predicateLog),
154-
predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue),
155+
predicates.All(mgr.GetScheme(), *r.predicateLog,
156+
predicates.ResourceIsChanged(mgr.GetScheme(), *r.predicateLog),
157+
predicates.ClusterControlPlaneInitialized(mgr.GetScheme(), *r.predicateLog),
158+
predicates.ResourceHasFilterLabel(mgr.GetScheme(), *r.predicateLog, r.WatchFilterValue),
155159
),
156160
)).
157161
WatchesRawSource(r.ClusterCache.GetClusterSource("machine", clusterToMachines, clustercache.WatchForProbeFailure(r.RemoteConditionsGracePeriod))).
158162
Watches(
159163
&clusterv1.MachineSet{},
160164
handler.EnqueueRequestsFromMapFunc(msToMachines),
165+
builder.WithPredicates(predicates.ResourceIsChanged(mgr.GetScheme(), *r.predicateLog)),
161166
).
162167
Watches(
163168
&clusterv1.MachineDeployment{},
164169
handler.EnqueueRequestsFromMapFunc(mdToMachines),
170+
builder.WithPredicates(predicates.ResourceIsChanged(mgr.GetScheme(), *r.predicateLog)),
165171
).
166172
Build(r)
167173
if err != nil {
@@ -174,7 +180,7 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
174180
Controller: c,
175181
Cache: mgr.GetCache(),
176182
Scheme: mgr.GetScheme(),
177-
PredicateLogger: &predicateLog,
183+
PredicateLogger: r.predicateLog,
178184
}
179185
r.ssaCache = ssa.NewCache()
180186
r.reconcileDeleteCache = cache.New[cache.ReconcileEntry]()
@@ -1081,6 +1087,7 @@ func (r *Reconciler) watchClusterNodes(ctx context.Context, cluster *clusterv1.C
10811087
Watcher: r.controller,
10821088
Kind: &corev1.Node{},
10831089
EventHandler: handler.EnqueueRequestsFromMapFunc(r.nodeToMachine),
1090+
Predicates: []predicate.TypedPredicate[client.Object]{predicates.TypedResourceIsChanged[client.Object](r.Client.Scheme(), *r.predicateLog)},
10841091
}))
10851092
}
10861093

internal/controllers/machine/machine_controller_phases.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"sigs.k8s.io/cluster-api/util/conditions"
4141
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
4242
"sigs.k8s.io/cluster-api/util/patch"
43+
"sigs.k8s.io/cluster-api/util/predicates"
4344
)
4445

4546
var externalReadyWait = 30 * time.Second
@@ -89,7 +90,7 @@ func (r *Reconciler) ensureExternalOwnershipAndWatch(ctx context.Context, cluste
8990
}
9091

9192
// Ensure we add a watch to the external object, if there isn't one already.
92-
if err := r.externalTracker.Watch(log, obj, handler.EnqueueRequestForOwner(r.Client.Scheme(), r.Client.RESTMapper(), &clusterv1.Machine{})); err != nil {
93+
if err := r.externalTracker.Watch(log, obj, handler.EnqueueRequestForOwner(r.Client.Scheme(), r.Client.RESTMapper(), &clusterv1.Machine{}), predicates.ResourceIsChanged(r.Client.Scheme(), *r.externalTracker.PredicateLogger)); err != nil {
9394
return nil, err
9495
}
9596

internal/controllers/machinedeployment/machinedeployment_controller.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,22 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
9191

9292
err = ctrl.NewControllerManagedBy(mgr).
9393
For(&clusterv1.MachineDeployment{}).
94-
Owns(&clusterv1.MachineSet{}).
94+
Owns(&clusterv1.MachineSet{}, builder.WithPredicates(predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog))).
9595
// Watches enqueues MachineDeployment for corresponding MachineSet resources, if no managed controller reference (owner) exists.
9696
Watches(
9797
&clusterv1.MachineSet{},
9898
handler.EnqueueRequestsFromMapFunc(r.MachineSetToDeployments),
99+
builder.WithPredicates(predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog)),
99100
).
100101
WithOptions(options).
101102
WithEventFilter(predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue)).
102103
Watches(
103104
&clusterv1.Cluster{},
104105
handler.EnqueueRequestsFromMapFunc(clusterToMachineDeployments),
105-
builder.WithPredicates(
106+
builder.WithPredicates(predicates.All(mgr.GetScheme(), predicateLog,
107+
predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog),
106108
predicates.ClusterPausedTransitions(mgr.GetScheme(), predicateLog),
107-
),
109+
)),
108110
// TODO: should this wait for Cluster.Status.InfrastructureReady similar to Infra Machine resources?
109111
).Complete(r)
110112
if err != nil {

0 commit comments

Comments
 (0)