-
Notifications
You must be signed in to change notification settings - Fork 82
/
channel.go
298 lines (276 loc) · 10 KB
/
channel.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2022 The kubegems.io Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package observability
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"kubegems.io/kubegems/pkg/service/handlers"
"kubegems.io/kubegems/pkg/service/models"
"kubegems.io/kubegems/pkg/utils/prometheus"
)
func (h *ObservabilityHandler) getChannelReq(c *gin.Context) (*models.AlertChannel, error) {
req := models.AlertChannel{}
if err := c.BindJSON(&req); err != nil {
return nil, err
}
tenantID := c.Param("tenant_id")
if tenantID != "_all" {
t, _ := strconv.Atoi(tenantID)
if t == 0 {
return nil, fmt.Errorf("tenant id not valid")
}
tmp := uint(t)
h.SetExtraAuditData(c, models.ResTenant, tmp)
req.TenantID = &tmp
}
if req.ChannelConfig.ChannelIf == nil {
return nil, fmt.Errorf("channel config can't be null")
}
if err := req.ChannelConfig.ChannelIf.Check(); err != nil {
return nil, err
}
return &req, nil
}
// ListChannels 告警渠道列表
// @Tags Observability
// @Summary 告警渠道列表
// @Description 告警渠道列表
// @Accept json
// @Produce json
// @Param tenant_id path string true "租户id, 所有租户为_all"
// @Param search query string false "search in (name)"
// @Param page query int false "page"
// @Param size query int false "size"
// @Success 200 {object} handlers.ResponseStruct{Data=[]models.AlertChannel} "resp"
// @Router /v1/observability/tenant/{tenant_id}/channels [get]
// @Security JWT
func (h *ObservabilityHandler) ListChannels(c *gin.Context) {
list := []models.AlertChannel{}
query, err := handlers.GetQuery(c, nil)
if err != nil {
handlers.NotOK(c, err)
return
}
cond := &handlers.PageQueryCond{
Model: "AlertChannel",
SearchFields: []string{"name"},
}
tenantID := c.Param("tenant_id")
if tenantID != "_all" {
cond.Where = append(cond.Where, handlers.Args("tenant_id is null or tenant_id = ?", tenantID))
}
total, page, size, err := query.PageList(h.GetDB().WithContext(c.Request.Context()), cond, &list)
if err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, handlers.Page(total, list, page, size))
}
// GetChannel 渠道列表详情
// @Tags Observability
// @Summary 渠道列表详情
// @Description 渠道列表详情
// @Accept json
// @Produce json
// @Param tenant_id path string true "租户id, 所有租户为_all"
// @Param channel_id path string true "告警渠道id"
// @Success 200 {object} handlers.ResponseStruct{Data=models.AlertChannel} "resp"
// @Router /v1/observability/tenant/{tenant_id}/channels/{channel_id} [get]
// @Security JWT
func (h *ObservabilityHandler) GetChannel(c *gin.Context) {
tenantID := c.Param("tenant_id")
query := h.GetDB().WithContext(c.Request.Context())
if tenantID != "_all" {
query.Where("tenant_id = ? or tenant_id is null", tenantID)
}
ret := models.AlertChannel{}
if err := query.First(&ret, "id = ?", c.Param("channel_id")).Error; err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, ret)
}
// CreateChannel 创建告警渠道
// @Tags Observability
// @Summary 创建告警渠道
// @Description 创建告警渠道
// @Accept json
// @Produce json
// @Param tenant_id path string true "租户id, 所有租户为_all"
// @Param form body models.AlertChannel true "body"
// @Success 200 {object} handlers.ResponseStruct{Data=string} "resp"
// @Router /v1/observability/tenant/{tenant_id}/channels [post]
// @Security JWT
func (h *ObservabilityHandler) CreateChannel(c *gin.Context) {
req, err := h.getChannelReq(c)
if err != nil {
handlers.NotOK(c, err)
return
}
h.SetAuditData(c, "创建", "告警渠道", req.Name)
if req.TenantID == nil {
if c.Param("tenant_id") != "_all" {
handlers.NotOK(c, fmt.Errorf("你不能创建系统级告警渠道"))
return
}
} else {
h.SetExtraAuditData(c, models.ResTenant, *req.TenantID)
}
if err := h.GetDB().WithContext(c.Request.Context()).Create(req).Error; err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, "ok")
}
// UpdateChannel 更新告警渠道
// @Tags Observability
// @Summary 更新告警渠道
// @Description 更新告警渠道
// @Accept json
// @Produce json
// @Param tenant_id path string true "租户id, 所有租户为_all"
// @Param channel_id path string true "告警渠道id"
// @Param form body models.AlertChannel true "body"
// @Success 200 {object} handlers.ResponseStruct{Data=map[string]bool} "告警规则-更新状态的map"
// @Router /v1/observability/tenant/{tenant_id}/channels/{channel_id} [put]
// @Security JWT
func (h *ObservabilityHandler) UpdateChannel(c *gin.Context) {
req, err := h.getChannelReq(c)
if err != nil {
handlers.NotOK(c, err)
return
}
h.SetAuditData(c, "更新", "告警渠道", req.Name)
if req.TenantID == nil {
if c.Param("tenant_id") != "_all" {
handlers.NotOK(c, fmt.Errorf("你不能更新系统级告警渠道"))
return
}
} else {
h.SetExtraAuditData(c, models.ResTenant, *req.TenantID)
}
// find associated alert rules
alertrules := []*models.AlertRule{}
ctx := c.Request.Context()
if err := h.GetDB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Updates(req).Error; err != nil {
return err
}
alertruleIDs := []uint{}
if err := tx.Model(&models.AlertReceiver{}).Distinct("alert_rule_id").Where("alert_channel_id = ?", req.ID).Scan(&alertruleIDs).Error; err != nil {
return err
}
return tx.Preload("Receivers.AlertChannel").Where("id in ?", alertruleIDs).Find(&alertrules).Error
}); err != nil {
handlers.NotOK(c, err)
return
}
status, _ := h.syncAlertRulesWithTimeout(ctx, alertrules, 5*time.Second)
handlers.OK(c, status)
}
// DeleteChannel 删除告警渠道
// @Tags Observability
// @Summary 删除告警渠道
// @Description 删除告警渠道
// @Accept json
// @Produce json
// @Param tenant_id path string true "租户id, 所有租户为_all"
// @Param channel_id path string true "告警渠道id"
// @Param form body models.AlertChannel true "body"
// @Success 200 {object} handlers.ResponseStruct{Data=string} "resp"
// @Router /v1/observability/tenant/{tenant_id}/channels/{channel_id} [delete]
// @Security JWT
func (h *ObservabilityHandler) DeleteChannel(c *gin.Context) {
ch := &models.AlertChannel{}
ctx := c.Request.Context()
if err := h.GetDB().WithContext(ctx).First(ch, "id = ?", c.Param("channel_id")).Error; err != nil {
handlers.NotOK(c, err)
return
}
h.SetAuditData(c, "删除", "告警渠道", ch.Name)
if ch.TenantID == nil {
if c.Param("tenant_id") != "_all" {
handlers.NotOK(c, fmt.Errorf("你不能删除系统级告警渠道"))
return
}
} else {
h.SetExtraAuditData(c, models.ResTenant, *ch.TenantID)
}
receivers := []models.AlertReceiver{}
if err := h.GetDB().WithContext(ctx).Preload("AlertRule").Find(&receivers, "alert_channel_id = ?", c.Param("channel_id")).Error; err != nil {
handlers.NotOK(c, err)
return
}
if len(receivers) > 0 {
tmp := make([]string, len(receivers))
for i, v := range receivers {
tmp[i] = v.AlertRule.FullName()
}
handlers.NotOK(c, fmt.Errorf("该告警渠道正在被告警规则: [%s] 使用", strings.Join(tmp, ",")))
return
}
if err := h.GetDB().WithContext(ctx).Delete(ch).Error; err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, "ok")
}
// TestChannel 测试告警渠道
// @Tags Observability
// @Summary 测试告警渠道
// @Description 测试告警渠道
// @Accept json
// @Produce json
// @Param tenant_id path string true "租户id, 所有租户为_all"
// @Param channel_id path string true "告警渠道id"
// @Success 200 {object} handlers.ResponseStruct{Data=string} "resp"
// @Router /v1/observability/tenant/{tenant_id}/channels/{channel_id}/test [post]
// @Security JWT
func (h *ObservabilityHandler) TestChannel(c *gin.Context) {
ch := &models.AlertChannel{}
if err := h.GetDB().WithContext(c.Request.Context()).First(ch, "id = ?", c.Param("channel_id")).Error; err != nil {
handlers.NotOK(c, err)
return
}
now := time.Now()
alertObj := prometheus.WebhookAlert{
Receiver: ch.ReceiverName(),
Status: "firing",
Alerts: []prometheus.Alert{
{
Status: "firing",
Labels: map[string]string{
prometheus.AlertNameLabel: "kubegems-test-alert",
prometheus.SeverityLabel: prometheus.SeverityError,
prometheus.AlertClusterKey: "kubegems",
prometheus.AlertNamespaceLabel: "kubegems-test-namespace",
},
Annotations: map[string]string{
prometheus.MessageAnnotationsKey: "kubegems test alert message",
prometheus.ValueAnnotationKey: "0",
},
StartsAt: &now,
},
},
}
if err := ch.ChannelConfig.ChannelIf.Test(alertObj); err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, "ok")
}