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 4, 2024
1 parent d1ca160 commit 84ef63f
Show file tree
Hide file tree
Showing 39 changed files with 428 additions and 297 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
7 changes: 4 additions & 3 deletions bootstrap/kubeadm/controllers/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

kubeadmbootstrapcontrollers "sigs.k8s.io/cluster-api/bootstrap/kubeadm/internal/controllers"
"sigs.k8s.io/cluster-api/controllers/clustercache"
watchfilter "sigs.k8s.io/cluster-api/controllers/watchfilter"
)

// Following types provides access to reconcilers implemented in internal/controllers, thus
Expand All @@ -43,8 +44,8 @@ type KubeadmConfigReconciler struct {

ClusterCache clustercache.ClusterCache

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilter is used to filter events prior to reconciliation.
WatchFilter watchfilter.WatchFilter

// TokenTTL is the amount of time a bootstrap token (and therefore a KubeadmConfig) will be valid.
TokenTTL time.Duration
Expand All @@ -56,7 +57,7 @@ func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl
Client: r.Client,
SecretCachingClient: r.SecretCachingClient,
ClusterCache: r.ClusterCache,
WatchFilterValue: r.WatchFilterValue,
WatchFilter: r.WatchFilter,
TokenTTL: r.TokenTTL,
}).SetupWithManager(ctx, mgr, options)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
kubeadmtypes "sigs.k8s.io/cluster-api/bootstrap/kubeadm/types"
bsutil "sigs.k8s.io/cluster-api/bootstrap/util"
"sigs.k8s.io/cluster-api/controllers/clustercache"
watchfilter "sigs.k8s.io/cluster-api/controllers/watchfilter"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/util/taints"
Expand Down Expand Up @@ -86,8 +87,8 @@ type KubeadmConfigReconciler struct {
ClusterCache clustercache.ClusterCache
KubeadmInitLock InitLocker

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilter is used to filter events prior to reconciliation.
WatchFilter watchfilter.WatchFilter

// TokenTTL is the amount of time a bootstrap token (and therefore a KubeadmConfig) will be valid.
TokenTTL time.Duration
Expand Down Expand Up @@ -121,7 +122,7 @@ func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl
Watches(
&clusterv1.Machine{},
handler.EnqueueRequestsFromMapFunc(r.MachineToBootstrapMapFunc),
).WithEventFilter(predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue))
).WithEventFilter(predicates.ResourceHasFilter(mgr.GetScheme(), predicateLog, r.WatchFilter))

if feature.Gates.Enabled(feature.MachinePool) {
b = b.Watches(
Expand All @@ -136,7 +137,7 @@ func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl
builder.WithPredicates(
predicates.All(mgr.GetScheme(), predicateLog,
predicates.ClusterPausedTransitionsOrInfrastructureReady(mgr.GetScheme(), predicateLog),
predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue),
predicates.ResourceHasFilter(mgr.GetScheme(), predicateLog, r.WatchFilter),
),
),
).WatchesRawSource(r.ClusterCache.GetClusterSource("kubeadmconfig", r.ClusterToKubeadmConfigs))
Expand Down
57 changes: 35 additions & 22 deletions bootstrap/kubeadm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"os"
goruntime "runtime"
"strings"
"time"

"github.com/spf13/pflag"
Expand All @@ -49,6 +50,7 @@ import (
"sigs.k8s.io/cluster-api/bootstrap/kubeadm/internal/webhooks"
"sigs.k8s.io/cluster-api/controllers/clustercache"
"sigs.k8s.io/cluster-api/controllers/remote"
watchfilter "sigs.k8s.io/cluster-api/controllers/watchfilter"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/feature"
bootstrapv1alpha3 "sigs.k8s.io/cluster-api/internal/apis/bootstrap/kubeadm/v1alpha3"
Expand All @@ -64,26 +66,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
watchFilterExcludedNamespaces 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 +125,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.StringVar(&watchFilterExcludedNamespaces, "excluded-namespaces", "",
fmt.Sprintf("Comma separated list of names. 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 @@ -313,6 +319,13 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
os.Exit(1)
}

watchFilter := watchfilter.WatchFilter{
Value: watchFilterValue,
}
if watchFilterExcludedNamespaces != "" {
watchFilter.ExcludedNamespaces = strings.Split(watchFilterExcludedNamespaces, ",")
}

clusterCache, err := clustercache.SetupWithManager(ctx, mgr, clustercache.Options{
SecretClient: secretCachingClient,
Cache: clustercache.CacheOptions{},
Expand All @@ -328,7 +341,7 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
},
},
},
WatchFilterValue: watchFilterValue,
WatchFilter: watchFilter,
}, concurrency(clusterCacheConcurrency))
if err != nil {
setupLog.Error(err, "Unable to create ClusterCache")
Expand All @@ -339,7 +352,7 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
Client: mgr.GetClient(),
SecretCachingClient: secretCachingClient,
ClusterCache: clusterCache,
WatchFilterValue: watchFilterValue,
WatchFilter: watchFilter,
TokenTTL: tokenTTL,
}).SetupWithManager(ctx, mgr, concurrency(kubeadmConfigConcurrency)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "KubeadmConfig")
Expand Down
83 changes: 42 additions & 41 deletions controllers/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"

"sigs.k8s.io/cluster-api/controllers/clustercache"
watchfilter "sigs.k8s.io/cluster-api/controllers/watchfilter"
clustercontroller "sigs.k8s.io/cluster-api/internal/controllers/cluster"
clusterclasscontroller "sigs.k8s.io/cluster-api/internal/controllers/clusterclass"
machinecontroller "sigs.k8s.io/cluster-api/internal/controllers/machine"
Expand All @@ -46,8 +47,8 @@ type ClusterReconciler struct {
APIReader client.Reader
ClusterCache clustercache.ClusterCache

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilter is used to filter events prior to reconciliation.
WatchFilter watchfilter.WatchFilter

RemoteConnectionGracePeriod time.Duration
}
Expand All @@ -57,7 +58,7 @@ func (r *ClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manag
Client: r.Client,
APIReader: r.APIReader,
ClusterCache: r.ClusterCache,
WatchFilterValue: r.WatchFilterValue,
WatchFilter: r.WatchFilter,
RemoteConnectionGracePeriod: r.RemoteConnectionGracePeriod,
}).SetupWithManager(ctx, mgr, options)
}
Expand All @@ -68,8 +69,8 @@ type MachineReconciler struct {
APIReader client.Reader
ClusterCache clustercache.ClusterCache

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilter is used to filter events prior to reconciliation.
WatchFilter watchfilter.WatchFilter

RemoteConditionsGracePeriod time.Duration
}
Expand All @@ -79,7 +80,7 @@ func (r *MachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manag
Client: r.Client,
APIReader: r.APIReader,
ClusterCache: r.ClusterCache,
WatchFilterValue: r.WatchFilterValue,
WatchFilter: r.WatchFilter,
RemoteConditionsGracePeriod: r.RemoteConditionsGracePeriod,
}).SetupWithManager(ctx, mgr, options)
}
Expand All @@ -90,8 +91,8 @@ type MachineSetReconciler struct {
APIReader client.Reader
ClusterCache clustercache.ClusterCache

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilter is used to filter events prior to reconciliation.
WatchFilter watchfilter.WatchFilter

// Deprecated: DeprecatedInfraMachineNaming. Name the InfraStructureMachines after the InfraMachineTemplate.
DeprecatedInfraMachineNaming bool
Expand All @@ -102,7 +103,7 @@ func (r *MachineSetReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Ma
Client: r.Client,
APIReader: r.APIReader,
ClusterCache: r.ClusterCache,
WatchFilterValue: r.WatchFilterValue,
WatchFilter: r.WatchFilter,
DeprecatedInfraMachineNaming: r.DeprecatedInfraMachineNaming,
}).SetupWithManager(ctx, mgr, options)
}
Expand All @@ -112,15 +113,15 @@ type MachineDeploymentReconciler struct {
Client client.Client
APIReader client.Reader

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilter is used to filter events prior to reconciliation.
WatchFilter watchfilter.WatchFilter
}

func (r *MachineDeploymentReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
return (&machinedeploymentcontroller.Reconciler{
Client: r.Client,
APIReader: r.APIReader,
WatchFilterValue: r.WatchFilterValue,
Client: r.Client,
APIReader: r.APIReader,
WatchFilter: r.WatchFilter,
}).SetupWithManager(ctx, mgr, options)
}

Expand All @@ -129,15 +130,15 @@ type MachineHealthCheckReconciler struct {
Client client.Client
ClusterCache clustercache.ClusterCache

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilter is used to filter events prior to reconciliation.
WatchFilter watchfilter.WatchFilter
}

func (r *MachineHealthCheckReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
return (&machinehealthcheckcontroller.Reconciler{
Client: r.Client,
ClusterCache: r.ClusterCache,
WatchFilterValue: r.WatchFilterValue,
Client: r.Client,
ClusterCache: r.ClusterCache,
WatchFilter: r.WatchFilter,
}).SetupWithManager(ctx, mgr, options)
}

Expand All @@ -151,17 +152,17 @@ type ClusterTopologyReconciler struct {

RuntimeClient runtimeclient.Client

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilter is used to filter events prior to reconciliation.
WatchFilter watchfilter.WatchFilter
}

func (r *ClusterTopologyReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
return (&clustertopologycontroller.Reconciler{
Client: r.Client,
APIReader: r.APIReader,
ClusterCache: r.ClusterCache,
RuntimeClient: r.RuntimeClient,
WatchFilterValue: r.WatchFilterValue,
Client: r.Client,
APIReader: r.APIReader,
ClusterCache: r.ClusterCache,
RuntimeClient: r.RuntimeClient,
WatchFilter: r.WatchFilter,
}).SetupWithManager(ctx, mgr, options)
}

Expand All @@ -173,15 +174,15 @@ type MachineDeploymentTopologyReconciler struct {
Client client.Client
// APIReader is used to list MachineSets directly via the API server to avoid
// race conditions caused by an outdated cache.
APIReader client.Reader
WatchFilterValue string
APIReader client.Reader
WatchFilter watchfilter.WatchFilter
}

func (r *MachineDeploymentTopologyReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
return (&machinedeploymenttopologycontroller.Reconciler{
Client: r.Client,
APIReader: r.APIReader,
WatchFilterValue: r.WatchFilterValue,
Client: r.Client,
APIReader: r.APIReader,
WatchFilter: r.WatchFilter,
}).SetupWithManager(ctx, mgr, options)
}

Expand All @@ -193,15 +194,15 @@ type MachineSetTopologyReconciler struct {
Client client.Client
// APIReader is used to list MachineSets directly via the API server to avoid
// race conditions caused by an outdated cache.
APIReader client.Reader
WatchFilterValue string
APIReader client.Reader
WatchFilter watchfilter.WatchFilter
}

func (r *MachineSetTopologyReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
return (&machinesettopologycontroller.Reconciler{
Client: r.Client,
APIReader: r.APIReader,
WatchFilterValue: r.WatchFilterValue,
Client: r.Client,
APIReader: r.APIReader,
WatchFilter: r.WatchFilter,
}).SetupWithManager(ctx, mgr, options)
}

Expand All @@ -216,15 +217,15 @@ type ClusterClassReconciler struct {
// RuntimeClient is a client for calling runtime extensions.
RuntimeClient runtimeclient.Client

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilter is used to filter events prior to reconciliation.
WatchFilter watchfilter.WatchFilter
}

func (r *ClusterClassReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
r.internalReconciler = &clusterclasscontroller.Reconciler{
Client: r.Client,
RuntimeClient: r.RuntimeClient,
WatchFilterValue: r.WatchFilterValue,
Client: r.Client,
RuntimeClient: r.RuntimeClient,
WatchFilter: r.WatchFilter,
}
return r.internalReconciler.SetupWithManager(ctx, mgr, options)
}
Expand Down
Loading

0 comments on commit 84ef63f

Please sign in to comment.