Skip to content

Commit

Permalink
Merge pull request #50 from wanjunlei/master
Browse files Browse the repository at this point in the history
add cluster label to the notifications
  • Loading branch information
Benjamin Huo authored Nov 27, 2020
2 parents ec19614 + 011525b commit 4f23c94
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 21 deletions.
4 changes: 4 additions & 0 deletions config/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,10 @@ spec:
type: object
global:
properties:
cluster:
description: The name of the cluster in which the notification
manager is deployed.
type: string
template:
description: The name of the template to generate message.
If the receiver dose not setup template, it will use this.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,10 @@ spec:
type: object
global:
properties:
cluster:
description: The name of the cluster in which the notification
manager is deployed.
type: string
template:
description: The name of the template to generate message.
If the receiver dose not setup template, it will use this.
Expand Down
Binary file modified docs/images/architecture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions helm/crds/notificationmanagers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,10 @@ spec:
type: object
global:
properties:
cluster:
description: The name of the cluster in which the notification
manager is deployed.
type: string
template:
description: The name of the template to generate message.
If the receiver dose not setup template, it will use this.
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/v1alpha1/notificationmanager_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ type GlobalOptions struct {
// The name of the template to generate message.
// If the receiver dose not setup template, it will use this.
Template string `json:"template,omitempty"`
// The name of the cluster in which the notification manager is deployed.
Cluster string `json:"cluster,omitempty"`
}

type EmailOptions struct {
Expand Down
10 changes: 4 additions & 6 deletions pkg/notify/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,7 @@ func (c *Config) nmChange(p *param) {
c.defaultConfigSelector = p.defaultConfigSelector
c.tenantReceiverSelector = p.tenantReceiverSelector
c.globalReceiverSelector = p.globalReceiverSelector
if p.ReceiverOpts != nil {
c.ReceiverOpts = p.ReceiverOpts
}
c.ReceiverOpts = p.ReceiverOpts
c.nmNamespaces = p.nmNamespaces
c.nmAdd = true
} else if p.op == opDel {
Expand Down Expand Up @@ -612,13 +610,13 @@ func (c *Config) updateReloadtimestamp() {
var objs []runtime.Object
receivers, err := getObjects(f.newReceiverObjectListFunc())
if err != nil {
_ = level.Error(c.logger).Log("msg", "Failed to list %s receiver", key, "err", err)
_ = level.Error(c.logger).Log("msg", "Failed to list receiver", "type", key, "err", err)
}
objs = append(objs, receivers...)

configs, err := getObjects(f.newConfigObjectListFunc())
if err != nil {
_ = level.Error(c.logger).Log("msg", "Failed to list %s config", key, "err", err)
_ = level.Error(c.logger).Log("msg", "Failed to list config", "type", key, "err", err)
}
objs = append(objs, configs...)

Expand All @@ -635,7 +633,7 @@ func (c *Config) updateReloadtimestamp() {

err = c.client.Update(c.ctx, obj)
if err != nil {
_ = level.Error(c.logger).Log("msg", "update %s error", key, "err", err)
_ = level.Error(c.logger).Log("msg", "update error", "type", key, "name", accessor.GetName(), "namespace", accessor.GetNamespace(), "err", err)
continue
}
}
Expand Down
32 changes: 17 additions & 15 deletions pkg/notify/notifier/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,28 @@ func (n *Notifier) Notify(ctx context.Context, data template.Data) []error {
}
request.Header.Set("Content-Type", "application/json")

if w.WebhookConfig.HttpConfig.BearerToken != nil {
bearer, err := n.notifierCfg.GetSecretData(w.GetNamespace(), w.WebhookConfig.HttpConfig.BearerToken)
if err != nil {
_ = level.Error(n.logger).Log("msg", "WebhookNotifier: get bearer token error", "error", err.Error())
return err
}

request.Header.Set("Authorization", bearer)
} else if w.WebhookConfig.HttpConfig.BasicAuth != nil {
pass := ""
if w.WebhookConfig.HttpConfig.BasicAuth.Password != nil {
p, err := n.notifierCfg.GetSecretData(w.GetNamespace(), w.WebhookConfig.HttpConfig.BasicAuth.Password)
if w.WebhookConfig.HttpConfig != nil {
if w.WebhookConfig.HttpConfig.BearerToken != nil {
bearer, err := n.notifierCfg.GetSecretData(w.GetNamespace(), w.WebhookConfig.HttpConfig.BearerToken)
if err != nil {
_ = level.Error(n.logger).Log("msg", "WebhookNotifier: get password error", "error", err.Error())
_ = level.Error(n.logger).Log("msg", "WebhookNotifier: get bearer token error", "error", err.Error())
return err
}

pass = p
request.Header.Set("Authorization", bearer)
} else if w.WebhookConfig.HttpConfig.BasicAuth != nil {
pass := ""
if w.WebhookConfig.HttpConfig.BasicAuth.Password != nil {
p, err := n.notifierCfg.GetSecretData(w.GetNamespace(), w.WebhookConfig.HttpConfig.BasicAuth.Password)
if err != nil {
_ = level.Error(n.logger).Log("msg", "WebhookNotifier: get password error", "error", err.Error())
return err
}

pass = p
}
request.SetBasicAuth(w.WebhookConfig.HttpConfig.BasicAuth.Username, pass)
}
request.SetBasicAuth(w.WebhookConfig.HttpConfig.BasicAuth.Username, pass)
}

transport, err := n.getTransport(w)
Expand Down
11 changes: 11 additions & 0 deletions pkg/webhook/v1/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ func (h *HttpHandler) CreateNotificationfromAlerts(w http.ResponseWriter, r *htt
go func() {
defer close(wkrCh)

cluster := "default"
if h.notifierCfg != nil && h.notifierCfg.ReceiverOpts != nil && h.notifierCfg.ReceiverOpts.Global != nil {
if h.notifierCfg.ReceiverOpts.Global.Cluster != "" {
cluster = h.notifierCfg.ReceiverOpts.Global.Cluster
}
}

for _, alert := range data.Alerts {
alert.Labels["cluster"] = cluster
}

dm := make(map[string]template.Data)
ns, ok := wkload.CommonLabels["namespace"]
if ok {
Expand Down

0 comments on commit 4f23c94

Please sign in to comment.