Skip to content

Commit

Permalink
feat(cluster-api): implement watch-filter arg
Browse files Browse the repository at this point in the history
<!-- If this is your first PR, welcome! Please make sure you read the [contributing guidelines](../CONTRIBUTING.md#contributing-a-patch). -->

**What type of PR is this?**

/kind feature           implement watch-filter cluster-api arg

**What this PR does / why we need it**:

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #330

**Special notes for your reviewer**:

_Please confirm that if this PR changes any image versions, then that's the sole change this PR makes._

**TODOs**:
<!-- Put an "X" character inside the brackets of each completed task. Some may be optional depending on the PR. -->

- [ ] squashed commits
- [ ] includes documentation
- [ ] adds unit tests
  • Loading branch information
sebglon committed Apr 24, 2024
1 parent 5e10d24 commit 9541a0e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
5 changes: 4 additions & 1 deletion controllers/osccluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand All @@ -49,6 +50,7 @@ type OscClusterReconciler struct {
client.Client
Recorder record.EventRecorder
ReconcileTimeout time.Duration
WatchFilterValue string
}

//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=oscclusters,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -500,8 +502,9 @@ func (r *OscClusterReconciler) reconcileDelete(ctx context.Context, clusterScope
}

// SetupWithManager sets up the controller with the Manager.
func (r *OscClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *OscClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&infrastructurev1beta1.OscCluster{}).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Complete(r)
}
2 changes: 2 additions & 0 deletions controllers/oscmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type OscMachineReconciler struct {
client.Client
Recorder record.EventRecorder
ReconcileTimeout time.Duration
WatchFilterValue string
}

//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=oscmachines,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -393,6 +394,7 @@ func (r *OscMachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Ma
return errors.Errorf("failed to create mapper for Cluster to OscMachines: %+v", err)
}
err = ctrl.NewControllerManagedBy(mgr).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
For(&infrastructurev1beta1.OscMachine{}).
Watches(
&source.Kind{Type: &clusterv1.Machine{}},
Expand Down
3 changes: 3 additions & 0 deletions controllers/oscmachinetemplate_capacity_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/record"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand All @@ -43,6 +44,7 @@ type OscMachineTemplateReconciler struct {
client.Client
Recorder record.EventRecorder
ReconcileTimeout time.Duration
WatchFilterValue string
}

// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=oscmachinetemplates,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -147,5 +149,6 @@ func (r *OscMachineTemplateReconciler) SetupWithManager(ctx context.Context, mgr
return ctrl.NewControllerManagedBy(mgr).
WithOptions(options).
For(&infrastructurev1beta1.OscMachineTemplate{}).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Complete(r)
}
13 changes: 10 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"flag"
"fmt"
"os"
"time"

Expand Down Expand Up @@ -59,11 +60,14 @@ func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
var watchFilterValue string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", true,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.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))

opts := zap.Options{}
opts.BindFlags(flag.CommandLine)
flag.Parse()
Expand All @@ -88,21 +92,23 @@ func main() {
os.Exit(1)
}

ctx := ctrl.SetupSignalHandler()

if err = (&controllers.OscClusterReconciler{
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor("osccluster-controller"),
ReconcileTimeout: reconcileTimeout,
}).SetupWithManager(mgr); err != nil {
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OscCluster")
os.Exit(1)
}

ctx := ctrl.SetupSignalHandler()

if err = (&controllers.OscMachineReconciler{
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor("oscmachine-controller"),
ReconcileTimeout: reconcileTimeout,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OscMachine")
os.Exit(1)
Expand All @@ -112,6 +118,7 @@ func main() {
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor("oscmachinetemplate-controller"),
ReconcileTimeout: reconcileTimeout,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OscMachineTemplate")
os.Exit(1)
Expand Down

0 comments on commit 9541a0e

Please sign in to comment.