Skip to content

Commit

Permalink
add namespace filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
marek-veber committed Nov 11, 2024
1 parent e13abea commit 4c5ea6f
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 89 deletions.
2 changes: 1 addition & 1 deletion api/v1beta1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const (
// older MachineSets when Machines are deleted and add the new replicas to the latest MachineSet.
DisableMachineCreateAnnotation = "cluster.x-k8s.io/disable-machine-create"

// WatchLabel is a label othat can be applied to any Cluster API object.
// WatchLabel is a label that can be applied to any Cluster API object.
//
// Controllers which allow for selective reconciliation may check this label and proceed
// with reconciliation of the object only if this label and a configured value is present.
Expand Down
59 changes: 37 additions & 22 deletions bootstrap/kubeadm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/spf13/pflag"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
Expand Down Expand Up @@ -64,26 +65,27 @@ var (
controllerName = "cluster-api-kubeadm-bootstrap-manager"

// flags.
enableLeaderElection bool
leaderElectionLeaseDuration time.Duration
leaderElectionRenewDeadline time.Duration
leaderElectionRetryPeriod time.Duration
watchFilterValue string
watchNamespace string
profilerAddress string
enableContentionProfiling bool
syncPeriod time.Duration
restConfigQPS float32
restConfigBurst int
clusterCacheClientQPS float32
clusterCacheClientBurst int
webhookPort int
webhookCertDir string
webhookCertName string
webhookKeyName string
healthAddr string
managerOptions = flags.ManagerOptions{}
logOptions = logs.NewOptions()
enableLeaderElection bool
leaderElectionLeaseDuration time.Duration
leaderElectionRenewDeadline time.Duration
leaderElectionRetryPeriod time.Duration
watchFilterValue string
watchExcludedNamespaces []string
watchNamespace string
profilerAddress string
enableContentionProfiling bool
syncPeriod time.Duration
restConfigQPS float32
restConfigBurst int
clusterCacheClientQPS float32
clusterCacheClientBurst int
webhookPort int
webhookCertDir string
webhookCertName string
webhookKeyName string
healthAddr string
managerOptions = flags.ManagerOptions{}
logOptions = logs.NewOptions()
// CABPK specific flags.
clusterConcurrency int
clusterCacheConcurrency int
Expand Down Expand Up @@ -122,6 +124,9 @@ func InitFlags(fs *pflag.FlagSet) {
fs.StringVar(&watchFilterValue, "watch-filter", "",
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))

fs.StringSliceVar(&watchExcludedNamespaces, "excluded-namespace", nil,
"Comma separated list of namespaces. Exclude the namespaces controller watches to reconcile cluster-api objects.")

fs.StringVar(&profilerAddress, "profiler-address", "",
"Bind address to expose the pprof profiler (e.g. localhost:6060)")

Expand Down Expand Up @@ -218,6 +223,15 @@ func main() {
}
}

var fieldSelector fields.Selector
if watchExcludedNamespaces != nil {
var conditions []fields.Selector
for i := range watchExcludedNamespaces {
conditions = append(conditions, fields.OneTermNotEqualSelector("metadata.namespace", watchExcludedNamespaces[i]))
}
fieldSelector = fields.AndSelectors(conditions...)
}

if enableContentionProfiling {
goruntime.SetBlockProfileRate(1)
}
Expand All @@ -237,8 +251,9 @@ func main() {
PprofBindAddress: profilerAddress,
Metrics: *metricsOptions,
Cache: cache.Options{
DefaultNamespaces: watchNamespaces,
SyncPeriod: &syncPeriod,
DefaultFieldSelector: fieldSelector,
DefaultNamespaces: watchNamespaces,
SyncPeriod: &syncPeriod,
ByObject: map[client.Object]cache.ByObject{
// Note: Only Secrets with the cluster name label are cached.
// The default client of the manager won't use the cache for secrets at all (see Client.Cache.DisableFor).
Expand Down
59 changes: 37 additions & 22 deletions controlplane/kubeadm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
Expand Down Expand Up @@ -69,26 +70,27 @@ var (
controllerName = "cluster-api-kubeadm-control-plane-manager"

// flags.
enableLeaderElection bool
leaderElectionLeaseDuration time.Duration
leaderElectionRenewDeadline time.Duration
leaderElectionRetryPeriod time.Duration
watchFilterValue string
watchNamespace string
profilerAddress string
enableContentionProfiling bool
syncPeriod time.Duration
restConfigQPS float32
restConfigBurst int
clusterCacheClientQPS float32
clusterCacheClientBurst int
webhookPort int
webhookCertDir string
webhookCertName string
webhookKeyName string
healthAddr string
managerOptions = flags.ManagerOptions{}
logOptions = logs.NewOptions()
enableLeaderElection bool
leaderElectionLeaseDuration time.Duration
leaderElectionRenewDeadline time.Duration
leaderElectionRetryPeriod time.Duration
watchFilterValue string
watchExcludedNamespaces []string
watchNamespace string
profilerAddress string
enableContentionProfiling bool
syncPeriod time.Duration
restConfigQPS float32
restConfigBurst int
clusterCacheClientQPS float32
clusterCacheClientBurst int
webhookPort int
webhookCertDir string
webhookCertName string
webhookKeyName string
healthAddr string
managerOptions = flags.ManagerOptions{}
logOptions = logs.NewOptions()
// KCP specific flags.
remoteConditionsGracePeriod time.Duration
kubeadmControlPlaneConcurrency int
Expand Down Expand Up @@ -131,6 +133,9 @@ func InitFlags(fs *pflag.FlagSet) {
fs.StringVar(&watchFilterValue, "watch-filter", "",
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))

fs.StringSliceVar(&watchExcludedNamespaces, "excluded-namespace", nil,
"Comma separated list of namespaces. Exclude the namespaces controller watches to reconcile cluster-api objects.")

fs.StringVar(&profilerAddress, "profiler-address", "",
"Bind address to expose the pprof profiler (e.g. localhost:6060)")

Expand Down Expand Up @@ -243,6 +248,15 @@ func main() {
}
}

var fieldSelector fields.Selector
if watchExcludedNamespaces != nil {
var conditions []fields.Selector
for i := range watchExcludedNamespaces {
conditions = append(conditions, fields.OneTermNotEqualSelector("metadata.namespace", watchExcludedNamespaces[i]))
}
fieldSelector = fields.AndSelectors(conditions...)
}

if enableContentionProfiling {
goruntime.SetBlockProfileRate(1)
}
Expand All @@ -262,8 +276,9 @@ func main() {
PprofBindAddress: profilerAddress,
Metrics: *metricsOptions,
Cache: cache.Options{
DefaultNamespaces: watchNamespaces,
SyncPeriod: &syncPeriod,
DefaultFieldSelector: fieldSelector,
DefaultNamespaces: watchNamespaces,
SyncPeriod: &syncPeriod,
ByObject: map[client.Object]cache.ByObject{
// Note: Only Secrets with the cluster name label are cached.
// The default client of the manager won't use the cache for secrets at all (see Client.Cache.DisableFor).
Expand Down
59 changes: 37 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
storagev1 "k8s.io/api/storage/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
Expand Down Expand Up @@ -88,26 +89,27 @@ var (
controllerName = "cluster-api-controller-manager"

// flags.
enableLeaderElection bool
leaderElectionLeaseDuration time.Duration
leaderElectionRenewDeadline time.Duration
leaderElectionRetryPeriod time.Duration
watchFilterValue string
watchNamespace string
profilerAddress string
enableContentionProfiling bool
syncPeriod time.Duration
restConfigQPS float32
restConfigBurst int
clusterCacheClientQPS float32
clusterCacheClientBurst int
webhookPort int
webhookCertDir string
webhookCertName string
webhookKeyName string
healthAddr string
managerOptions = flags.ManagerOptions{}
logOptions = logs.NewOptions()
enableLeaderElection bool
leaderElectionLeaseDuration time.Duration
leaderElectionRenewDeadline time.Duration
leaderElectionRetryPeriod time.Duration
watchFilterValue string
watchExcludedNamespaces []string
watchNamespace string
profilerAddress string
enableContentionProfiling bool
syncPeriod time.Duration
restConfigQPS float32
restConfigBurst int
clusterCacheClientQPS float32
clusterCacheClientBurst int
webhookPort int
webhookCertDir string
webhookCertName string
webhookKeyName string
healthAddr string
managerOptions = flags.ManagerOptions{}
logOptions = logs.NewOptions()
// core Cluster API specific flags.
remoteConnectionGracePeriod time.Duration
remoteConditionsGracePeriod time.Duration
Expand Down Expand Up @@ -172,6 +174,9 @@ func InitFlags(fs *pflag.FlagSet) {
fs.StringVar(&watchFilterValue, "watch-filter", "",
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))

fs.StringSliceVar(&watchExcludedNamespaces, "excluded-namespace", nil,
"Comma separated list of namespaces. Exclude the namespaces controller watches to reconcile cluster-api objects.")

fs.StringVar(&profilerAddress, "profiler-address", "",
"Bind address to expose the pprof profiler (e.g. localhost:6060)")

Expand Down Expand Up @@ -323,6 +328,15 @@ func main() {
}
}

var fieldSelector fields.Selector
if watchExcludedNamespaces != nil {
var conditions []fields.Selector
for i := range watchExcludedNamespaces {
conditions = append(conditions, fields.OneTermNotEqualSelector("metadata.namespace", watchExcludedNamespaces[i]))
}
fieldSelector = fields.AndSelectors(conditions...)
}

if enableContentionProfiling {
goruntime.SetBlockProfileRate(1)
}
Expand All @@ -342,8 +356,9 @@ func main() {
PprofBindAddress: profilerAddress,
Metrics: *metricsOptions,
Cache: cache.Options{
DefaultNamespaces: watchNamespaces,
SyncPeriod: &syncPeriod,
DefaultFieldSelector: fieldSelector,
DefaultNamespaces: watchNamespaces,
SyncPeriod: &syncPeriod,
ByObject: map[client.Object]cache.ByObject{
// Note: Only Secrets with the cluster name label are cached.
// The default client of the manager won't use the cache for secrets at all (see Client.Cache.DisableFor).
Expand Down
59 changes: 37 additions & 22 deletions test/infrastructure/docker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/spf13/pflag"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
Expand Down Expand Up @@ -70,26 +71,27 @@ var (
controllerName = "cluster-api-docker-controller-manager"

// flags.
enableLeaderElection bool
leaderElectionLeaseDuration time.Duration
leaderElectionRenewDeadline time.Duration
leaderElectionRetryPeriod time.Duration
watchFilterValue string
watchNamespace string
profilerAddress string
enableContentionProfiling bool
syncPeriod time.Duration
restConfigQPS float32
restConfigBurst int
clusterCacheClientQPS float32
clusterCacheClientBurst int
webhookPort int
webhookCertDir string
webhookCertName string
webhookKeyName string
healthAddr string
managerOptions = flags.ManagerOptions{}
logOptions = logs.NewOptions()
enableLeaderElection bool
leaderElectionLeaseDuration time.Duration
leaderElectionRenewDeadline time.Duration
leaderElectionRetryPeriod time.Duration
watchFilterValue string
watchExcludedNamespaces []string
watchNamespace string
profilerAddress string
enableContentionProfiling bool
syncPeriod time.Duration
restConfigQPS float32
restConfigBurst int
clusterCacheClientQPS float32
clusterCacheClientBurst int
webhookPort int
webhookCertDir string
webhookCertName string
webhookKeyName string
healthAddr string
managerOptions = flags.ManagerOptions{}
logOptions = logs.NewOptions()
// CAPD specific flags.
concurrency int
clusterCacheConcurrency int
Expand Down Expand Up @@ -129,6 +131,9 @@ func InitFlags(fs *pflag.FlagSet) {
fs.StringVar(&watchFilterValue, "watch-filter", "",
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))

fs.StringSliceVar(&watchExcludedNamespaces, "excluded-namespaces", nil,
"Comma separated list of namespaces. Exclude the namespaces controller watches to reconcile cluster-api objects.")

fs.StringVar(&profilerAddress, "profiler-address", "",
"Bind address to expose the pprof profiler (e.g. localhost:6060)")

Expand Down Expand Up @@ -222,6 +227,15 @@ func main() {
}
}

var fieldSelector fields.Selector
if watchExcludedNamespaces != nil {
var conditions []fields.Selector
for i := range watchExcludedNamespaces {
conditions = append(conditions, fields.OneTermNotEqualSelector("metadata.namespace", watchExcludedNamespaces[i]))
}
fieldSelector = fields.AndSelectors(conditions...)
}

if enableContentionProfiling {
goruntime.SetBlockProfileRate(1)
}
Expand All @@ -241,8 +255,9 @@ func main() {
PprofBindAddress: profilerAddress,
Metrics: *metricsOptions,
Cache: cache.Options{
DefaultNamespaces: watchNamespaces,
SyncPeriod: &syncPeriod,
DefaultFieldSelector: fieldSelector,
DefaultNamespaces: watchNamespaces,
SyncPeriod: &syncPeriod,
ByObject: map[client.Object]cache.ByObject{
// Note: Only Secrets with the cluster name label are cached.
// The default client of the manager won't use the cache for secrets at all (see Client.Cache.DisableFor).
Expand Down

0 comments on commit 4c5ea6f

Please sign in to comment.