From 048cd1bcb1d41c17267e497e53114c1b26d8433f Mon Sep 17 00:00:00 2001 From: "artur.zheludkov" Date: Thu, 23 Mar 2023 16:02:01 -0400 Subject: [PATCH 1/2] redis operator under its namespace --- operator/redisfailover/factory.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/operator/redisfailover/factory.go b/operator/redisfailover/factory.go index 2ebc67eb7..807a9b983 100644 --- a/operator/redisfailover/factory.go +++ b/operator/redisfailover/factory.go @@ -36,7 +36,7 @@ func New(cfg Config, k8sService k8s.Services, k8sClient kubernetes.Interface, lo // Create the handlers. rfHandler := NewRedisFailoverHandler(cfg, rfService, rfChecker, rfHealer, k8sService, kooperMetricsRecorder, logger) - rfRetriever := NewRedisFailoverRetriever(k8sService) + rfRetriever := NewRedisFailoverRetriever(k8sService, lockNamespace) kooperLogger := kooperlogger{Logger: logger.WithField("operator", "redisfailover")} // Leader election service. @@ -58,13 +58,13 @@ func New(cfg Config, k8sService k8s.Services, k8sClient kubernetes.Interface, lo }) } -func NewRedisFailoverRetriever(cli k8s.Services) controller.Retriever { +func NewRedisFailoverRetriever(cli k8s.Services, lockNamespace string) controller.Retriever { return controller.MustRetrieverFromListerWatcher(&cache.ListWatch{ ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { - return cli.ListRedisFailovers(context.Background(), "", options) + return cli.ListRedisFailovers(context.Background(), lockNamespace, options) }, WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { - return cli.WatchRedisFailovers(context.Background(), "", options) + return cli.WatchRedisFailovers(context.Background(), lockNamespace, options) }, }) } From 518d274e117661aef24235ac1a463036f9fa4f42 Mon Sep 17 00:00:00 2001 From: "artur.zheludkov" Date: Thu, 6 Apr 2023 20:58:33 -0400 Subject: [PATCH 2/2] Allow redis operator to be namespace scoped. --- README.md | 14 ++++++++++++++ operator/redisfailover/factory.go | 12 +++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f27d99e01..7db7c446b 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,20 @@ By default, no service annotations will be applied to the Redis nor Sentinel ser In order to apply custom service Annotations, you can provide the `serviceAnnotations` option inside redis/sentinel spec. An example can be found in the [custom annotations example file](example/redisfailover/custom-annotations.yaml). +### Allow Operator to Be Namespace-Scoped +By default operator is cluster wide. + +In order to make operator namespace-scoped you can provide the env variable `WATCH_NAMESPACE` to the redisoperator deployment manifest. + +``` +env: + - name: WATCH_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace +``` + + ### Control of label propagation. By default the operator will propagate all labels on the CRD down to the resources that it creates. This can be problematic if the labels on the CRD are not fully under your own control (for example: being deployed by a gitops operator) diff --git a/operator/redisfailover/factory.go b/operator/redisfailover/factory.go index 807a9b983..add10eb5a 100644 --- a/operator/redisfailover/factory.go +++ b/operator/redisfailover/factory.go @@ -3,6 +3,7 @@ package redisfailover import ( "context" "time" + "os" "github.com/spotahome/kooper/v2/controller" "github.com/spotahome/kooper/v2/controller/leaderelection" @@ -33,10 +34,11 @@ func New(cfg Config, k8sService k8s.Services, k8sClient kubernetes.Interface, lo rfService := rfservice.NewRedisFailoverKubeClient(k8sService, logger, kooperMetricsRecorder) rfChecker := rfservice.NewRedisFailoverChecker(k8sService, redisClient, logger, kooperMetricsRecorder) rfHealer := rfservice.NewRedisFailoverHealer(k8sService, redisClient, logger) - + watchNamespace := os.Getenv("WATCH_NAMESPACE") + // Create the handlers. rfHandler := NewRedisFailoverHandler(cfg, rfService, rfChecker, rfHealer, k8sService, kooperMetricsRecorder, logger) - rfRetriever := NewRedisFailoverRetriever(k8sService, lockNamespace) + rfRetriever := NewRedisFailoverRetriever(k8sService, watchNamespace) kooperLogger := kooperlogger{Logger: logger.WithField("operator", "redisfailover")} // Leader election service. @@ -58,13 +60,13 @@ func New(cfg Config, k8sService k8s.Services, k8sClient kubernetes.Interface, lo }) } -func NewRedisFailoverRetriever(cli k8s.Services, lockNamespace string) controller.Retriever { +func NewRedisFailoverRetriever(cli k8s.Services, watchNamespace string) controller.Retriever { return controller.MustRetrieverFromListerWatcher(&cache.ListWatch{ ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { - return cli.ListRedisFailovers(context.Background(), lockNamespace, options) + return cli.ListRedisFailovers(context.Background(), watchNamespace, options) }, WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { - return cli.WatchRedisFailovers(context.Background(), lockNamespace, options) + return cli.WatchRedisFailovers(context.Background(), watchNamespace, options) }, }) }