Skip to content

Commit 604a465

Browse files
authored
Merge pull request #4929 from fiunchinho/missing-watch-filter
🐛 Missing watch label to allow multiple manager instances
2 parents 4d087c0 + 9fbe827 commit 604a465

File tree

6 files changed

+48
-23
lines changed

6 files changed

+48
-23
lines changed

bootstrap/kubeadm/controllers/kubeadmconfig_controller.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ type InitLocker interface {
7171

7272
// KubeadmConfigReconciler reconciles a KubeadmConfig object.
7373
type KubeadmConfigReconciler struct {
74-
Client client.Client
75-
KubeadmInitLock InitLocker
74+
Client client.Client
75+
KubeadmInitLock InitLocker
76+
WatchFilterValue string
7677

7778
remoteClientGetter remote.ClusterClientGetter
7879
}
@@ -97,7 +98,7 @@ func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl
9798
b := ctrl.NewControllerManagedBy(mgr).
9899
For(&bootstrapv1.KubeadmConfig{}).
99100
WithOptions(option).
100-
WithEventFilter(predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx))).
101+
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
101102
Watches(
102103
&source.Kind{Type: &clusterv1.Machine{}},
103104
handler.EnqueueRequestsFromMapFunc(r.MachineToBootstrapMapFunc),
@@ -107,7 +108,7 @@ func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl
107108
b = b.Watches(
108109
&source.Kind{Type: &expv1.MachinePool{}},
109110
handler.EnqueueRequestsFromMapFunc(r.MachinePoolToBootstrapMapFunc),
110-
)
111+
).WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue))
111112
}
112113

113114
c, err := b.Build(r)
@@ -118,7 +119,10 @@ func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl
118119
err = c.Watch(
119120
&source.Kind{Type: &clusterv1.Cluster{}},
120121
handler.EnqueueRequestsFromMapFunc(r.ClusterToKubeadmConfigs),
121-
predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx)),
122+
predicates.All(ctrl.LoggerFrom(ctx),
123+
predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx)),
124+
predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue),
125+
),
122126
)
123127
if err != nil {
124128
return errors.Wrap(err, "failed adding Watch for Clusters to controller manager")

bootstrap/kubeadm/main.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919
import (
2020
"context"
2121
"flag"
22+
"fmt"
2223
"math/rand"
2324
"net/http"
2425
_ "net/http/pprof"
@@ -69,6 +70,7 @@ var (
6970
leaderElectionLeaseDuration time.Duration
7071
leaderElectionRenewDeadline time.Duration
7172
leaderElectionRetryPeriod time.Duration
73+
watchFilterValue string
7274
watchNamespace string
7375
profilerAddress string
7476
kubeadmConfigConcurrency int
@@ -110,6 +112,9 @@ func InitFlags(fs *pflag.FlagSet) {
110112
fs.DurationVar(&kubeadmbootstrapcontrollers.DefaultTokenTTL, "bootstrap-token-ttl", 15*time.Minute,
111113
"The amount of time the bootstrap token will be valid")
112114

115+
fs.StringVar(&watchFilterValue, "watch-filter", "",
116+
fmt.Sprintf("Label value that the controller watches to reconcile cluster-api objects. Label key is always %s. If unspecified, the controller watches for all cluster-api objects.", clusterv1.WatchLabel))
117+
113118
fs.IntVar(&webhookPort, "webhook-port", 9443,
114119
"Webhook Server port")
115120

@@ -193,7 +198,8 @@ func setupChecks(mgr ctrl.Manager) {
193198

194199
func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
195200
if err := (&kubeadmbootstrapcontrollers.KubeadmConfigReconciler{
196-
Client: mgr.GetClient(),
201+
Client: mgr.GetClient(),
202+
WatchFilterValue: watchFilterValue,
197203
}).SetupWithManager(ctx, mgr, concurrency(kubeadmConfigConcurrency)); err != nil {
198204
setupLog.Error(err, "unable to create controller", "controller", "KubeadmConfig")
199205
os.Exit(1)

controllers/remote/cluster_cache_reconciler.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/pkg/errors"
2424
apierrors "k8s.io/apimachinery/pkg/api/errors"
2525
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
26+
"sigs.k8s.io/cluster-api/util/predicates"
2627
ctrl "sigs.k8s.io/controller-runtime"
2728
"sigs.k8s.io/controller-runtime/pkg/client"
2829
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -32,15 +33,17 @@ import (
3233
// ClusterCacheReconciler is responsible for stopping remote cluster caches when
3334
// the cluster for the remote cache is being deleted.
3435
type ClusterCacheReconciler struct {
35-
Log logr.Logger
36-
Client client.Client
37-
Tracker *ClusterCacheTracker
36+
Log logr.Logger
37+
Client client.Client
38+
Tracker *ClusterCacheTracker
39+
WatchFilterValue string
3840
}
3941

4042
func (r *ClusterCacheReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
4143
_, err := ctrl.NewControllerManagedBy(mgr).
4244
For(&clusterv1.Cluster{}).
4345
WithOptions(options).
46+
WithEventFilter(predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
4447
Build(r)
4548

4649
if err != nil {

controlplane/kubeadm/controllers/controller.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ import (
6060

6161
// KubeadmControlPlaneReconciler reconciles a KubeadmControlPlane object.
6262
type KubeadmControlPlaneReconciler struct {
63-
Client client.Client
64-
controller controller.Controller
65-
recorder record.EventRecorder
66-
Tracker *remote.ClusterCacheTracker
63+
Client client.Client
64+
controller controller.Controller
65+
recorder record.EventRecorder
66+
Tracker *remote.ClusterCacheTracker
67+
WatchFilterValue string
6768

6869
managementCluster internal.ManagementCluster
6970
managementClusterUncached internal.ManagementCluster
@@ -74,7 +75,7 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
7475
For(&controlplanev1.KubeadmControlPlane{}).
7576
Owns(&clusterv1.Machine{}).
7677
WithOptions(options).
77-
WithEventFilter(predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx))).
78+
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
7879
Build(r)
7980
if err != nil {
8081
return errors.Wrap(err, "failed setting up with a controller manager")
@@ -83,7 +84,10 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
8384
err = c.Watch(
8485
&source.Kind{Type: &clusterv1.Cluster{}},
8586
handler.EnqueueRequestsFromMapFunc(r.ClusterToKubeadmControlPlane),
86-
predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx)),
87+
predicates.All(ctrl.LoggerFrom(ctx),
88+
predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue),
89+
predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx)),
90+
),
8791
)
8892
if err != nil {
8993
return errors.Wrap(err, "failed adding Watch for Clusters to controller manager")

controlplane/kubeadm/main.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919
import (
2020
"context"
2121
"flag"
22+
"fmt"
2223
"math/rand"
2324
"net/http"
2425
_ "net/http/pprof"
@@ -68,6 +69,7 @@ var (
6869
leaderElectionLeaseDuration time.Duration
6970
leaderElectionRenewDeadline time.Duration
7071
leaderElectionRetryPeriod time.Duration
72+
watchFilterValue string
7173
watchNamespace string
7274
profilerAddress string
7375
kubeadmControlPlaneConcurrency int
@@ -106,6 +108,9 @@ func InitFlags(fs *pflag.FlagSet) {
106108
fs.DurationVar(&syncPeriod, "sync-period", 10*time.Minute,
107109
"The minimum interval at which watched resources are reconciled (e.g. 15m)")
108110

111+
fs.StringVar(&watchFilterValue, "watch-filter", "",
112+
fmt.Sprintf("Label value that the controller watches to reconcile cluster-api objects. Label key is always %s. If unspecified, the controller watches for all cluster-api objects.", clusterv1.WatchLabel))
113+
109114
fs.IntVar(&webhookPort, "webhook-port", 9443,
110115
"Webhook Server port")
111116

@@ -193,17 +198,19 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
193198
os.Exit(1)
194199
}
195200
if err := (&remote.ClusterCacheReconciler{
196-
Client: mgr.GetClient(),
197-
Log: ctrl.Log.WithName("remote").WithName("ClusterCacheReconciler"),
198-
Tracker: tracker,
201+
Client: mgr.GetClient(),
202+
Log: ctrl.Log.WithName("remote").WithName("ClusterCacheReconciler"),
203+
Tracker: tracker,
204+
WatchFilterValue: watchFilterValue,
199205
}).SetupWithManager(ctx, mgr, concurrency(kubeadmControlPlaneConcurrency)); err != nil {
200206
setupLog.Error(err, "unable to create controller", "controller", "ClusterCacheReconciler")
201207
os.Exit(1)
202208
}
203209

204210
if err := (&kubeadmcontrolplanecontrollers.KubeadmControlPlaneReconciler{
205-
Client: mgr.GetClient(),
206-
Tracker: tracker,
211+
Client: mgr.GetClient(),
212+
Tracker: tracker,
213+
WatchFilterValue: watchFilterValue,
207214
}).SetupWithManager(ctx, mgr, concurrency(kubeadmControlPlaneConcurrency)); err != nil {
208215
setupLog.Error(err, "unable to create controller", "controller", "KubeadmControlPlane")
209216
os.Exit(1)

main.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,10 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
244244
os.Exit(1)
245245
}
246246
if err := (&remote.ClusterCacheReconciler{
247-
Client: mgr.GetClient(),
248-
Log: ctrl.Log.WithName("remote").WithName("ClusterCacheReconciler"),
249-
Tracker: tracker,
247+
Client: mgr.GetClient(),
248+
Log: ctrl.Log.WithName("remote").WithName("ClusterCacheReconciler"),
249+
Tracker: tracker,
250+
WatchFilterValue: watchFilterValue,
250251
}).SetupWithManager(ctx, mgr, concurrency(clusterConcurrency)); err != nil {
251252
setupLog.Error(err, "unable to create controller", "controller", "ClusterCacheReconciler")
252253
os.Exit(1)

0 commit comments

Comments
 (0)