Skip to content
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

feat: support watching specific namespaces #226

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
*.swp
TODO
/silence-operator
/silence-operator-v*-*-linux-*
/silence-operator-v*-*-darwin-*
/silence-operator-v*-*-amd64-*
/silence-operator-v*
!vendor/**
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Support watching specific namespaces

## [0.9.0] - 2023-05-22

### Added
Expand Down
2 changes: 1 addition & 1 deletion config/crd/monitoring.giantswarm.io_silences.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
listKind: SilenceList
plural: silences
singular: silence
scope: Cluster
scope: Namespaced
versions:
- name: v1alpha1
schema:
Expand Down
5 changes: 3 additions & 2 deletions flag/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions helm/silence-operator/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ data:
listen:
address: 'http://0.0.0.0:8000'
service:
watchNamespaces:
- ""
alertmanager:
address: {{ .Values.alertmanagerAddress }}
kubernetes:
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion service/controller/silence.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type SilenceConfig struct {
Logger micrologger.Logger

AlertManagerAddress string
Namespace string
}

type Silence struct {
Expand All @@ -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{
Expand All @@ -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)
Expand Down
25 changes: 15 additions & 10 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
})
}