Commit 2c63f3a1 authored by 李科's avatar 李科

refactor: 预警规则设置同步至PrometheusRule

parent 58c8e0c3
...@@ -17,7 +17,7 @@ func AddAlertRules(c *gin.Context) { ...@@ -17,7 +17,7 @@ func AddAlertRules(c *gin.Context) {
return return
} }
sort.SliceStable(req.AlertCondition, func(i, j int) bool { sort.SliceStable(req.AlertCondition, func(i, j int) bool {
return *req.AlertCondition[i].ThresholdsMin < *req.AlertCondition[j].ThresholdsMin return req.AlertCondition[i].RiskLevel < req.AlertCondition[j].RiskLevel
}) })
svc := service.AlertRulesSvc{User: header.GetUser(c)} svc := service.AlertRulesSvc{User: header.GetUser(c)}
......
...@@ -108,7 +108,7 @@ func (a *AlertRulesSvc) Add(req request.AddAlertRules) (err error) { ...@@ -108,7 +108,7 @@ func (a *AlertRulesSvc) Add(req request.AddAlertRules) (err error) {
return nil return nil
} }
func (a *AlertRulesSvc) Update(req request.UpdateAlertRules) error { func (a *AlertRulesSvc) Update(req request.UpdateAlertRules) (err error) {
db, err := client.GetDbClient() db, err := client.GetDbClient()
if err != nil { if err != nil {
err = resp.DbConnectError.WithError(err) err = resp.DbConnectError.WithError(err)
...@@ -184,10 +184,32 @@ func (a *AlertRulesSvc) Update(req request.UpdateAlertRules) error { ...@@ -184,10 +184,32 @@ func (a *AlertRulesSvc) Update(req request.UpdateAlertRules) error {
return err return err
} }
} }
var item response.AlertRulesItem
item, err = a.GetDataById(request.DetailAlertRules{Id: data.Id})
prSvc := PrometheusRuleSvc{User: a.User}
{
// 删除PrometheusRule
err = prSvc.Delete(item)
if err != nil {
return
}
// 再次创建PrometheusRule
err = prSvc.Create(item)
if err != nil {
_, err = db.Delete(&data)
if err != nil {
return err
}
return
}
}
return nil return nil
} }
func (a *AlertRulesSvc) UpdateIsEnabled(req request.UpdateIsEnabledAlertRules) error { func (a *AlertRulesSvc) UpdateIsEnabled(req request.UpdateIsEnabledAlertRules) (err error) {
db, err := client.GetDbClient() db, err := client.GetDbClient()
if err != nil { if err != nil {
err = resp.DbConnectError.WithError(err) err = resp.DbConnectError.WithError(err)
...@@ -200,10 +222,31 @@ func (a *AlertRulesSvc) UpdateIsEnabled(req request.UpdateIsEnabledAlertRules) e ...@@ -200,10 +222,31 @@ func (a *AlertRulesSvc) UpdateIsEnabled(req request.UpdateIsEnabledAlertRules) e
UpdatedBy: a.User.SystemAccount, UpdatedBy: a.User.SystemAccount,
UpdatedAt: now, UpdatedAt: now,
} }
if req.IsEnabled == 2 {
// TODO 关闭状态需要删除prometheus规则
var item response.AlertRulesItem
item, err = a.GetDataById(request.DetailAlertRules{Id: req.Id})
if err != nil {
return
} }
if req.IsEnabled == 2 {
if item.IsEnabled == 1 {
prSvc := PrometheusRuleSvc{User: a.User}
err = prSvc.Delete(item)
if err != nil {
return
}
}
} else if req.IsEnabled == 1 {
if item.IsEnabled == 2 {
prSvc := PrometheusRuleSvc{User: a.User}
err = prSvc.Create(item)
if err != nil {
return
}
}
}
session := db.NewSession() session := db.NewSession()
defer session.Close() defer session.Close()
_, err = session.Table(data.TableName()).Cols("is_enabled,updated_by,updated_at").Where("id = ?", req.Id).Update(&data) _, err = session.Table(data.TableName()).Cols("is_enabled,updated_by,updated_at").Where("id = ?", req.Id).Update(&data)
...@@ -278,8 +321,15 @@ func (a *AlertRulesSvc) Delete(ids []string) (err error) { ...@@ -278,8 +321,15 @@ func (a *AlertRulesSvc) Delete(ids []string) (err error) {
return return
} }
if !exist { if !exist {
prSvc := PrometheusRuleSvc{User: a.User}
err = prSvc.Delete(response.AlertRulesItem{AlertRules: entity.AlertRules{Id: id}})
if err != nil {
return
}
_, err = db.NewSession().Where("id = ?", id).Delete(new(entity.AlertRules)) _, err = db.NewSession().Where("id = ?", id).Delete(new(entity.AlertRules))
// TODO 删除普罗米修斯规则 if err != nil {
return
}
} else { } else {
return errors.New("alert_rules_id already exists in opensearch") return errors.New("alert_rules_id already exists in opensearch")
} }
......
...@@ -7,7 +7,7 @@ import ( ...@@ -7,7 +7,7 @@ import (
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/util" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/util"
"strings" "strings"
v1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
) )
var ( var (
...@@ -41,7 +41,7 @@ type PrometheusRule struct { ...@@ -41,7 +41,7 @@ type PrometheusRule struct {
Header map[string]string Header map[string]string
} }
func (p PrometheusRule) Create(pRule *v1.PrometheusRule) error { func (p PrometheusRule) Create(pRule *monitoringv1.PrometheusRule) error {
k8sSvc := K8sSvc{Header: p.Header} k8sSvc := K8sSvc{Header: p.Header}
c := &Content{Kind: PrometheusRuleKind, ApiVersion: PrometheusRuleApiVersion, Metadata: pRule.ObjectMeta, Spec: pRule.Spec} c := &Content{Kind: PrometheusRuleKind, ApiVersion: PrometheusRuleApiVersion, Metadata: pRule.ObjectMeta, Spec: pRule.Spec}
_, err := k8sSvc.SendFile(c) _, err := k8sSvc.SendFile(c)
...@@ -54,7 +54,7 @@ func (p PrometheusRule) Delete(namespace string, name string) error { ...@@ -54,7 +54,7 @@ func (p PrometheusRule) Delete(namespace string, name string) error {
return err return err
} }
func (p PrometheusRule) Update(pRule *v1.PrometheusRule) error { func (p PrometheusRule) Update(pRule *monitoringv1.PrometheusRule) error {
updateUrl := fmt.Sprintf("%s/kubernetes/api/v1/_raw/%s/namespace/%s/name/%s", conf.Options.AweRestURL, PrometheusRuleName, pRule.Namespace, pRule.Name) updateUrl := fmt.Sprintf("%s/kubernetes/api/v1/_raw/%s/namespace/%s/name/%s", conf.Options.AweRestURL, PrometheusRuleName, pRule.Namespace, pRule.Name)
body, _ := json.Marshal(pRule) body, _ := json.Marshal(pRule)
p.Header["Content-Type"] = "application/json" p.Header["Content-Type"] = "application/json"
...@@ -62,8 +62,7 @@ func (p PrometheusRule) Update(pRule *v1.PrometheusRule) error { ...@@ -62,8 +62,7 @@ func (p PrometheusRule) Update(pRule *v1.PrometheusRule) error {
return err return err
} }
func (p PrometheusRule) Get(namespace string, name string) (obj *v1.PrometheusRule, err error) { func (p PrometheusRule) Get(namespace string, name string) (obj *monitoringv1.PrometheusRule, err error) {
// http://awecloud-rest/awecloud/rest/kubernetes/api/v1/_raw/prometheusrules.monitoring.coreos.com/namespace/kube-public/name/nginx
getUrl := fmt.Sprintf("%s/kubernetes/api/v1/_raw/%s/namespace/%s/name/%s", conf.Options.AweRestURL, PrometheusRuleName, namespace, name) getUrl := fmt.Sprintf("%s/kubernetes/api/v1/_raw/%s/namespace/%s/name/%s", conf.Options.AweRestURL, PrometheusRuleName, namespace, name)
res, err := util.ProxySendRes("GET", getUrl, "", p.Header) res, err := util.ProxySendRes("GET", getUrl, "", p.Header)
if err != nil { if err != nil {
......
...@@ -10,8 +10,8 @@ import ( ...@@ -10,8 +10,8 @@ import (
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/constant" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/constant"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/service/k8s" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/service/k8s"
"go.uber.org/zap" "go.uber.org/zap"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/intstr"
"log"
"strings" "strings"
) )
...@@ -20,11 +20,14 @@ type PrometheusRuleSvc struct { ...@@ -20,11 +20,14 @@ type PrometheusRuleSvc struct {
} }
func (p *PrometheusRuleSvc) Create(data response.AlertRulesItem) (err error) { func (p *PrometheusRuleSvc) Create(data response.AlertRulesItem) (err error) {
pr := monitoringv1.PrometheusRule{}
prometheusRuleName := k8s.GetPrometheusRuleName(data.Id) prometheusRuleName := k8s.GetPrometheusRuleName(data.Id)
pr.Name = prometheusRuleName pr := monitoringv1.PrometheusRule{
pr.Namespace = k8s.Namespace ObjectMeta: v1.ObjectMeta{
pr.Labels = k8s.AlertDefLabels Name: prometheusRuleName,
Namespace: k8s.Namespace,
Labels: k8s.AlertDefLabels,
},
}
group := monitoringv1.RuleGroup{} group := monitoringv1.RuleGroup{}
groupInterval := monitoringv1.Duration(fmt.Sprintf("%d%s", data.CheckPeriod, "m")) groupInterval := monitoringv1.Duration(fmt.Sprintf("%d%s", data.CheckPeriod, "m"))
...@@ -80,35 +83,42 @@ func (p *PrometheusRuleSvc) Create(data response.AlertRulesItem) (err error) { ...@@ -80,35 +83,42 @@ func (p *PrometheusRuleSvc) Create(data response.AlertRulesItem) (err error) {
group.Rules = append(group.Rules, rule) group.Rules = append(group.Rules, rule)
} }
pr.Spec.Groups = append(pr.Spec.Groups, group) pr.Spec.Groups = append(pr.Spec.Groups, group)
header := make(map[string]string) header := map[string]string{"Authorization": "Bearer " + conf.Options.KubernetesToken}
header["Authorization"] = "Bearer " + conf.Options.KubernetesToken
prSvc := k8s.PrometheusRule{Header: header} prSvc := k8s.PrometheusRule{Header: header}
conf.Logger.Info("创建规则", zap.Any("pr", pr)) conf.Logger.Info("pr", zap.Any("pr", pr))
err = prSvc.Create(&pr) err = prSvc.Create(&pr)
if err != nil { return
log.Println("添加失败" + err.Error()) }
} else {
log.Println("添加成功") func (p *PrometheusRuleSvc) Get(data response.AlertRulesItem) (obj *monitoringv1.PrometheusRule, err error) {
prometheusRuleName := k8s.GetPrometheusRuleName(data.Id)
pr := monitoringv1.PrometheusRule{
ObjectMeta: v1.ObjectMeta{
Name: prometheusRuleName,
Namespace: k8s.Namespace,
Labels: k8s.AlertDefLabels,
},
} }
header := map[string]string{"Authorization": "Bearer " + conf.Options.KubernetesToken}
prSvc := k8s.PrometheusRule{Header: header}
conf.Logger.Info("pr", zap.Any("pr", pr))
obj, err = prSvc.Get(pr.Namespace, pr.Name)
return return
} }
func (p *PrometheusRuleSvc) Delete(data response.AlertRulesItem) (err error) { func (p *PrometheusRuleSvc) Delete(data response.AlertRulesItem) (err error) {
pr := monitoringv1.PrometheusRule{}
prometheusRuleName := k8s.GetPrometheusRuleName(data.Id) prometheusRuleName := k8s.GetPrometheusRuleName(data.Id)
pr.Name = prometheusRuleName pr := monitoringv1.PrometheusRule{
pr.Namespace = k8s.Namespace ObjectMeta: v1.ObjectMeta{
pr.Labels = k8s.AlertDefLabels Name: prometheusRuleName,
Namespace: k8s.Namespace,
Labels: k8s.AlertDefLabels,
},
}
header := make(map[string]string) header := map[string]string{"Authorization": "Bearer " + conf.Options.KubernetesToken}
header["Authorization"] = "Bearer " + conf.Options.KubernetesToken
prSvc := k8s.PrometheusRule{Header: header} prSvc := k8s.PrometheusRule{Header: header}
conf.Logger.Info("创建规则", zap.Any("pr", pr)) conf.Logger.Info("pr", zap.Any("pr", pr))
err = prSvc.Delete(pr.Namespace, pr.Name) err = prSvc.Delete(pr.Namespace, pr.Name)
if err != nil {
fmt.Println("删除失败" + err.Error())
} else {
fmt.Println("删除成功")
}
return return
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment