forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configsyncer.go
274 lines (226 loc) · 8.32 KB
/
configsyncer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package configsyner
import (
"context"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/rancher/norman/controller"
alertconfig "github.com/rancher/rancher/pkg/controllers/user/alert/config"
"github.com/rancher/rancher/pkg/controllers/user/alert/deploy"
"github.com/rancher/rancher/pkg/controllers/user/alert/manager"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
var (
defaultGroupInterval = 10
eventGroupInterval = 1
)
func NewConfigSyncer(ctx context.Context, cluster *config.UserContext, manager *manager.Manager) *ConfigSyncer {
return &ConfigSyncer{
secrets: cluster.Core.Secrets("cattle-alerting"),
clusterAlertLister: cluster.Management.Management.ClusterAlerts(cluster.ClusterName).Controller().Lister(),
projectAlertLister: cluster.Management.Management.ProjectAlerts("").Controller().Lister(),
notifierLister: cluster.Management.Management.Notifiers(cluster.ClusterName).Controller().Lister(),
clusterName: cluster.ClusterName,
alertManager: manager,
}
}
type ConfigSyncer struct {
secrets v1.SecretInterface
projectAlertLister v3.ProjectAlertLister
clusterAlertLister v3.ClusterAlertLister
notifierLister v3.NotifierLister
clusterName string
alertManager *manager.Manager
}
func (d *ConfigSyncer) ProjectSync(key string, alert *v3.ProjectAlert) error {
return d.sync()
}
func (d *ConfigSyncer) ClusterSync(key string, alert *v3.ClusterAlert) error {
return d.sync()
}
func (d *ConfigSyncer) NotifierSync(key string, alert *v3.Notifier) error {
return d.sync()
}
//sync: update the secret which store the configuration of alertmanager given the latest configured notifiers and alerts rules.
//For each alert, it will generate a route and a receiver in the alertmanager's configuration file.
func (d *ConfigSyncer) sync() error {
if d.alertManager.IsDeploy == false {
return nil
}
notifiers, err := d.notifierLister.List("", labels.NewSelector())
if err != nil {
return errors.Wrapf(err, "List notifiers")
}
clusterAlerts, err := d.clusterAlertLister.List("", labels.NewSelector())
if err != nil {
return errors.Wrapf(err, "List cluster alerts")
}
projectAlerts, err := d.projectAlertLister.List("", labels.NewSelector())
if err != nil {
return errors.Wrapf(err, "List project alerts")
}
pAlerts := []*v3.ProjectAlert{}
for _, alert := range projectAlerts {
if controller.ObjectInCluster(d.clusterName, alert) {
pAlerts = append(pAlerts, alert)
}
}
config := d.alertManager.GetDefaultConfig()
config.Global.PagerdutyURL = "https://events.pagerduty.com/generic/2010-04-15/create_event.json"
d.addClusterAlert2Config(config, clusterAlerts, notifiers)
d.addProjectAlert2Config(config, pAlerts, notifiers)
data, err := yaml.Marshal(config)
if err != nil {
return errors.Wrapf(err, "Marshal secrets")
}
//TODO: check why lister does not work
configSecret, err := d.secrets.Get("alertmanager", metav1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "Get secrets")
}
if string(configSecret.Data["config.yml"]) != string(data) {
configSecret.Data["config.yml"] = data
configSecret.Data["notification.tmpl"] = []byte(deploy.NotificationTmpl)
_, err = d.secrets.Update(configSecret)
if err != nil {
return errors.Wrapf(err, "Update secrets")
}
} else {
logrus.Debug("The config stay the same, will not update the secret")
}
return nil
}
func (d *ConfigSyncer) getNotifier(id string, notifiers []*v3.Notifier) *v3.Notifier {
for _, n := range notifiers {
if d.clusterName+":"+n.Name == id {
return n
}
}
return nil
}
func (d *ConfigSyncer) addProjectAlert2Config(config *alertconfig.Config, alerts []*v3.ProjectAlert, notifiers []*v3.Notifier) {
for _, alert := range alerts {
if alert.Status.AlertState == "inactive" {
continue
}
id := alert.Namespace + "-" + alert.Name
receiver := &alertconfig.Receiver{Name: id}
exist := d.addRecipients(notifiers, receiver, alert.Spec.Recipients)
if exist {
config.Receivers = append(config.Receivers, receiver)
d.addRoute(config, id, alert.Spec.InitialWaitSeconds, alert.Spec.RepeatIntervalSeconds, defaultGroupInterval)
}
}
}
func (d *ConfigSyncer) addClusterAlert2Config(config *alertconfig.Config, alerts []*v3.ClusterAlert, notifiers []*v3.Notifier) {
for _, alert := range alerts {
if alert.Status.AlertState == "inactive" {
continue
}
id := alert.Namespace + "-" + alert.Name
receiver := &alertconfig.Receiver{Name: id}
exist := d.addRecipients(notifiers, receiver, alert.Spec.Recipients)
if exist {
config.Receivers = append(config.Receivers, receiver)
updateGroupInterval := defaultGroupInterval
if alert.Spec.TargetEvent != nil {
updateGroupInterval = eventGroupInterval
}
d.addRoute(config, id, alert.Spec.InitialWaitSeconds, alert.Spec.RepeatIntervalSeconds, updateGroupInterval)
}
}
}
func (d *ConfigSyncer) addRoute(config *alertconfig.Config, id string, initalWait, repeatInterval, groupInterval int) {
routes := config.Route.Routes
if routes == nil {
routes = []*alertconfig.Route{}
}
match := map[string]string{}
match["alert_id"] = id
route := &alertconfig.Route{
Receiver: id,
Match: match,
}
gw := model.Duration(time.Duration(initalWait) * time.Second)
route.GroupWait = &gw
ri := model.Duration(time.Duration(repeatInterval) * time.Second)
route.RepeatInterval = &ri
if groupInterval != defaultGroupInterval {
gi := model.Duration(time.Duration(groupInterval) * time.Second)
route.GroupInterval = &gi
}
routes = append(routes, route)
config.Route.Routes = routes
}
func (d *ConfigSyncer) addRecipients(notifiers []*v3.Notifier, receiver *alertconfig.Receiver, recipients []v3.Recipient) bool {
receiverExist := false
for _, r := range recipients {
if r.NotifierName != "" {
notifier := d.getNotifier(r.NotifierName, notifiers)
if notifier == nil {
logrus.Debugf("Can not find the notifier %s", r.NotifierName)
continue
}
if notifier.Spec.PagerdutyConfig != nil {
pagerduty := &alertconfig.PagerdutyConfig{
ServiceKey: alertconfig.Secret(notifier.Spec.PagerdutyConfig.ServiceKey),
Description: `{{ template "rancher.title" . }}`,
}
if r.Recipient != "" {
pagerduty.ServiceKey = alertconfig.Secret(r.Recipient)
}
receiver.PagerdutyConfigs = append(receiver.PagerdutyConfigs, pagerduty)
receiverExist = true
} else if notifier.Spec.WebhookConfig != nil {
webhook := &alertconfig.WebhookConfig{
URL: notifier.Spec.WebhookConfig.URL,
}
if r.Recipient != "" {
webhook.URL = r.Recipient
}
receiver.WebhookConfigs = append(receiver.WebhookConfigs, webhook)
receiverExist = true
} else if notifier.Spec.SlackConfig != nil {
slack := &alertconfig.SlackConfig{
APIURL: alertconfig.Secret(notifier.Spec.SlackConfig.URL),
Channel: notifier.Spec.SlackConfig.DefaultRecipient,
Text: `{{ template "slack.text" . }}`,
Title: `{{ template "rancher.title" . }}`,
TitleLink: "",
Color: `{{ if eq (index .Alerts 0).Labels.severity "critical" }}danger{{ else if eq (index .Alerts 0).Labels.severity "warning" }}warning{{ else }}good{{ end }}`,
}
if r.Recipient != "" {
slack.Channel = r.Recipient
}
receiver.SlackConfigs = append(receiver.SlackConfigs, slack)
receiverExist = true
} else if notifier.Spec.SMTPConfig != nil {
header := map[string]string{}
header["Subject"] = `{{ template "rancher.title" . }}`
email := &alertconfig.EmailConfig{
Smarthost: notifier.Spec.SMTPConfig.Host + ":" + strconv.Itoa(notifier.Spec.SMTPConfig.Port),
AuthPassword: alertconfig.Secret(notifier.Spec.SMTPConfig.Password),
AuthUsername: notifier.Spec.SMTPConfig.Username,
RequireTLS: ¬ifier.Spec.SMTPConfig.TLS,
To: notifier.Spec.SMTPConfig.DefaultRecipient,
Headers: header,
From: notifier.Spec.SMTPConfig.Sender,
HTML: `{{ template "email.text" . }}`,
}
if r.Recipient != "" {
email.To = r.Recipient
}
receiver.EmailConfigs = append(receiver.EmailConfigs, email)
receiverExist = true
}
}
}
return receiverExist
}