diff --git a/controllers/osccluster_controller.go b/controllers/osccluster_controller.go index a02f49248..562b8b5a5 100644 --- a/controllers/osccluster_controller.go +++ b/controllers/osccluster_controller.go @@ -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" @@ -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 @@ -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) } diff --git a/controllers/oscmachine_controller.go b/controllers/oscmachine_controller.go index 24fd0a723..eaaac0b29 100644 --- a/controllers/oscmachine_controller.go +++ b/controllers/oscmachine_controller.go @@ -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 @@ -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{}}, diff --git a/controllers/oscmachinetemplate_capacity_controller.go b/controllers/oscmachinetemplate_capacity_controller.go index 7fbc6d22d..5ac2fdcb7 100644 --- a/controllers/oscmachinetemplate_capacity_controller.go +++ b/controllers/oscmachinetemplate_capacity_controller.go @@ -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" @@ -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 @@ -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) } diff --git a/example/cluster-machine-template-simple.yaml b/example/cluster-machine-template-simple.yaml index d853b7a13..c5a212f53 100644 --- a/example/cluster-machine-template-simple.yaml +++ b/example/cluster-machine-template-simple.yaml @@ -6,6 +6,7 @@ metadata: labels: cni: "cluster-api-crs-cni" ccm: "cluster-api-crs-ccm" + cluster.x-k8s.io/watch-filter: "dev" spec: clusterNetwork: pods: @@ -26,6 +27,8 @@ kind: OscCluster metadata: name: cluster-api namespace: default + labels: + cluster.x-k8s.io/watch-filter: "dev" spec: network: extraSecurityGroupRule: true @@ -237,6 +240,8 @@ kind: MachineDeployment metadata: name: "cluster-api-md-0" namespace: default + labels: + cluster.x-k8s.io/watch-filter: "dev" spec: clusterName: "cluster-api" replicas: 1 @@ -263,6 +268,8 @@ kind: OscMachineTemplate metadata: name: "cluster-api-md-0" namespace: default + labels: + cluster.x-k8s.io/watch-filter: "dev" spec: template: spec: @@ -296,6 +303,8 @@ kind: OscMachineTemplate metadata: name: "cluster-api-control-plane" namespace: default + labels: + cluster.x-k8s.io/watch-filter: "dev" spec: template: spec: @@ -329,6 +338,8 @@ kind: KubeadmConfigTemplate metadata: name: "cluster-api-md-0" namespace: default + labels: + cluster.x-k8s.io/watch-filter: "dev" spec: template: spec: @@ -355,6 +366,8 @@ kind: KubeadmControlPlane apiVersion: controlplane.cluster.x-k8s.io/v1beta1 metadata: name: "cluster-api-control-plane" + labels: + cluster.x-k8s.io/watch-filter: "dev" spec: replicas: 1 machineTemplate: diff --git a/helm/clusterapioutscale/Chart.yaml b/helm/clusterapioutscale/Chart.yaml index cce86d95f..4c1adfc25 100644 --- a/helm/clusterapioutscale/Chart.yaml +++ b/helm/clusterapioutscale/Chart.yaml @@ -10,4 +10,4 @@ version: 0.1.1 # incremented each time you make changes to the application. Versions are not expected to # follow Semantic Versioning. They should reflect the version the application is using. # It is recommended to use it with quotes. -appVersion: "1.16.0" +appVersion: "1.17.0" diff --git a/helm/clusterapioutscale/templates/deployment.yaml b/helm/clusterapioutscale/templates/deployment.yaml index 4fc9c1840..064405216 100644 --- a/helm/clusterapioutscale/templates/deployment.yaml +++ b/helm/clusterapioutscale/templates/deployment.yaml @@ -40,6 +40,9 @@ spec: - --upstream=http://127.0.0.1:8080/ - --logtostderr=true - --v={{ .verbosity }} + {{- if .watch_filter }} + - --watch-filter {{ .watch_filter }} + {{- end}} image: {{ .containers.imageProxy }}:{{ .containers.imageProxyTag }} name: kube-rbac-proxy ports: @@ -80,7 +83,7 @@ spec: secretKeyRef: key: region name: cluster-api-provider-outscale - optional: true + optional: true - name: BACKOFF_DURATION value: "{{ .backoff_duration }}" - name: BACKOFF_FACTOR diff --git a/helm/clusterapioutscale/values.yaml b/helm/clusterapioutscale/values.yaml index 41b3825ab..09a86d6a1 100644 --- a/helm/clusterapioutscale/values.yaml +++ b/helm/clusterapioutscale/values.yaml @@ -18,6 +18,7 @@ clusterapioutscale: # integer defining the remaining number of iterations in which the duration parameter may change # -- Remaining number of iterations in which the duration parameter may change backoff_steps: "20" + watch_filter: "" # -- Additional annotions to use pods: annotations: diff --git a/main.go b/main.go index e0197a920..7e3918ddc 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ package main import ( "flag" + "fmt" "os" "time" @@ -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() @@ -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) @@ -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) diff --git a/testclean/suite_test.go b/testclean/suite_test.go index 38b378c5e..e4a555bc5 100644 --- a/testclean/suite_test.go +++ b/testclean/suite_test.go @@ -18,16 +18,22 @@ package test import ( "flag" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/outscale-dev/cluster-api-provider-outscale.git/controllers" + "golang.org/x/net/context" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" "k8s.io/klog/v2" "k8s.io/klog/v2/klogr" + // "os" - utilruntime "k8s.io/apimachinery/pkg/util/runtime" "path/filepath" + "testing" + "time" + + utilruntime "k8s.io/apimachinery/pkg/util/runtime" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" "sigs.k8s.io/cluster-api/test/framework/ginkgoextensions" @@ -36,8 +42,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/envtest" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" - "testing" - "time" infrastructurev1beta1 "github.com/outscale-dev/cluster-api-provider-outscale.git/api/v1beta1" //+kubebuilder:scaffold:imports @@ -107,7 +111,7 @@ var _ = BeforeSuite(func() { Client: k8sManager.GetClient(), Recorder: k8sManager.GetEventRecorderFor("osc-controller"), ReconcileTimeout: reconcileTimeout, - }).SetupWithManager(k8sManager) + }).SetupWithManager(context.Background(), k8sManager) go func() { defer GinkgoRecover() diff --git a/testenv/suite_test.go b/testenv/suite_test.go index 4c6acbeb6..fc419d0f2 100644 --- a/testenv/suite_test.go +++ b/testenv/suite_test.go @@ -17,15 +17,19 @@ limitations under the License. package test import ( + "path/filepath" + "testing" + "time" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/outscale-dev/cluster-api-provider-outscale.git/controllers" + "golang.org/x/net/context" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" "k8s.io/klog/v2" "k8s.io/klog/v2/klogr" - "path/filepath" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" "sigs.k8s.io/cluster-api/test/framework/ginkgoextensions" ctrl "sigs.k8s.io/controller-runtime" @@ -33,8 +37,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/envtest" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" - "testing" - "time" infrastructurev1beta1 "github.com/outscale-dev/cluster-api-provider-outscale.git/api/v1beta1" //+kubebuilder:scaffold:imports @@ -106,7 +108,7 @@ var _ = BeforeSuite(func() { Client: k8sManager.GetClient(), Recorder: k8sManager.GetEventRecorderFor("osc-controller"), ReconcileTimeout: reconcileTimeout, - }).SetupWithManager(k8sManager) + }).SetupWithManager(context.Background(), k8sManager) go func() { defer GinkgoRecover()