From 7592a8e2e824891b953d2777373c3bbbef9b42e2 Mon Sep 17 00:00:00 2001 From: Maxime Leroy <19607336+maxime1907@users.noreply.github.com> Date: Mon, 26 Sep 2022 18:39:35 +0200 Subject: [PATCH] feat: support watching specific namespaces --- .gitignore | 4 +-- CHANGELOG.md | 1 + .../monitoring.giantswarm.io_silences.yaml | 2 +- flag/service/service.go | 5 ++-- .../silence-operator/templates/configmap.yaml | 2 ++ main.go | 1 + service/controller/silence.go | 8 +++++- service/service.go | 25 +++++++++++-------- 8 files changed, 31 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index f4c9cbcf..eb345ecc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,5 @@ *.swp TODO /silence-operator -/silence-operator-v*-*-linux-* -/silence-operator-v*-*-darwin-* -/silence-operator-v*-*-amd64-* +/silence-operator-v* !vendor/** diff --git a/CHANGELOG.md b/CHANGELOG.md index ce54e85c..be89dbac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Bump github.com/giantswarm/k8smetadata from 0.11.1 to 0.12.0 - Bump sigs.k8s.io/controller-runtime from 0.12.3 to 0.13.0 - Reconcile API if Silence CR gets updated +- Support watching specific namespaces ## [0.7.0] - 2022-06-13 diff --git a/config/crd/monitoring.giantswarm.io_silences.yaml b/config/crd/monitoring.giantswarm.io_silences.yaml index d78b8707..d1396713 100644 --- a/config/crd/monitoring.giantswarm.io_silences.yaml +++ b/config/crd/monitoring.giantswarm.io_silences.yaml @@ -17,7 +17,7 @@ spec: listKind: SilenceList plural: silences singular: silence - scope: Cluster + scope: Namespaced versions: - name: v1alpha1 schema: diff --git a/flag/service/service.go b/flag/service/service.go index 17fbd00f..97eab01b 100644 --- a/flag/service/service.go +++ b/flag/service/service.go @@ -8,6 +8,7 @@ import ( // Service is an intermediate data structure for command line configuration flags. type Service struct { - AlertManager alertmanager.AlertManager - Kubernetes kubernetes.Kubernetes + AlertManager alertmanager.AlertManager + Kubernetes kubernetes.Kubernetes + WatchNamespaces string } diff --git a/helm/silence-operator/templates/configmap.yaml b/helm/silence-operator/templates/configmap.yaml index c6873256..699d2a18 100644 --- a/helm/silence-operator/templates/configmap.yaml +++ b/helm/silence-operator/templates/configmap.yaml @@ -14,6 +14,8 @@ data: listen: address: 'http://0.0.0.0:8000' service: + watchNamespaces: + - "" alertmanager: address: http://alertmanager-operated.monitoring:9093 kubernetes: diff --git a/main.go b/main.go index 6506aadb..e212e32a 100644 --- a/main.go +++ b/main.go @@ -129,6 +129,7 @@ func mainE(ctx context.Context) error { daemonCommand.PersistentFlags().String(f.Service.Kubernetes.TLS.KeyFile, "", "Key file path to use to authenticate with Kubernetes.") daemonCommand.PersistentFlags().String(f.Service.AlertManager.Address, "http://localhost:9093", "Alertmanager address used to create silences.") + daemonCommand.PersistentFlags().StringSlice(f.Service.WatchNamespaces, []string{""}, "Service namespaces that should be watched.") err = newCommand.CobraCommand().Execute() if err != nil { diff --git a/service/controller/silence.go b/service/controller/silence.go index bbd99298..402525c2 100644 --- a/service/controller/silence.go +++ b/service/controller/silence.go @@ -22,6 +22,7 @@ type SilenceConfig struct { Logger micrologger.Logger AlertManagerAddress string + Namespace string } type Silence struct { @@ -36,6 +37,10 @@ func NewSilence(config SilenceConfig) (*Silence, error) { return nil, microerror.Mask(err) } + var controllerName string = project.Name() + if len(config.Namespace) > 0 { + controllerName = controllerName + "-" + config.Namespace + } var operatorkitController *controller.Controller { c := controller.Config{ @@ -46,7 +51,8 @@ func NewSilence(config SilenceConfig) (*Silence, error) { }, Resources: resources, - Name: project.Name() + "-silence-controller", + Name: controllerName + "-silence-controller", + Namespace: config.Namespace, } operatorkitController, err = controller.New(c) diff --git a/service/service.go b/service/service.go index 4c689e38..6fa332e1 100644 --- a/service/service.go +++ b/service/service.go @@ -32,9 +32,9 @@ type Config struct { type Service struct { Version *version.Service - bootOnce sync.Once - silenceController *controller.Silence - operatorCollector *collector.Set + bootOnce sync.Once + silenceControllers []*controller.Silence + operatorCollector *collector.Set } // New creates a new configured service object. @@ -97,20 +97,23 @@ func New(config Config) (*Service, error) { } } - var silenceController *controller.Silence - { - + watchNamespaces := config.Viper.GetStringSlice(config.Flag.Service.WatchNamespaces) + var silenceControllers []*controller.Silence + for _, watchNamespace := range watchNamespaces { c := controller.SilenceConfig{ K8sClient: k8sClient, Logger: config.Logger, AlertManagerAddress: config.Viper.GetString(config.Flag.Service.AlertManager.Address), + Namespace: watchNamespace, } + var silenceController *controller.Silence silenceController, err = controller.NewSilence(c) if err != nil { return nil, microerror.Mask(err) } + silenceControllers = append(silenceControllers, silenceController) } var operatorCollector *collector.Set @@ -145,9 +148,9 @@ func New(config Config) (*Service, error) { s := &Service{ Version: versionService, - bootOnce: sync.Once{}, - silenceController: silenceController, - operatorCollector: operatorCollector, + bootOnce: sync.Once{}, + silenceControllers: silenceControllers, + operatorCollector: operatorCollector, } return s, nil @@ -157,6 +160,8 @@ func (s *Service) Boot(ctx context.Context) { s.bootOnce.Do(func() { go s.operatorCollector.Boot(ctx) // nolint:errcheck - go s.silenceController.Boot(ctx) + for _, silenceController := range s.silenceControllers { + go silenceController.Boot(ctx) + } }) }