Skip to content

Commit

Permalink
Pass label predicate everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil-Grigorev committed Jul 14, 2023
1 parent a65bfa2 commit f133da3
Show file tree
Hide file tree
Showing 22 changed files with 131 additions and 144 deletions.
15 changes: 8 additions & 7 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/remote"
"sigs.k8s.io/cluster-api/util/predicates"
)

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

Tracker *remote.ClusterCacheTracker

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilterPredicate is the label selector value used to filter events prior to reconciliation.
WatchFilterPredicate predicates.LabelMatcher

// TokenTTL is the amount of time a bootstrap token (and therefore a KubeadmConfig) will be valid.
TokenTTL time.Duration
Expand All @@ -53,10 +54,10 @@ type KubeadmConfigReconciler struct {
// SetupWithManager sets up the reconciler with the Manager.
func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
return (&kubeadmbootstrapcontrollers.KubeadmConfigReconciler{
Client: r.Client,
SecretCachingClient: r.SecretCachingClient,
Tracker: r.Tracker,
WatchFilterValue: r.WatchFilterValue,
TokenTTL: r.TokenTTL,
Client: r.Client,
SecretCachingClient: r.SecretCachingClient,
Tracker: r.Tracker,
WatchFilterPredicate: r.WatchFilterPredicate,
TokenTTL: r.TokenTTL,
}).SetupWithManager(ctx, mgr, options)
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ type KubeadmConfigReconciler struct {
Tracker *remote.ClusterCacheTracker
KubeadmInitLock InitLocker

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilterPredicate is the label selector value used to filter events prior to reconciliation.
WatchFilterPredicate predicates.LabelMatcher

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

if feature.Gates.Enabled(feature.MachinePool) {
b = b.Watches(
Expand All @@ -126,7 +126,7 @@ func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl
builder.WithPredicates(
predicates.All(ctrl.LoggerFrom(ctx),
predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx)),
predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue),
predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterPredicate),
),
),
)
Expand Down
67 changes: 34 additions & 33 deletions controllers/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
machinedeploymenttopologycontroller "sigs.k8s.io/cluster-api/internal/controllers/topology/machinedeployment"
machinesettopologycontroller "sigs.k8s.io/cluster-api/internal/controllers/topology/machineset"
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
"sigs.k8s.io/cluster-api/util/predicates"
)

// Following types provides access to reconcilers implemented in internal/controllers, thus
Expand All @@ -46,16 +47,16 @@ type ClusterReconciler struct {
UnstructuredCachingClient client.Client
APIReader client.Reader

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilterPredicate is the label selector value used to filter events prior to reconciliation.
WatchFilterPredicate predicates.LabelMatcher
}

func (r *ClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
return (&clustercontroller.Reconciler{
Client: r.Client,
UnstructuredCachingClient: r.UnstructuredCachingClient,
APIReader: r.APIReader,
WatchFilterValue: r.WatchFilterValue,
WatchFilterPredicate: r.WatchFilterPredicate,
}).SetupWithManager(ctx, mgr, options)
}

Expand All @@ -66,8 +67,8 @@ type MachineReconciler struct {
APIReader client.Reader
Tracker *remote.ClusterCacheTracker

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilterPredicate is the label selector value used to filter events prior to reconciliation.
WatchFilterPredicate predicates.LabelMatcher

// NodeDrainClientTimeout timeout of the client used for draining nodes.
NodeDrainClientTimeout time.Duration
Expand All @@ -79,7 +80,7 @@ func (r *MachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manag
UnstructuredCachingClient: r.UnstructuredCachingClient,
APIReader: r.APIReader,
Tracker: r.Tracker,
WatchFilterValue: r.WatchFilterValue,
WatchFilterPredicate: r.WatchFilterPredicate,
NodeDrainClientTimeout: r.NodeDrainClientTimeout,
}).SetupWithManager(ctx, mgr, options)
}
Expand All @@ -91,8 +92,8 @@ type MachineSetReconciler struct {
APIReader client.Reader
Tracker *remote.ClusterCacheTracker

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilterPredicate is the label selector value used to filter events prior to reconciliation.
WatchFilterPredicate predicates.LabelMatcher
}

func (r *MachineSetReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
Expand All @@ -101,7 +102,7 @@ func (r *MachineSetReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Ma
UnstructuredCachingClient: r.UnstructuredCachingClient,
APIReader: r.APIReader,
Tracker: r.Tracker,
WatchFilterValue: r.WatchFilterValue,
WatchFilterPredicate: r.WatchFilterPredicate,
}).SetupWithManager(ctx, mgr, options)
}

Expand All @@ -111,16 +112,16 @@ type MachineDeploymentReconciler struct {
UnstructuredCachingClient client.Client
APIReader client.Reader

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilterPredicate is the label selector value used to filter events prior to reconciliation.
WatchFilterPredicate predicates.LabelMatcher
}

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

Expand All @@ -129,15 +130,15 @@ type MachineHealthCheckReconciler struct {
Client client.Client
Tracker *remote.ClusterCacheTracker

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilterPredicate is the label selector value used to filter events prior to reconciliation.
WatchFilterPredicate predicates.LabelMatcher
}

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

Expand All @@ -150,8 +151,8 @@ type ClusterTopologyReconciler struct {

RuntimeClient runtimeclient.Client

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilterPredicate is the label selector value used to filter events prior to reconciliation.
WatchFilterPredicate predicates.LabelMatcher

// UnstructuredCachingClient provides a client that forces caching of unstructured objects,
// thus allowing to optimize reads for templates or provider specific objects in a managed topology.
Expand All @@ -164,7 +165,7 @@ func (r *ClusterTopologyReconciler) SetupWithManager(ctx context.Context, mgr ct
APIReader: r.APIReader,
RuntimeClient: r.RuntimeClient,
UnstructuredCachingClient: r.UnstructuredCachingClient,
WatchFilterValue: r.WatchFilterValue,
WatchFilterPredicate: r.WatchFilterPredicate,
}).SetupWithManager(ctx, mgr, options)
}

Expand All @@ -176,15 +177,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
WatchFilterPredicate predicates.LabelMatcher
}

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,
WatchFilterPredicate: r.WatchFilterPredicate,
}).SetupWithManager(ctx, mgr, options)
}

Expand All @@ -196,15 +197,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
WatchFilterPredicate predicates.LabelMatcher
}

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,
WatchFilterPredicate: r.WatchFilterPredicate,
}).SetupWithManager(ctx, mgr, options)
}

Expand All @@ -216,8 +217,8 @@ 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
// WatchFilterPredicate is the label selector value used to filter events prior to reconciliation.
WatchFilterPredicate predicates.LabelMatcher

// UnstructuredCachingClient provides a client that forces caching of unstructured objects,
// thus allowing to optimize reads for templates or provider specific objects.
Expand All @@ -230,6 +231,6 @@ func (r *ClusterClassReconciler) SetupWithManager(ctx context.Context, mgr ctrl.
APIReader: r.APIReader,
RuntimeClient: r.RuntimeClient,
UnstructuredCachingClient: r.UnstructuredCachingClient,
WatchFilterValue: r.WatchFilterValue,
WatchFilterPredicate: r.WatchFilterPredicate,
}).SetupWithManager(ctx, mgr, options)
}
7 changes: 3 additions & 4 deletions controllers/remote/cluster_cache_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,16 @@ type ClusterCacheReconciler struct {
Client client.Client
Tracker *ClusterCacheTracker

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilterPredicate is the label selector value used to filter events prior to reconciliation.
WatchFilterPredicate predicates.LabelMatcher
}

func (r *ClusterCacheReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
err := ctrl.NewControllerManagedBy(mgr).
Named("remote/clustercache").
For(&clusterv1.Cluster{}).
WithOptions(options).
WithEventFilter(predicates.GetExpressionMatcher().Matches(ctrl.LoggerFrom(ctx))).
WithEventFilter(predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
WithEventFilter(predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterPredicate)).
Complete(r)

if err != nil {
Expand Down
17 changes: 9 additions & 8 deletions controlplane/kubeadm/controllers/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"sigs.k8s.io/cluster-api/controllers/remote"
kubeadmcontrolplanecontrollers "sigs.k8s.io/cluster-api/controlplane/kubeadm/internal/controllers"
"sigs.k8s.io/cluster-api/util/predicates"
)

// KubeadmControlPlaneReconciler reconciles a KubeadmControlPlane object.
Expand All @@ -37,18 +38,18 @@ type KubeadmControlPlaneReconciler struct {
EtcdDialTimeout time.Duration
EtcdCallTimeout time.Duration

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilterPredicate is the label selector value used to filter events prior to reconciliation.
WatchFilterPredicate predicates.LabelMatcher
}

// SetupWithManager sets up the reconciler with the Manager.
func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
return (&kubeadmcontrolplanecontrollers.KubeadmControlPlaneReconciler{
Client: r.Client,
SecretCachingClient: r.SecretCachingClient,
Tracker: r.Tracker,
EtcdDialTimeout: r.EtcdDialTimeout,
EtcdCallTimeout: r.EtcdCallTimeout,
WatchFilterValue: r.WatchFilterValue,
Client: r.Client,
SecretCachingClient: r.SecretCachingClient,
Tracker: r.Tracker,
EtcdDialTimeout: r.EtcdDialTimeout,
EtcdCallTimeout: r.EtcdCallTimeout,
WatchFilterPredicate: r.WatchFilterPredicate,
}).SetupWithManager(ctx, mgr, options)
}
9 changes: 4 additions & 5 deletions controlplane/kubeadm/internal/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ type KubeadmControlPlaneReconciler struct {
EtcdDialTimeout time.Duration
EtcdCallTimeout time.Duration

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilterPredicate is the label selector value used to filter events prior to reconciliation.
WatchFilterPredicate predicates.LabelMatcher

managementCluster internal.ManagementCluster
managementClusterUncached internal.ManagementCluster
Expand All @@ -99,15 +99,14 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
For(&controlplanev1.KubeadmControlPlane{}).
Owns(&clusterv1.Machine{}).
WithOptions(options).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterPredicate)).
Watches(
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(r.ClusterToKubeadmControlPlane),
builder.WithPredicates(
predicates.All(ctrl.LoggerFrom(ctx),
predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue),
predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterPredicate),
predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx)),
predicates.GetExpressionMatcher().Matches(ctrl.LoggerFrom(ctx)),
),
),
).Build(r)
Expand Down
19 changes: 10 additions & 9 deletions exp/addons/controllers/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,37 @@ import (

"sigs.k8s.io/cluster-api/controllers/remote"
clusterresourcesets "sigs.k8s.io/cluster-api/exp/addons/internal/controllers"
"sigs.k8s.io/cluster-api/util/predicates"
)

// ClusterResourceSetReconciler reconciles a ClusterResourceSet object.
type ClusterResourceSetReconciler struct {
Client client.Client
Tracker *remote.ClusterCacheTracker

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilterPredicate is the label selector value used to filter events prior to reconciliation.
WatchFilterPredicate predicates.LabelMatcher
}

func (r *ClusterResourceSetReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
return (&clusterresourcesets.ClusterResourceSetReconciler{
Client: r.Client,
Tracker: r.Tracker,
WatchFilterValue: r.WatchFilterValue,
Client: r.Client,
Tracker: r.Tracker,
WatchFilterPredicate: r.WatchFilterPredicate,
}).SetupWithManager(ctx, mgr, options)
}

// ClusterResourceSetBindingReconciler reconciles a ClusterResourceSetBinding object.
type ClusterResourceSetBindingReconciler struct {
Client client.Client

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
// WatchFilterPredicate is the label selector value used to filter events prior to reconciliation.
WatchFilterPredicate predicates.LabelMatcher
}

func (r *ClusterResourceSetBindingReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
return (&clusterresourcesets.ClusterResourceSetBindingReconciler{
Client: r.Client,
WatchFilterValue: r.WatchFilterValue,
Client: r.Client,
WatchFilterPredicate: r.WatchFilterPredicate,
}).SetupWithManager(ctx, mgr, options)
}
Loading

0 comments on commit f133da3

Please sign in to comment.