Skip to content

Commit beba100

Browse files
committed
add namespace filtering
1 parent d1ca160 commit beba100

File tree

5 files changed

+149
-89
lines changed

5 files changed

+149
-89
lines changed

api/v1beta1/common_types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const (
107107
// older MachineSets when Machines are deleted and add the new replicas to the latest MachineSet.
108108
DisableMachineCreateAnnotation = "cluster.x-k8s.io/disable-machine-create"
109109

110-
// WatchLabel is a label othat can be applied to any Cluster API object.
110+
// WatchLabel is a label that can be applied to any Cluster API object.
111111
//
112112
// Controllers which allow for selective reconciliation may check this label and proceed
113113
// with reconciliation of the object only if this label and a configured value is present.

bootstrap/kubeadm/main.go

+37-22
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/spf13/pflag"
2929
corev1 "k8s.io/api/core/v1"
30+
"k8s.io/apimachinery/pkg/fields"
3031
"k8s.io/apimachinery/pkg/labels"
3132
"k8s.io/apimachinery/pkg/runtime"
3233
"k8s.io/apimachinery/pkg/selection"
@@ -64,26 +65,27 @@ var (
6465
controllerName = "cluster-api-kubeadm-bootstrap-manager"
6566

6667
// flags.
67-
enableLeaderElection bool
68-
leaderElectionLeaseDuration time.Duration
69-
leaderElectionRenewDeadline time.Duration
70-
leaderElectionRetryPeriod time.Duration
71-
watchFilterValue string
72-
watchNamespace string
73-
profilerAddress string
74-
enableContentionProfiling bool
75-
syncPeriod time.Duration
76-
restConfigQPS float32
77-
restConfigBurst int
78-
clusterCacheClientQPS float32
79-
clusterCacheClientBurst int
80-
webhookPort int
81-
webhookCertDir string
82-
webhookCertName string
83-
webhookKeyName string
84-
healthAddr string
85-
managerOptions = flags.ManagerOptions{}
86-
logOptions = logs.NewOptions()
68+
enableLeaderElection bool
69+
leaderElectionLeaseDuration time.Duration
70+
leaderElectionRenewDeadline time.Duration
71+
leaderElectionRetryPeriod time.Duration
72+
watchFilterValue string
73+
watchFilterExcludedNamespaces []string
74+
watchNamespace string
75+
profilerAddress string
76+
enableContentionProfiling bool
77+
syncPeriod time.Duration
78+
restConfigQPS float32
79+
restConfigBurst int
80+
clusterCacheClientQPS float32
81+
clusterCacheClientBurst int
82+
webhookPort int
83+
webhookCertDir string
84+
webhookCertName string
85+
webhookKeyName string
86+
healthAddr string
87+
managerOptions = flags.ManagerOptions{}
88+
logOptions = logs.NewOptions()
8789
// CABPK specific flags.
8890
clusterConcurrency int
8991
clusterCacheConcurrency int
@@ -122,6 +124,9 @@ func InitFlags(fs *pflag.FlagSet) {
122124
fs.StringVar(&watchFilterValue, "watch-filter", "",
123125
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))
124126

127+
fs.StringSliceVar(&watchFilterExcludedNamespaces, "excluded-namespaces", nil,
128+
"Comma separated list of names. Exclude the namespaces controller watches to reconcile cluster-api objects.")
129+
125130
fs.StringVar(&profilerAddress, "profiler-address", "",
126131
"Bind address to expose the pprof profiler (e.g. localhost:6060)")
127132

@@ -218,6 +223,15 @@ func main() {
218223
}
219224
}
220225

226+
var fieldSelector fields.Selector
227+
if watchFilterExcludedNamespaces != nil {
228+
var conditions []fields.Selector
229+
for i := range watchFilterExcludedNamespaces {
230+
conditions = append(conditions, fields.OneTermNotEqualSelector("metadata.namespace", watchFilterExcludedNamespaces[i]))
231+
}
232+
fieldSelector = fields.AndSelectors(conditions...)
233+
}
234+
221235
if enableContentionProfiling {
222236
goruntime.SetBlockProfileRate(1)
223237
}
@@ -237,8 +251,9 @@ func main() {
237251
PprofBindAddress: profilerAddress,
238252
Metrics: *metricsOptions,
239253
Cache: cache.Options{
240-
DefaultNamespaces: watchNamespaces,
241-
SyncPeriod: &syncPeriod,
254+
DefaultFieldSelector: fieldSelector,
255+
DefaultNamespaces: watchNamespaces,
256+
SyncPeriod: &syncPeriod,
242257
ByObject: map[client.Object]cache.ByObject{
243258
// Note: Only Secrets with the cluster name label are cached.
244259
// The default client of the manager won't use the cache for secrets at all (see Client.Cache.DisableFor).

controlplane/kubeadm/main.go

+37-22
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
appsv1 "k8s.io/api/apps/v1"
3131
corev1 "k8s.io/api/core/v1"
3232
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
33+
"k8s.io/apimachinery/pkg/fields"
3334
"k8s.io/apimachinery/pkg/labels"
3435
"k8s.io/apimachinery/pkg/runtime"
3536
"k8s.io/apimachinery/pkg/selection"
@@ -69,26 +70,27 @@ var (
6970
controllerName = "cluster-api-kubeadm-control-plane-manager"
7071

7172
// flags.
72-
enableLeaderElection bool
73-
leaderElectionLeaseDuration time.Duration
74-
leaderElectionRenewDeadline time.Duration
75-
leaderElectionRetryPeriod time.Duration
76-
watchFilterValue string
77-
watchNamespace string
78-
profilerAddress string
79-
enableContentionProfiling bool
80-
syncPeriod time.Duration
81-
restConfigQPS float32
82-
restConfigBurst int
83-
clusterCacheClientQPS float32
84-
clusterCacheClientBurst int
85-
webhookPort int
86-
webhookCertDir string
87-
webhookCertName string
88-
webhookKeyName string
89-
healthAddr string
90-
managerOptions = flags.ManagerOptions{}
91-
logOptions = logs.NewOptions()
73+
enableLeaderElection bool
74+
leaderElectionLeaseDuration time.Duration
75+
leaderElectionRenewDeadline time.Duration
76+
leaderElectionRetryPeriod time.Duration
77+
watchFilterValue string
78+
watchFilterExcludedNamespaces []string
79+
watchNamespace string
80+
profilerAddress string
81+
enableContentionProfiling bool
82+
syncPeriod time.Duration
83+
restConfigQPS float32
84+
restConfigBurst int
85+
clusterCacheClientQPS float32
86+
clusterCacheClientBurst int
87+
webhookPort int
88+
webhookCertDir string
89+
webhookCertName string
90+
webhookKeyName string
91+
healthAddr string
92+
managerOptions = flags.ManagerOptions{}
93+
logOptions = logs.NewOptions()
9294
// KCP specific flags.
9395
remoteConditionsGracePeriod time.Duration
9496
kubeadmControlPlaneConcurrency int
@@ -131,6 +133,9 @@ func InitFlags(fs *pflag.FlagSet) {
131133
fs.StringVar(&watchFilterValue, "watch-filter", "",
132134
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))
133135

136+
fs.StringSliceVar(&watchFilterExcludedNamespaces, "excluded-namespaces", nil,
137+
"Comma separated list of names. Exclude the namespaces controller watches to reconcile cluster-api objects.")
138+
134139
fs.StringVar(&profilerAddress, "profiler-address", "",
135140
"Bind address to expose the pprof profiler (e.g. localhost:6060)")
136141

@@ -243,6 +248,15 @@ func main() {
243248
}
244249
}
245250

251+
var fieldSelector fields.Selector
252+
if watchFilterExcludedNamespaces != nil {
253+
var conditions []fields.Selector
254+
for i := range watchFilterExcludedNamespaces {
255+
conditions = append(conditions, fields.OneTermNotEqualSelector("metadata.namespace", watchFilterExcludedNamespaces[i]))
256+
}
257+
fieldSelector = fields.AndSelectors(conditions...)
258+
}
259+
246260
if enableContentionProfiling {
247261
goruntime.SetBlockProfileRate(1)
248262
}
@@ -262,8 +276,9 @@ func main() {
262276
PprofBindAddress: profilerAddress,
263277
Metrics: *metricsOptions,
264278
Cache: cache.Options{
265-
DefaultNamespaces: watchNamespaces,
266-
SyncPeriod: &syncPeriod,
279+
DefaultFieldSelector: fieldSelector,
280+
DefaultNamespaces: watchNamespaces,
281+
SyncPeriod: &syncPeriod,
267282
ByObject: map[client.Object]cache.ByObject{
268283
// Note: Only Secrets with the cluster name label are cached.
269284
// The default client of the manager won't use the cache for secrets at all (see Client.Cache.DisableFor).

main.go

+37-22
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
storagev1 "k8s.io/api/storage/v1"
3333
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3434
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
35+
"k8s.io/apimachinery/pkg/fields"
3536
"k8s.io/apimachinery/pkg/labels"
3637
"k8s.io/apimachinery/pkg/runtime"
3738
"k8s.io/apimachinery/pkg/selection"
@@ -88,26 +89,27 @@ var (
8889
controllerName = "cluster-api-controller-manager"
8990

9091
// flags.
91-
enableLeaderElection bool
92-
leaderElectionLeaseDuration time.Duration
93-
leaderElectionRenewDeadline time.Duration
94-
leaderElectionRetryPeriod time.Duration
95-
watchFilterValue string
96-
watchNamespace string
97-
profilerAddress string
98-
enableContentionProfiling bool
99-
syncPeriod time.Duration
100-
restConfigQPS float32
101-
restConfigBurst int
102-
clusterCacheClientQPS float32
103-
clusterCacheClientBurst int
104-
webhookPort int
105-
webhookCertDir string
106-
webhookCertName string
107-
webhookKeyName string
108-
healthAddr string
109-
managerOptions = flags.ManagerOptions{}
110-
logOptions = logs.NewOptions()
92+
enableLeaderElection bool
93+
leaderElectionLeaseDuration time.Duration
94+
leaderElectionRenewDeadline time.Duration
95+
leaderElectionRetryPeriod time.Duration
96+
watchFilterValue string
97+
watchFilterExcludedNamespaces []string
98+
watchNamespace string
99+
profilerAddress string
100+
enableContentionProfiling bool
101+
syncPeriod time.Duration
102+
restConfigQPS float32
103+
restConfigBurst int
104+
clusterCacheClientQPS float32
105+
clusterCacheClientBurst int
106+
webhookPort int
107+
webhookCertDir string
108+
webhookCertName string
109+
webhookKeyName string
110+
healthAddr string
111+
managerOptions = flags.ManagerOptions{}
112+
logOptions = logs.NewOptions()
111113
// core Cluster API specific flags.
112114
remoteConnectionGracePeriod time.Duration
113115
remoteConditionsGracePeriod time.Duration
@@ -172,6 +174,9 @@ func InitFlags(fs *pflag.FlagSet) {
172174
fs.StringVar(&watchFilterValue, "watch-filter", "",
173175
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))
174176

177+
fs.StringSliceVar(&watchFilterExcludedNamespaces, "excluded-namespaces", nil,
178+
"Comma separated list of names. Exclude the namespaces controller watches to reconcile cluster-api objects.")
179+
175180
fs.StringVar(&profilerAddress, "profiler-address", "",
176181
"Bind address to expose the pprof profiler (e.g. localhost:6060)")
177182

@@ -323,6 +328,15 @@ func main() {
323328
}
324329
}
325330

331+
var fieldSelector fields.Selector
332+
if watchFilterExcludedNamespaces != nil {
333+
var conditions []fields.Selector
334+
for i := range watchFilterExcludedNamespaces {
335+
conditions = append(conditions, fields.OneTermNotEqualSelector("metadata.namespace", watchFilterExcludedNamespaces[i]))
336+
}
337+
fieldSelector = fields.AndSelectors(conditions...)
338+
}
339+
326340
if enableContentionProfiling {
327341
goruntime.SetBlockProfileRate(1)
328342
}
@@ -342,8 +356,9 @@ func main() {
342356
PprofBindAddress: profilerAddress,
343357
Metrics: *metricsOptions,
344358
Cache: cache.Options{
345-
DefaultNamespaces: watchNamespaces,
346-
SyncPeriod: &syncPeriod,
359+
DefaultFieldSelector: fieldSelector,
360+
DefaultNamespaces: watchNamespaces,
361+
SyncPeriod: &syncPeriod,
347362
ByObject: map[client.Object]cache.ByObject{
348363
// Note: Only Secrets with the cluster name label are cached.
349364
// The default client of the manager won't use the cache for secrets at all (see Client.Cache.DisableFor).

test/infrastructure/docker/main.go

+37-22
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/spf13/pflag"
2929
corev1 "k8s.io/api/core/v1"
30+
"k8s.io/apimachinery/pkg/fields"
3031
"k8s.io/apimachinery/pkg/labels"
3132
"k8s.io/apimachinery/pkg/runtime"
3233
"k8s.io/apimachinery/pkg/selection"
@@ -70,26 +71,27 @@ var (
7071
controllerName = "cluster-api-docker-controller-manager"
7172

7273
// flags.
73-
enableLeaderElection bool
74-
leaderElectionLeaseDuration time.Duration
75-
leaderElectionRenewDeadline time.Duration
76-
leaderElectionRetryPeriod time.Duration
77-
watchFilterValue string
78-
watchNamespace string
79-
profilerAddress string
80-
enableContentionProfiling bool
81-
syncPeriod time.Duration
82-
restConfigQPS float32
83-
restConfigBurst int
84-
clusterCacheClientQPS float32
85-
clusterCacheClientBurst int
86-
webhookPort int
87-
webhookCertDir string
88-
webhookCertName string
89-
webhookKeyName string
90-
healthAddr string
91-
managerOptions = flags.ManagerOptions{}
92-
logOptions = logs.NewOptions()
74+
enableLeaderElection bool
75+
leaderElectionLeaseDuration time.Duration
76+
leaderElectionRenewDeadline time.Duration
77+
leaderElectionRetryPeriod time.Duration
78+
watchFilterValue string
79+
watchFilterExcludedNamespaces []string
80+
watchNamespace string
81+
profilerAddress string
82+
enableContentionProfiling bool
83+
syncPeriod time.Duration
84+
restConfigQPS float32
85+
restConfigBurst int
86+
clusterCacheClientQPS float32
87+
clusterCacheClientBurst int
88+
webhookPort int
89+
webhookCertDir string
90+
webhookCertName string
91+
webhookKeyName string
92+
healthAddr string
93+
managerOptions = flags.ManagerOptions{}
94+
logOptions = logs.NewOptions()
9395
// CAPD specific flags.
9496
concurrency int
9597
clusterCacheConcurrency int
@@ -129,6 +131,9 @@ func InitFlags(fs *pflag.FlagSet) {
129131
fs.StringVar(&watchFilterValue, "watch-filter", "",
130132
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))
131133

134+
fs.StringSliceVar(&watchFilterExcludedNamespaces, "excluded-namespaces", nil,
135+
"Comma separated list of names. Exclude the namespaces controller watches to reconcile cluster-api objects.")
136+
132137
fs.StringVar(&profilerAddress, "profiler-address", "",
133138
"Bind address to expose the pprof profiler (e.g. localhost:6060)")
134139

@@ -222,6 +227,15 @@ func main() {
222227
}
223228
}
224229

230+
var fieldSelector fields.Selector
231+
if watchFilterExcludedNamespaces != nil {
232+
var conditions []fields.Selector
233+
for i := range watchFilterExcludedNamespaces {
234+
conditions = append(conditions, fields.OneTermNotEqualSelector("metadata.namespace", watchFilterExcludedNamespaces[i]))
235+
}
236+
fieldSelector = fields.AndSelectors(conditions...)
237+
}
238+
225239
if enableContentionProfiling {
226240
goruntime.SetBlockProfileRate(1)
227241
}
@@ -241,8 +255,9 @@ func main() {
241255
PprofBindAddress: profilerAddress,
242256
Metrics: *metricsOptions,
243257
Cache: cache.Options{
244-
DefaultNamespaces: watchNamespaces,
245-
SyncPeriod: &syncPeriod,
258+
DefaultFieldSelector: fieldSelector,
259+
DefaultNamespaces: watchNamespaces,
260+
SyncPeriod: &syncPeriod,
246261
ByObject: map[client.Object]cache.ByObject{
247262
// Note: Only Secrets with the cluster name label are cached.
248263
// The default client of the manager won't use the cache for secrets at all (see Client.Cache.DisableFor).

0 commit comments

Comments
 (0)