Skip to content

Commit eeaad04

Browse files
committed
add namespace filtering
1 parent 712e887 commit eeaad04

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+
watchExcludedNamespaces []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(&watchExcludedNamespaces, "excluded-namespace", nil,
128+
"Comma separated list of namespaces. 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 watchExcludedNamespaces != nil {
228+
var conditions []fields.Selector
229+
for i := range watchExcludedNamespaces {
230+
conditions = append(conditions, fields.OneTermNotEqualSelector("metadata.namespace", watchExcludedNamespaces[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
@@ -31,6 +31,7 @@ import (
3131
corev1 "k8s.io/api/core/v1"
3232
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3333
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34+
"k8s.io/apimachinery/pkg/fields"
3435
"k8s.io/apimachinery/pkg/labels"
3536
"k8s.io/apimachinery/pkg/runtime"
3637
"k8s.io/apimachinery/pkg/selection"
@@ -70,26 +71,27 @@ var (
7071
controllerName = "cluster-api-kubeadm-control-plane-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+
watchExcludedNamespaces []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
// KCP specific flags.
9496
remoteConditionsGracePeriod time.Duration
9597
kubeadmControlPlaneConcurrency int
@@ -132,6 +134,9 @@ func InitFlags(fs *pflag.FlagSet) {
132134
fs.StringVar(&watchFilterValue, "watch-filter", "",
133135
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))
134136

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

@@ -244,6 +249,15 @@ func main() {
244249
}
245250
}
246251

252+
var fieldSelector fields.Selector
253+
if watchExcludedNamespaces != nil {
254+
var conditions []fields.Selector
255+
for i := range watchExcludedNamespaces {
256+
conditions = append(conditions, fields.OneTermNotEqualSelector("metadata.namespace", watchExcludedNamespaces[i]))
257+
}
258+
fieldSelector = fields.AndSelectors(conditions...)
259+
}
260+
247261
if enableContentionProfiling {
248262
goruntime.SetBlockProfileRate(1)
249263
}
@@ -263,8 +277,9 @@ func main() {
263277
PprofBindAddress: profilerAddress,
264278
Metrics: *metricsOptions,
265279
Cache: cache.Options{
266-
DefaultNamespaces: watchNamespaces,
267-
SyncPeriod: &syncPeriod,
280+
DefaultFieldSelector: fieldSelector,
281+
DefaultNamespaces: watchNamespaces,
282+
SyncPeriod: &syncPeriod,
268283
ByObject: map[client.Object]cache.ByObject{
269284
// Note: Only Secrets with the cluster name label are cached.
270285
// 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+
watchExcludedNamespaces []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(&watchExcludedNamespaces, "excluded-namespace", nil,
178+
"Comma separated list of namespaces. 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 watchExcludedNamespaces != nil {
333+
var conditions []fields.Selector
334+
for i := range watchExcludedNamespaces {
335+
conditions = append(conditions, fields.OneTermNotEqualSelector("metadata.namespace", watchExcludedNamespaces[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+
watchExcludedNamespaces []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(&watchExcludedNamespaces, "excluded-namespaces", nil,
135+
"Comma separated list of namespaces. 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 watchExcludedNamespaces != nil {
232+
var conditions []fields.Selector
233+
for i := range watchExcludedNamespaces {
234+
conditions = append(conditions, fields.OneTermNotEqualSelector("metadata.namespace", watchExcludedNamespaces[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)