Skip to content

Commit 9fbe827

Browse files
Tuomas Mäkinenfiunchinho
Tuomas Mäkinen
authored andcommitted
Add watch-filter support to ClusterCacheReconciler
Add watch-filter support to kubeadm bootstrap controller Add event filtering based on optional watch-filter parameter to kubeadm boostrap controller. Add watch-filter support to kubeadm controlplane controller
1 parent 0452e8b commit 9fbe827

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
@@ -59,10 +59,11 @@ import (
5959

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

6768
managementCluster internal.ManagementCluster
6869
managementClusterUncached internal.ManagementCluster
@@ -73,7 +74,7 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
7374
For(&controlplanev1.KubeadmControlPlane{}).
7475
Owns(&clusterv1.Machine{}).
7576
WithOptions(options).
76-
WithEventFilter(predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx))).
77+
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
7778
Build(r)
7879
if err != nil {
7980
return errors.Wrap(err, "failed setting up with a controller manager")
@@ -82,7 +83,10 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
8283
err = c.Watch(
8384
&source.Kind{Type: &clusterv1.Cluster{}},
8485
handler.EnqueueRequestsFromMapFunc(r.ClusterToKubeadmControlPlane),
85-
predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx)),
86+
predicates.All(ctrl.LoggerFrom(ctx),
87+
predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue),
88+
predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx)),
89+
),
8690
)
8791
if err != nil {
8892
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)