Skip to content

⚠️ Add watch label to allow multiple manager instances #4119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/v1alpha4/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ const (
// on the reconciled object.
PausedAnnotation = "cluster.x-k8s.io/paused"

// WatchLabel is a label othat 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.
WatchLabel = "cluster.x-k8s.io/watch-filter"

// DeleteMachineAnnotation marks control plane and worker nodes that will be given priority for deletion
// when KCP or a machineset scales down. This annotation is given top priority on all delete policies.
DeleteMachineAnnotation = "cluster.x-k8s.io/delete-machine"
Expand Down
5 changes: 3 additions & 2 deletions controllers/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ const (

// ClusterReconciler reconciles a Cluster object
type ClusterReconciler struct {
Client client.Client
Client client.Client
WatchFilterValue string

restConfig *rest.Config
recorder record.EventRecorder
Expand All @@ -78,7 +79,7 @@ func (r *ClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manag
handler.EnqueueRequestsFromMapFunc(r.controlPlaneMachineToCluster),
).
WithOptions(options).
WithEventFilter(predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx))).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Build(r)

if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions controllers/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ var (

// MachineReconciler reconciles a Machine object
type MachineReconciler struct {
Client client.Client
Tracker *remote.ClusterCacheTracker
Client client.Client
Tracker *remote.ClusterCacheTracker
WatchFilterValue string

controller controller.Controller
restConfig *rest.Config
Expand All @@ -91,7 +92,7 @@ func (r *MachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manag
controller, err := ctrl.NewControllerManagedBy(mgr).
For(&clusterv1.Machine{}).
WithOptions(options).
WithEventFilter(predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx))).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Build(r)
if err != nil {
return errors.Wrap(err, "failed setting up with a controller manager")
Expand Down
5 changes: 3 additions & 2 deletions controllers/machinedeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ var (

// MachineDeploymentReconciler reconciles a MachineDeployment object
type MachineDeploymentReconciler struct {
Client client.Client
Client client.Client
WatchFilterValue string

recorder record.EventRecorder
restConfig *rest.Config
Expand All @@ -73,7 +74,7 @@ func (r *MachineDeploymentReconciler) SetupWithManager(ctx context.Context, mgr
handler.EnqueueRequestsFromMapFunc(r.MachineSetToDeployments),
).
WithOptions(options).
WithEventFilter(predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx))).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Build(r)
if err != nil {
return errors.Wrap(err, "failed setting up with a controller manager")
Expand Down
7 changes: 4 additions & 3 deletions controllers/machinehealthcheck_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ const (

// MachineHealthCheckReconciler reconciles a MachineHealthCheck object
type MachineHealthCheckReconciler struct {
Client client.Client
Tracker *remote.ClusterCacheTracker
Client client.Client
Tracker *remote.ClusterCacheTracker
WatchFilterValue string

controller controller.Controller
recorder record.EventRecorder
Expand All @@ -80,7 +81,7 @@ func (r *MachineHealthCheckReconciler) SetupWithManager(ctx context.Context, mgr
handler.EnqueueRequestsFromMapFunc(r.machineToMachineHealthCheck),
).
WithOptions(options).
WithEventFilter(predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx))).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Build(r)
if err != nil {
return errors.Wrap(err, "failed setting up with a controller manager")
Expand Down
7 changes: 4 additions & 3 deletions controllers/machineset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ var (

// MachineSetReconciler reconciles a MachineSet object
type MachineSetReconciler struct {
Client client.Client
Tracker *remote.ClusterCacheTracker
Client client.Client
Tracker *remote.ClusterCacheTracker
WatchFilterValue string

recorder record.EventRecorder
restConfig *rest.Config
Expand All @@ -88,7 +89,7 @@ func (r *MachineSetReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Ma
handler.EnqueueRequestsFromMapFunc(r.MachineToMachineSets),
).
WithOptions(options).
WithEventFilter(predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx))).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Build(r)
if err != nil {
return errors.Wrap(err, "failed setting up with a controller manager")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ in case the above limitations/extra complexity are acceptable for them.
In order to make it possible for users to deploy multiple instances of the same provider:

- Providers MUST support the `--namespace` flag in their controllers.
- Providers MUST support the `--watch-filter` flag in their controllers.

⚠️ Users selecting this deployment model, please be aware:

Expand Down
26 changes: 25 additions & 1 deletion docs/book/src/developer/providers/v1alpha3-to-v1alpha4.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Provider's `/config` folder has the same structure of `/config` folder in CAPI
- ../manager
```
- Remove the `patchesStrategicMerge` list
- Copy the `vars` list into a temporary file to be used later in the process
- Copy the `vars` list into a temporary file to be used later in the process
- Remove the `vars` list
1. Edit the `config/webhook/kustomizeconfig.yaml` file:
- In the `varReference:` list, remove the item with `kind: Deployment`
Expand Down Expand Up @@ -181,3 +181,27 @@ with `cert-manager.io/v1`
group: cert-manager.io
version: v1
```
## Support the cluster.x-k8s.io/watch-filter label and watch-filter flag.

- A new label `cluster.x-k8s.io/watch-filter` provides the ability to filter the controllers to only reconcile objects with a specific label.
- A new flag `watch-filter` enables users to specify the label value for the `cluster.x-k8s.io/watch-filter` label on controller boot.
- The flag which enables users to set the flag value can be structured like this:
```go
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))
```
- The `ResourceNotPausedAndHasFilterLabel` predicate is a useful helper to check for the pause annotation and the filter label easily:
```go
c, err := ctrl.NewControllerManagedBy(mgr).
For(&clusterv1.MachineSet{}).
Owns(&clusterv1.Machine{}).
Watches(
&source.Kind{Type: &clusterv1.Machine{}},
handler.EnqueueRequestsFromMapFunc(r.MachineToMachineSets),
).
WithOptions(options).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Build(r)
if err != nil {
return errors.Wrap(err, "failed setting up with a controller manager")
}
```
7 changes: 4 additions & 3 deletions exp/addons/controllers/clusterresourceset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ var (

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

func (r *ClusterResourceSetReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
Expand All @@ -88,7 +89,7 @@ func (r *ClusterResourceSetReconciler) SetupWithManager(ctx context.Context, mgr
),
).
WithOptions(options).
WithEventFilter(predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx))).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Complete(r)

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import (

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

func (r *ClusterResourceSetBindingReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
Expand All @@ -48,7 +49,7 @@ func (r *ClusterResourceSetBindingReconciler) SetupWithManager(ctx context.Conte
handler.EnqueueRequestsFromMapFunc(r.clusterToClusterResourceSetBinding),
).
WithOptions(options).
WithEventFilter(predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx))).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Build(r)
if err != nil {
return errors.Wrap(err, "failed setting up with a controller manager")
Expand Down
5 changes: 3 additions & 2 deletions exp/controllers/machinepool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ const (

// MachinePoolReconciler reconciles a MachinePool object
type MachinePoolReconciler struct {
Client client.Client
Client client.Client
WatchFilterValue string

config *rest.Config
controller controller.Controller
Expand All @@ -75,7 +76,7 @@ func (r *MachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.M
c, err := ctrl.NewControllerManagedBy(mgr).
For(&expv1.MachinePool{}).
WithOptions(options).
WithEventFilter(predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx))).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Build(r)
if err != nil {
return errors.Wrap(err, "failed setting up with a controller manager")
Expand Down
37 changes: 25 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main
import (
"context"
"flag"
"fmt"
"math/rand"
"net/http"
_ "net/http/pprof"
Expand Down Expand Up @@ -58,6 +59,7 @@ var (
leaderElectionRenewDeadline time.Duration
leaderElectionRetryPeriod time.Duration
watchNamespace string
watchFilterValue string
profilerAddress string
clusterConcurrency int
machineConcurrency int
Expand Down Expand Up @@ -102,6 +104,9 @@ func InitFlags(fs *pflag.FlagSet) {
fs.StringVar(&watchNamespace, "namespace", "",
"Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces.")

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(&profilerAddress, "profiler-address", "",
"Bind address to expose the pprof profiler (e.g. localhost:6060)")

Expand Down Expand Up @@ -224,35 +229,40 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
}

if err := (&controllers.ClusterReconciler{
Client: mgr.GetClient(),
Client: mgr.GetClient(),
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, concurrency(clusterConcurrency)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Cluster")
os.Exit(1)
}
if err := (&controllers.MachineReconciler{
Client: mgr.GetClient(),
Tracker: tracker,
Client: mgr.GetClient(),
Tracker: tracker,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, concurrency(machineConcurrency)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Machine")
os.Exit(1)
}
if err := (&controllers.MachineSetReconciler{
Client: mgr.GetClient(),
Tracker: tracker,
Client: mgr.GetClient(),
Tracker: tracker,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, concurrency(machineSetConcurrency)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MachineSet")
os.Exit(1)
}
if err := (&controllers.MachineDeploymentReconciler{
Client: mgr.GetClient(),
Client: mgr.GetClient(),
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, concurrency(machineDeploymentConcurrency)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MachineDeployment")
os.Exit(1)
}

if feature.Gates.Enabled(feature.MachinePool) {
if err := (&expcontrollers.MachinePoolReconciler{
Client: mgr.GetClient(),
Client: mgr.GetClient(),
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, concurrency(machinePoolConcurrency)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MachinePool")
os.Exit(1)
Expand All @@ -261,23 +271,26 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {

if feature.Gates.Enabled(feature.ClusterResourceSet) {
if err := (&addonscontrollers.ClusterResourceSetReconciler{
Client: mgr.GetClient(),
Tracker: tracker,
Client: mgr.GetClient(),
Tracker: tracker,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, concurrency(clusterResourceSetConcurrency)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ClusterResourceSet")
os.Exit(1)
}
if err := (&addonscontrollers.ClusterResourceSetBindingReconciler{
Client: mgr.GetClient(),
Client: mgr.GetClient(),
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, concurrency(clusterResourceSetConcurrency)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ClusterResourceSetBinding")
os.Exit(1)
}
}

if err := (&controllers.MachineHealthCheckReconciler{
Client: mgr.GetClient(),
Tracker: tracker,
Client: mgr.GetClient(),
Tracker: tracker,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, concurrency(machineHealthCheckConcurrency)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MachineHealthCheck")
os.Exit(1)
Expand Down
31 changes: 31 additions & 0 deletions util/labels/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2021 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package labels

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
)

// HasWatchLabel returns true if the object has a label with the WatchLabel key matching the given value.
func HasWatchLabel(o metav1.Object, labelValue string) bool {
val, ok := o.GetLabels()[clusterv1.WatchLabel]
if !ok {
return false
}
return val == labelValue
}
Loading