-
Notifications
You must be signed in to change notification settings - Fork 81
/
receiver.go
183 lines (171 loc) · 7.16 KB
/
receiver.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
package observability
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"kubegems.io/kubegems/pkg/service/handlers"
"kubegems.io/kubegems/pkg/utils/agents"
"kubegems.io/kubegems/pkg/utils/prometheus"
)
func checkScope(scope string) error {
if scope != prometheus.AlertTypeMonitor && scope != prometheus.AlertTypeLogging {
return fmt.Errorf("scope must be one of logging/monitor")
}
return nil
}
// ListReceiver (日志/监控)告警接收器列表
// @Tags Observability
// @Summary (日志/监控)告警接收器列表
// @Description (日志/监控)告警接收器列表
// @Accept json
// @Produce json
// @Param cluster path string true "cluster"
// @Param namespace path string true "namespace"
// @Param scope query string true "接收器类型(monitor/logging)"
// @Param search query string true "search"
// @Success 200 {object} handlers.ResponseStruct{Data=[]prometheus.ReceiverConfig} "resp"
// @Router /v1/observability/cluster/{cluster}/namespaces/{namespace}/receivers [get]
// @Security JWT
func (h *ObservabilityHandler) ListReceiver(c *gin.Context) {
cluster := c.Param("cluster")
namespace := c.Param("namespace")
search := c.Query("search")
scope := c.Query("scope")
ret := []prometheus.ReceiverConfig{}
if err := h.Execute(c.Request.Context(), cluster, func(ctx context.Context, cli agents.Client) error {
err := checkScope(scope)
if err != nil {
return err
}
ret, err = cli.Extend().ListReceivers(ctx, namespace, scope, search)
return err
}); err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, ret)
}
// CreateReceiver 创建(日志/监控)告警接收器
// @Tags Observability
// @Summary 创建(日志/监控)告警接收器
// @Description 创建(日志/监控)告警接收器
// @Accept json
// @Produce json
// @Param cluster path string true "cluster"
// @Param namespace path string true "namespace"
// @Param form body prometheus.ReceiverConfig true "body"
// @Success 200 {object} handlers.ResponseStruct{Data=string} "resp"
// @Router /v1/observability/cluster/{cluster}/namespaces/{namespace}/receivers [post]
// @Security JWT
func (h *ObservabilityHandler) CreateReceiver(c *gin.Context) {
cluster := c.Param("cluster")
namespace := c.Param("namespace")
req := prometheus.ReceiverConfig{}
if err := c.BindJSON(&req); err != nil {
handlers.NotOK(c, err)
return
}
req.Namespace = namespace
h.SetAuditData(c, "创建", "告警接收器", req.Name)
h.SetExtraAuditDataByClusterNamespace(c, cluster, namespace)
h.m.Lock()
defer h.m.Unlock()
if err := h.Execute(c.Request.Context(), cluster, func(ctx context.Context, cli agents.Client) error {
return cli.Extend().CreateReceiver(ctx, req)
}); err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, "ok")
}
// UpdateReceiver 更新(日志/监控)告警接收器
// @Tags Observability
// @Summary 更新(日志/监控)告警接收器
// @Description 更新(日志/监控)告警接收器
// @Accept json
// @Produce json
// @Param cluster path string true "cluster"
// @Param namespace path string true "namespace"
// @Param name path string true "name"
// @Param form body prometheus.ReceiverConfig true "body"
// @Success 200 {object} handlers.ResponseStruct{Data=string} "resp"
// @Router /v1/observability/cluster/{cluster}/namespaces/{namespace}/receivers/{name} [put]
// @Security JWT
func (h *ObservabilityHandler) UpdateReceiver(c *gin.Context) {
cluster := c.Param("cluster")
namespace := c.Param("namespace")
req := prometheus.ReceiverConfig{}
if err := c.BindJSON(&req); err != nil {
handlers.NotOK(c, err)
return
}
req.Namespace = namespace
h.SetAuditData(c, "更新", "告警接收器", req.Name)
h.SetExtraAuditDataByClusterNamespace(c, cluster, namespace)
h.m.Lock()
defer h.m.Unlock()
if err := h.Execute(c.Request.Context(), cluster, func(ctx context.Context, cli agents.Client) error {
return cli.Extend().UpdateReceiver(ctx, req)
}); err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, "ok")
}
// DeleteReceiver 删除(日志/监控)告警接收器
// @Tags Observability
// @Summary 删除(日志/监控)告警接收器
// @Description 删除(日志/监控)告警接收器
// @Accept json
// @Produce json
// @Param cluster path string true "cluster"
// @Param namespace path string true "namespace"
// @Param source query string true "source"
// @Param name path string true "name"
// @Success 200 {object} handlers.ResponseStruct{Data=string} "resp"
// @Router /v1/observability/cluster/{cluster}/namespaces/{namespace}/receivers/{name} [delete]
// @Security JWT
func (h *ObservabilityHandler) DeleteReceiver(c *gin.Context) {
cluster := c.Param("cluster")
namespace := c.Param("namespace")
name := c.Param("name")
source := c.Query("source")
h.SetExtraAuditDataByClusterNamespace(c, cluster, namespace)
h.SetAuditData(c, "删除", "监控告警接收器", name)
h.m.Lock()
defer h.m.Unlock()
if err := h.Execute(c.Request.Context(), cluster, func(ctx context.Context, cli agents.Client) error {
return cli.Extend().DeleteReceiver(ctx, prometheus.ReceiverConfig{
Name: name,
Namespace: namespace,
Source: source,
})
}); err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, "ok")
}
// TestEmail 发送测试邮件
// @Tags Observability
// @Summary 发送测试邮件
// @Description 发送测试邮件
// @Accept json
// @Produce json
// @Param cluster path string true "cluster"
// @Param namespace path string true "namespace"
// @Param form body prometheus.EmailConfig true "body"
// @Success 200 {object} handlers.ResponseStruct{Data=string} "resp"
// @Router /v1/observability/cluster/{cluster}/namespaces/{namespace}/receivers/_/actions/test [post]
// @Security JWT
func (h *ObservabilityHandler) TestEmail(c *gin.Context) {
req := prometheus.EmailConfig{}
c.BindJSON(&req)
h.SetExtraAuditDataByClusterNamespace(c, c.Param("cluster"), c.Param("namespace"))
h.SetAuditData(c, "测试", "告警接收器", "")
if err := prometheus.TestEmail(req, c.Param("cluster"), c.Param("namespace")); err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, "邮件发送成功!")
}