Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
so-operation-api
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
smart-operation
so-operation-api
Commits
2c63f3a1
Commit
2c63f3a1
authored
Jul 21, 2023
by
李科
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: 预警规则设置同步至PrometheusRule
parent
58c8e0c3
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
94 additions
and
35 deletions
+94
-35
src/controller/alert_rules.go
src/controller/alert_rules.go
+1
-1
src/service/alert_rules.go
src/service/alert_rules.go
+55
-5
src/service/k8s/prometheusrule.go
src/service/k8s/prometheusrule.go
+4
-5
src/service/prometheusrule.go
src/service/prometheusrule.go
+34
-24
No files found.
src/controller/alert_rules.go
View file @
2c63f3a1
...
...
@@ -17,7 +17,7 @@ func AddAlertRules(c *gin.Context) {
return
}
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
)}
...
...
src/service/alert_rules.go
View file @
2c63f3a1
...
...
@@ -108,7 +108,7 @@ func (a *AlertRulesSvc) Add(req request.AddAlertRules) (err error) {
return
nil
}
func
(
a
*
AlertRulesSvc
)
Update
(
req
request
.
UpdateAlertRules
)
error
{
func
(
a
*
AlertRulesSvc
)
Update
(
req
request
.
UpdateAlertRules
)
(
err
error
)
{
db
,
err
:=
client
.
GetDbClient
()
if
err
!=
nil
{
err
=
resp
.
DbConnectError
.
WithError
(
err
)
...
...
@@ -184,10 +184,32 @@ func (a *AlertRulesSvc) Update(req request.UpdateAlertRules) error {
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
}
func
(
a
*
AlertRulesSvc
)
UpdateIsEnabled
(
req
request
.
UpdateIsEnabledAlertRules
)
error
{
func
(
a
*
AlertRulesSvc
)
UpdateIsEnabled
(
req
request
.
UpdateIsEnabledAlertRules
)
(
err
error
)
{
db
,
err
:=
client
.
GetDbClient
()
if
err
!=
nil
{
err
=
resp
.
DbConnectError
.
WithError
(
err
)
...
...
@@ -200,10 +222,31 @@ func (a *AlertRulesSvc) UpdateIsEnabled(req request.UpdateIsEnabledAlertRules) e
UpdatedBy
:
a
.
User
.
SystemAccount
,
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
()
defer
session
.
Close
()
_
,
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) {
return
}
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
))
// TODO 删除普罗米修斯规则
if
err
!=
nil
{
return
}
}
else
{
return
errors
.
New
(
"alert_rules_id already exists in opensearch"
)
}
...
...
src/service/k8s/prometheusrule.go
View file @
2c63f3a1
...
...
@@ -7,7 +7,7 @@ import (
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/util"
"strings"
v1
"github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
monitoring
v1
"github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
)
var
(
...
...
@@ -41,7 +41,7 @@ type PrometheusRule struct {
Header
map
[
string
]
string
}
func
(
p
PrometheusRule
)
Create
(
pRule
*
v1
.
PrometheusRule
)
error
{
func
(
p
PrometheusRule
)
Create
(
pRule
*
monitoring
v1
.
PrometheusRule
)
error
{
k8sSvc
:=
K8sSvc
{
Header
:
p
.
Header
}
c
:=
&
Content
{
Kind
:
PrometheusRuleKind
,
ApiVersion
:
PrometheusRuleApiVersion
,
Metadata
:
pRule
.
ObjectMeta
,
Spec
:
pRule
.
Spec
}
_
,
err
:=
k8sSvc
.
SendFile
(
c
)
...
...
@@ -54,7 +54,7 @@ func (p PrometheusRule) Delete(namespace string, name string) error {
return
err
}
func
(
p
PrometheusRule
)
Update
(
pRule
*
v1
.
PrometheusRule
)
error
{
func
(
p
PrometheusRule
)
Update
(
pRule
*
monitoring
v1
.
PrometheusRule
)
error
{
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
)
p
.
Header
[
"Content-Type"
]
=
"application/json"
...
...
@@ -62,8 +62,7 @@ func (p PrometheusRule) Update(pRule *v1.PrometheusRule) error {
return
err
}
func
(
p
PrometheusRule
)
Get
(
namespace
string
,
name
string
)
(
obj
*
v1
.
PrometheusRule
,
err
error
)
{
// http://awecloud-rest/awecloud/rest/kubernetes/api/v1/_raw/prometheusrules.monitoring.coreos.com/namespace/kube-public/name/nginx
func
(
p
PrometheusRule
)
Get
(
namespace
string
,
name
string
)
(
obj
*
monitoringv1
.
PrometheusRule
,
err
error
)
{
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
)
if
err
!=
nil
{
...
...
src/service/prometheusrule.go
View file @
2c63f3a1
...
...
@@ -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/service/k8s"
"go.uber.org/zap"
v1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"log"
"strings"
)
...
...
@@ -20,11 +20,14 @@ type PrometheusRuleSvc struct {
}
func
(
p
*
PrometheusRuleSvc
)
Create
(
data
response
.
AlertRulesItem
)
(
err
error
)
{
pr
:=
monitoringv1
.
PrometheusRule
{}
prometheusRuleName
:=
k8s
.
GetPrometheusRuleName
(
data
.
Id
)
pr
.
Name
=
prometheusRuleName
pr
.
Namespace
=
k8s
.
Namespace
pr
.
Labels
=
k8s
.
AlertDefLabels
pr
:=
monitoringv1
.
PrometheusRule
{
ObjectMeta
:
v1
.
ObjectMeta
{
Name
:
prometheusRuleName
,
Namespace
:
k8s
.
Namespace
,
Labels
:
k8s
.
AlertDefLabels
,
},
}
group
:=
monitoringv1
.
RuleGroup
{}
groupInterval
:=
monitoringv1
.
Duration
(
fmt
.
Sprintf
(
"%d%s"
,
data
.
CheckPeriod
,
"m"
))
...
...
@@ -80,35 +83,42 @@ func (p *PrometheusRuleSvc) Create(data response.AlertRulesItem) (err error) {
group
.
Rules
=
append
(
group
.
Rules
,
rule
)
}
pr
.
Spec
.
Groups
=
append
(
pr
.
Spec
.
Groups
,
group
)
header
:=
make
(
map
[
string
]
string
)
header
[
"Authorization"
]
=
"Bearer "
+
conf
.
Options
.
KubernetesToken
header
:=
map
[
string
]
string
{
"Authorization"
:
"Bearer "
+
conf
.
Options
.
KubernetesToken
}
prSvc
:=
k8s
.
PrometheusRule
{
Header
:
header
}
conf
.
Logger
.
Info
(
"
创建规则
"
,
zap
.
Any
(
"pr"
,
pr
))
conf
.
Logger
.
Info
(
"
pr
"
,
zap
.
Any
(
"pr"
,
pr
))
err
=
prSvc
.
Create
(
&
pr
)
if
err
!=
nil
{
log
.
Println
(
"添加失败"
+
err
.
Error
())
}
else
{
log
.
Println
(
"添加成功"
)
return
}
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
}
func
(
p
*
PrometheusRuleSvc
)
Delete
(
data
response
.
AlertRulesItem
)
(
err
error
)
{
pr
:=
monitoringv1
.
PrometheusRule
{}
prometheusRuleName
:=
k8s
.
GetPrometheusRuleName
(
data
.
Id
)
pr
.
Name
=
prometheusRuleName
pr
.
Namespace
=
k8s
.
Namespace
pr
.
Labels
=
k8s
.
AlertDefLabels
pr
:=
monitoringv1
.
PrometheusRule
{
ObjectMeta
:
v1
.
ObjectMeta
{
Name
:
prometheusRuleName
,
Namespace
:
k8s
.
Namespace
,
Labels
:
k8s
.
AlertDefLabels
,
},
}
header
:=
make
(
map
[
string
]
string
)
header
[
"Authorization"
]
=
"Bearer "
+
conf
.
Options
.
KubernetesToken
header
:=
map
[
string
]
string
{
"Authorization"
:
"Bearer "
+
conf
.
Options
.
KubernetesToken
}
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
)
if
err
!=
nil
{
fmt
.
Println
(
"删除失败"
+
err
.
Error
())
}
else
{
fmt
.
Println
(
"删除成功"
)
}
return
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment