Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
302000a6
K
kubesphere
项目概览
水淹萌龙
/
kubesphere
与 Fork 源项目一致
Fork自
KubeSphere / kubesphere
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kubesphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
302000a6
编写于
3月 05, 2021
作者:
J
junotx
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
tweak some variables and comments to alerting bulk
Signed-off-by:
N
junotx
<
junotx@126.com
>
上级
c9243154
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
40 addition
and
40 deletion
+40
-40
pkg/api/alerting/v2alpha1/types.go
pkg/api/alerting/v2alpha1/types.go
+2
-2
pkg/models/alerting/alerting.go
pkg/models/alerting/alerting.go
+3
-3
pkg/models/alerting/rules/ruler.go
pkg/models/alerting/rules/ruler.go
+35
-35
未找到文件。
pkg/api/alerting/v2alpha1/types.go
浏览文件 @
302000a6
...
@@ -475,8 +475,8 @@ type BulkResponse struct {
...
@@ -475,8 +475,8 @@ type BulkResponse struct {
Items
[]
*
BulkItemResponse
`json:"items" description:"It contains the result of each operation in the bulk request"`
Items
[]
*
BulkItemResponse
`json:"items" description:"It contains the result of each operation in the bulk request"`
}
}
//
Neaten neaten
s the internal items and sets the errors
//
MakeBulkResponse tidie
s the internal items and sets the errors
func
(
br
*
BulkResponse
)
Neaten
()
*
BulkResponse
{
func
(
br
*
BulkResponse
)
MakeBulkResponse
()
*
BulkResponse
{
var
(
var
(
items
[]
*
BulkItemResponse
items
[]
*
BulkItemResponse
itemMap
=
make
(
map
[
string
]
*
BulkItemResponse
)
itemMap
=
make
(
map
[
string
]
*
BulkItemResponse
)
...
...
pkg/models/alerting/alerting.go
浏览文件 @
302000a6
...
@@ -665,7 +665,7 @@ func (o *operator) CreateOrUpdateCustomAlertingRules(ctx context.Context, namesp
...
@@ -665,7 +665,7 @@ func (o *operator) CreateOrUpdateCustomAlertingRules(ctx context.Context, namesp
}
}
}
}
if
len
(
nameSet
)
==
len
(
invalids
)
{
if
len
(
nameSet
)
==
len
(
invalids
)
{
return
br
.
Neaten
(),
nil
return
br
.
MakeBulkResponse
(),
nil
}
}
// Confirm whether the rules should be added or updated. For each rule that is committed,
// Confirm whether the rules should be added or updated. For each rule that is committed,
...
@@ -733,7 +733,7 @@ func (o *operator) CreateOrUpdateCustomAlertingRules(ctx context.Context, namesp
...
@@ -733,7 +733,7 @@ func (o *operator) CreateOrUpdateCustomAlertingRules(ctx context.Context, namesp
}
}
}
}
}
}
return
br
.
Neaten
(),
nil
return
br
.
MakeBulkResponse
(),
nil
}
}
func
(
o
*
operator
)
DeleteCustomAlertingRules
(
ctx
context
.
Context
,
namespace
string
,
func
(
o
*
operator
)
DeleteCustomAlertingRules
(
ctx
context
.
Context
,
namespace
string
,
...
@@ -797,7 +797,7 @@ func (o *operator) DeleteCustomAlertingRules(ctx context.Context, namespace stri
...
@@ -797,7 +797,7 @@ func (o *operator) DeleteCustomAlertingRules(ctx context.Context, namespace stri
}
}
br
.
Items
=
append
(
br
.
Items
,
respItems
...
)
br
.
Items
=
append
(
br
.
Items
,
respItems
...
)
return
br
.
Neaten
(),
nil
return
br
.
MakeBulkResponse
(),
nil
}
}
// getPrometheusRuler gets the cluster-in prometheus
// getPrometheusRuler gets the cluster-in prometheus
...
...
pkg/models/alerting/rules/ruler.go
浏览文件 @
302000a6
...
@@ -157,15 +157,15 @@ func (r *ruleResource) addAlertingRules(rules ...*RuleWithGroup) (bool, error) {
...
@@ -157,15 +157,15 @@ func (r *ruleResource) addAlertingRules(rules ...*RuleWithGroup) (bool, error) {
cursor
int
// indicates which rule to start adding for the rules with no groups
cursor
int
// indicates which rule to start adding for the rules with no groups
rulesNoGroup
[]
promresourcesv1
.
Rule
// rules that do not specify group names
unGroupedRules
[]
promresourcesv1
.
Rule
// rules that do not specify group names
rulesWithGroup
=
make
(
map
[
string
][]
promresourcesv1
.
Rule
)
// rules that have specific group names
groupedRules
=
make
(
map
[
string
][]
promresourcesv1
.
Rule
)
// rules that have specific group names
)
)
for
i
,
rule
:=
range
rules
{
for
i
,
rule
:=
range
rules
{
if
len
(
strings
.
TrimSpace
(
rule
.
Group
))
==
0
{
if
len
(
strings
.
TrimSpace
(
rule
.
Group
))
==
0
{
rulesNoGroup
=
append
(
rulesNoGroup
,
rules
[
i
]
.
Rule
)
unGroupedRules
=
append
(
unGroupedRules
,
rules
[
i
]
.
Rule
)
}
else
{
}
else
{
rulesWithGroup
[
rule
.
Group
]
=
append
(
rulesWithGroup
[
rule
.
Group
],
rules
[
i
]
.
Rule
)
groupedRules
[
rule
.
Group
]
=
append
(
groupedRules
[
rule
.
Group
],
rules
[
i
]
.
Rule
)
}
}
}
}
...
@@ -177,25 +177,25 @@ func (r *ruleResource) addAlertingRules(rules ...*RuleWithGroup) (bool, error) {
...
@@ -177,25 +177,25 @@ func (r *ruleResource) addAlertingRules(rules ...*RuleWithGroup) (bool, error) {
// For the rules that do not specify group names, add them to the automatically generated groups until the limit is reached.
// For the rules that do not specify group names, add them to the automatically generated groups until the limit is reached.
for
i
,
g
:=
range
spec
.
Groups
{
for
i
,
g
:=
range
spec
.
Groups
{
var
(
var
(
gName
=
g
.
Name
gName
=
g
.
Name
doneNoGroup
=
cursor
>=
len
(
rulesNoGroup
)
// whether all rules without groups have been added
unGroupedRulesDrained
=
cursor
>=
len
(
unGroupedRules
)
// whether all rules without groups have been added
doneWithGroup
=
len
(
rulesWithGroup
)
==
0
// whether all rules with groups have been added
groupedRulesDrained
=
len
(
groupedRules
)
==
0
// whether all rules with groups have been added
)
)
if
doneNoGroup
&&
doneWithGroup
{
if
unGroupedRulesDrained
&&
groupedRulesDrained
{
break
break
}
}
if
!
doneWithGroup
{
if
!
groupedRulesDrained
{
if
_
,
ok
:=
rulesWithGroup
[
gName
];
ok
{
if
_
,
ok
:=
groupedRules
[
gName
];
ok
{
spec
.
Groups
[
i
]
.
Rules
=
append
(
spec
.
Groups
[
i
]
.
Rules
,
rulesWithGroup
[
gName
]
...
)
spec
.
Groups
[
i
]
.
Rules
=
append
(
spec
.
Groups
[
i
]
.
Rules
,
groupedRules
[
gName
]
...
)
delete
(
rulesWithGroup
,
gName
)
delete
(
groupedRules
,
gName
)
commit
=
true
commit
=
true
}
}
}
}
g
=
spec
.
Groups
[
i
]
g
=
spec
.
Groups
[
i
]
if
!
doneNoGroup
&&
strings
.
HasPrefix
(
gName
,
customRuleGroupDefaultPrefix
)
{
if
!
unGroupedRulesDrained
&&
strings
.
HasPrefix
(
gName
,
customRuleGroupDefaultPrefix
)
{
suf
,
err
:=
strconv
.
Atoi
(
strings
.
TrimPrefix
(
gName
,
customRuleGroupDefaultPrefix
))
suf
,
err
:=
strconv
.
Atoi
(
strings
.
TrimPrefix
(
gName
,
customRuleGroupDefaultPrefix
))
if
err
!=
nil
{
if
err
!=
nil
{
continue
continue
...
@@ -207,10 +207,10 @@ func (r *ruleResource) addAlertingRules(rules ...*RuleWithGroup) (bool, error) {
...
@@ -207,10 +207,10 @@ func (r *ruleResource) addAlertingRules(rules ...*RuleWithGroup) (bool, error) {
if
size
:=
len
(
g
.
Rules
);
size
<
customRuleGroupSize
{
if
size
:=
len
(
g
.
Rules
);
size
<
customRuleGroupSize
{
num
:=
customRuleGroupSize
-
size
num
:=
customRuleGroupSize
-
size
var
stop
int
var
stop
int
if
stop
=
cursor
+
num
;
stop
>
len
(
rulesNoGroup
)
{
if
stop
=
cursor
+
num
;
stop
>
len
(
unGroupedRules
)
{
stop
=
len
(
rulesNoGroup
)
stop
=
len
(
unGroupedRules
)
}
}
spec
.
Groups
[
i
]
.
Rules
=
append
(
spec
.
Groups
[
i
]
.
Rules
,
rulesNoGroup
[
cursor
:
stop
]
...
)
spec
.
Groups
[
i
]
.
Rules
=
append
(
spec
.
Groups
[
i
]
.
Rules
,
unGroupedRules
[
cursor
:
stop
]
...
)
cursor
=
stop
cursor
=
stop
commit
=
true
commit
=
true
}
}
...
@@ -218,8 +218,8 @@ func (r *ruleResource) addAlertingRules(rules ...*RuleWithGroup) (bool, error) {
...
@@ -218,8 +218,8 @@ func (r *ruleResource) addAlertingRules(rules ...*RuleWithGroup) (bool, error) {
}
}
// If no groups are available, new groups will be created to place the remaining rules.
// If no groups are available, new groups will be created to place the remaining rules.
for
gName
:=
range
rulesWithGroup
{
for
gName
:=
range
groupedRules
{
rules
:=
rulesWithGroup
[
gName
]
rules
:=
groupedRules
[
gName
]
if
len
(
rules
)
==
0
{
if
len
(
rules
)
==
0
{
continue
continue
}
}
...
@@ -229,10 +229,10 @@ func (r *ruleResource) addAlertingRules(rules ...*RuleWithGroup) (bool, error) {
...
@@ -229,10 +229,10 @@ func (r *ruleResource) addAlertingRules(rules ...*RuleWithGroup) (bool, error) {
for
groupMax
++
;
cursor
<
len
(
rules
);
groupMax
++
{
for
groupMax
++
;
cursor
<
len
(
rules
);
groupMax
++
{
g
:=
promresourcesv1
.
RuleGroup
{
Name
:
fmt
.
Sprintf
(
"%s%d"
,
customRuleGroupDefaultPrefix
,
groupMax
)}
g
:=
promresourcesv1
.
RuleGroup
{
Name
:
fmt
.
Sprintf
(
"%s%d"
,
customRuleGroupDefaultPrefix
,
groupMax
)}
var
stop
int
var
stop
int
if
stop
=
cursor
+
customRuleGroupSize
;
stop
>
len
(
rulesNoGroup
)
{
if
stop
=
cursor
+
customRuleGroupSize
;
stop
>
len
(
unGroupedRules
)
{
stop
=
len
(
rulesNoGroup
)
stop
=
len
(
unGroupedRules
)
}
}
g
.
Rules
=
append
(
g
.
Rules
,
rulesNoGroup
[
cursor
:
stop
]
...
)
g
.
Rules
=
append
(
g
.
Rules
,
unGroupedRules
[
cursor
:
stop
]
...
)
spec
.
Groups
=
append
(
spec
.
Groups
,
g
)
spec
.
Groups
=
append
(
spec
.
Groups
,
g
)
cursor
=
stop
cursor
=
stop
commit
=
true
commit
=
true
...
@@ -504,7 +504,7 @@ func (r *ThanosRuler) addAlertingRules(ctx context.Context, ruleNamespace *corev
...
@@ -504,7 +504,7 @@ func (r *ThanosRuler) addAlertingRules(ctx context.Context, ruleNamespace *corev
rs
[]
*
RuleWithGroup
rs
[]
*
RuleWithGroup
)
)
// If adding the rules to the new resource exceeds the limit,
// If adding the rules to the new resource exceeds the limit,
// reduce the amount to 1/2, 1/3... of rest rules until
it
can accommodate.
// reduce the amount to 1/2, 1/3... of rest rules until
the new resource
can accommodate.
for
i
:=
1
;
;
i
++
{
for
i
:=
1
;
;
i
++
{
stop
=
cursor
+
num
/
i
stop
=
cursor
+
num
/
i
rs
=
rules
[
cursor
:
stop
]
rs
=
rules
[
cursor
:
stop
]
...
@@ -546,11 +546,11 @@ func (r *ThanosRuler) UpdateAlertingRules(ctx context.Context, ruleNamespace *co
...
@@ -546,11 +546,11 @@ func (r *ThanosRuler) UpdateAlertingRules(ctx context.Context, ruleNamespace *co
itemsMap
=
make
(
map
[
string
][]
*
ResourceRuleItem
)
itemsMap
=
make
(
map
[
string
][]
*
ResourceRuleItem
)
respItems
=
make
([]
*
v2alpha1
.
BulkItemResponse
,
0
,
len
(
ruleItems
))
respItems
=
make
([]
*
v2alpha1
.
BulkItemResponse
,
0
,
len
(
ruleItems
))
// rules updated successfully. The key is the rule name.
// rules updated successfully. The key is the rule name.
successes
=
make
(
map
[
string
]
struct
{})
rulesUpdated
=
make
(
map
[
string
]
struct
{})
// rules to be moved to other resources. The key is the resource name in which the rules
wer
e.
// rules to be moved to other resources. The key is the resource name in which the rules
resid
e.
moveMap
=
make
(
map
[
string
][]
*
ResourceRuleItem
)
rulesToMove
=
make
(
map
[
string
][]
*
ResourceRuleItem
)
// duplicate rules to be deleted
// duplicate rules to be deleted
delMap
=
make
(
map
[
string
][]
*
ResourceRuleItem
)
rulesToDelete
=
make
(
map
[
string
][]
*
ResourceRuleItem
)
)
)
for
i
,
item
:=
range
ruleItems
{
for
i
,
item
:=
range
ruleItems
{
...
@@ -569,8 +569,8 @@ func (r *ThanosRuler) UpdateAlertingRules(ctx context.Context, ruleNamespace *co
...
@@ -569,8 +569,8 @@ func (r *ThanosRuler) UpdateAlertingRules(ctx context.Context, ruleNamespace *co
for
i
:=
range
items
{
for
i
:=
range
items
{
item
:=
items
[
i
]
item
:=
items
[
i
]
if
_
,
ok
:=
successes
[
item
.
Alert
];
ok
{
if
_
,
ok
:=
rulesUpdated
[
item
.
Alert
];
ok
{
delMap
[
name
]
=
append
(
delMap
[
name
],
item
)
rulesToDelete
[
name
]
=
append
(
rulesToDelete
[
name
],
item
)
continue
continue
}
}
nrules
=
append
(
nrules
,
&
item
.
RuleWithGroup
)
nrules
=
append
(
nrules
,
&
item
.
RuleWithGroup
)
...
@@ -595,11 +595,11 @@ func (r *ThanosRuler) UpdateAlertingRules(ctx context.Context, ruleNamespace *co
...
@@ -595,11 +595,11 @@ func (r *ThanosRuler) UpdateAlertingRules(ctx context.Context, ruleNamespace *co
switch
{
switch
{
case
err
==
nil
:
case
err
==
nil
:
for
_
,
item
:=
range
items
{
for
_
,
item
:=
range
items
{
successes
[
item
.
Alert
]
=
struct
{}{}
rulesUpdated
[
item
.
Alert
]
=
struct
{}{}
respItems
=
append
(
respItems
,
v2alpha1
.
NewBulkItemSuccessResponse
(
item
.
Alert
,
v2alpha1
.
ResultUpdated
))
respItems
=
append
(
respItems
,
v2alpha1
.
NewBulkItemSuccessResponse
(
item
.
Alert
,
v2alpha1
.
ResultUpdated
))
}
}
case
err
==
errOutOfConfigMapSize
:
// Cannot update the rules in the original resource
case
err
==
errOutOfConfigMapSize
:
// Cannot update the rules in the original resource
moveMap
[
name
]
=
append
(
moveMap
[
name
],
nitems
...
)
rulesToMove
[
name
]
=
append
(
rulesToMove
[
name
],
nitems
...
)
case
resourceNotFound
(
err
)
:
case
resourceNotFound
(
err
)
:
for
_
,
item
:=
range
items
{
for
_
,
item
:=
range
items
{
respItems
=
append
(
respItems
,
&
v2alpha1
.
BulkItemResponse
{
respItems
=
append
(
respItems
,
&
v2alpha1
.
BulkItemResponse
{
...
@@ -616,9 +616,9 @@ func (r *ThanosRuler) UpdateAlertingRules(ctx context.Context, ruleNamespace *co
...
@@ -616,9 +616,9 @@ func (r *ThanosRuler) UpdateAlertingRules(ctx context.Context, ruleNamespace *co
}
}
// The move here is not really move, because the move also requires an update.
// The move here is not really move, because the move also requires an update.
//
So the actual operations are firstly add the new rules in other resources
//
What really happens is that the new rules will be added in other resources first,
// and then
delete the old rules in old
resources.
// and then
the old rules will be deleted from the original
resources.
for
name
,
items
:=
range
moveMap
{
for
name
,
items
:=
range
rulesToMove
{
var
(
var
(
nrules
=
make
([]
*
RuleWithGroup
,
0
,
len
(
items
))
nrules
=
make
([]
*
RuleWithGroup
,
0
,
len
(
items
))
nitems
=
make
(
map
[
string
]
*
ResourceRuleItem
,
len
(
items
))
nitems
=
make
(
map
[
string
]
*
ResourceRuleItem
,
len
(
items
))
...
@@ -646,7 +646,7 @@ func (r *ThanosRuler) UpdateAlertingRules(ctx context.Context, ruleNamespace *co
...
@@ -646,7 +646,7 @@ func (r *ThanosRuler) UpdateAlertingRules(ctx context.Context, ruleNamespace *co
switch
resp
.
Status
{
switch
resp
.
Status
{
case
v2alpha1
.
StatusSuccess
:
case
v2alpha1
.
StatusSuccess
:
if
item
,
ok
:=
nitems
[
resp
.
RuleName
];
ok
{
if
item
,
ok
:=
nitems
[
resp
.
RuleName
];
ok
{
delMap
[
name
]
=
append
(
delMap
[
name
],
item
)
rulesToDelete
[
name
]
=
append
(
rulesToDelete
[
name
],
item
)
}
}
default
:
default
:
respItems
=
append
(
respItems
,
resp
)
respItems
=
append
(
respItems
,
resp
)
...
@@ -654,7 +654,7 @@ func (r *ThanosRuler) UpdateAlertingRules(ctx context.Context, ruleNamespace *co
...
@@ -654,7 +654,7 @@ func (r *ThanosRuler) UpdateAlertingRules(ctx context.Context, ruleNamespace *co
}
}
}
}
for
_
,
items
:=
range
delMap
{
for
_
,
items
:=
range
rulesToDelete
{
dRespItems
,
err
:=
r
.
DeleteAlertingRules
(
ctx
,
ruleNamespace
,
items
...
)
dRespItems
,
err
:=
r
.
DeleteAlertingRules
(
ctx
,
ruleNamespace
,
items
...
)
if
err
!=
nil
{
if
err
!=
nil
{
for
_
,
item
:=
range
items
{
for
_
,
item
:=
range
items
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录