Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
cdbce887
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看板
提交
cdbce887
编写于
1月 27, 2021
作者:
J
junotx
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
enhance the alerting rule validation
Signed-off-by:
N
junotx
<
junotx@126.com
>
上级
3a0e0891
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
42 addition
and
3 deletion
+42
-3
pkg/api/alerting/v2alpha1/types.go
pkg/api/alerting/v2alpha1/types.go
+42
-3
未找到文件。
pkg/api/alerting/v2alpha1/types.go
浏览文件 @
cdbce887
...
...
@@ -17,6 +17,7 @@ limitations under the License.
package
v2alpha1
import
(
"context"
"regexp"
"sort"
"strconv"
...
...
@@ -26,7 +27,9 @@ import (
"github.com/emicklei/go-restful"
"github.com/pkg/errors"
prommodel
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/timestamp"
"github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/template"
utilerrors
"k8s.io/apimachinery/pkg/util/errors"
)
...
...
@@ -41,6 +44,9 @@ var (
ErrAlertingRuleAlreadyExists
=
errors
.
New
(
"The alerting rule already exists"
)
ErrAlertingAPIV2NotEnabled
=
errors
.
New
(
"The alerting v2 API was not enabled"
)
templateTestData
=
template
.
AlertTemplateData
(
map
[
string
]
string
{},
map
[
string
]
string
{},
0
)
templateTestTextPrefix
=
"{{$labels := .Labels}}{{$externalLabels := .ExternalLabels}}{{$value := .Value}}"
ruleLabelNameMatcher
=
regexp
.
MustCompile
(
`[a-zA-Z_][a-zA-Z0-9_]*`
)
)
...
...
@@ -66,7 +72,10 @@ func (r *PostableAlertingRule) Validate() error {
if
r
.
Name
==
""
{
errs
=
append
(
errs
,
errors
.
New
(
"name can not be empty"
))
}
if
_
,
err
:=
parser
.
ParseExpr
(
r
.
Query
);
err
!=
nil
{
if
r
.
Query
==
""
{
errs
=
append
(
errs
,
errors
.
New
(
"query can not be empty"
))
}
else
if
_
,
err
:=
parser
.
ParseExpr
(
r
.
Query
);
err
!=
nil
{
errs
=
append
(
errs
,
errors
.
Wrapf
(
err
,
"query is invalid: %s"
,
r
.
Query
))
}
if
r
.
Duration
!=
""
{
...
...
@@ -75,12 +84,42 @@ func (r *PostableAlertingRule) Validate() error {
}
}
parseTest
:=
func
(
text
string
)
error
{
tmpl
:=
template
.
NewTemplateExpander
(
context
.
TODO
(),
templateTestTextPrefix
+
text
,
"__alert_"
+
r
.
Name
,
templateTestData
,
prommodel
.
Time
(
timestamp
.
FromTime
(
time
.
Now
())),
nil
,
nil
,
)
return
tmpl
.
ParseTest
()
}
if
len
(
r
.
Labels
)
>
0
{
for
name
,
_
:=
range
r
.
Labels
{
if
!
ruleLabelNameMatcher
.
MatchString
(
name
)
||
strings
.
HasPrefix
(
name
,
"__"
)
{
for
name
,
v
:=
range
r
.
Labels
{
if
!
prommodel
.
LabelName
(
name
)
.
IsValid
(
)
||
strings
.
HasPrefix
(
name
,
"__"
)
{
errs
=
append
(
errs
,
errors
.
Errorf
(
"label name (%s) is not valid. The name must match [a-zA-Z_][a-zA-Z0-9_]* and has not the __ prefix (label names with this prefix are for internal use)"
,
name
))
}
if
!
prommodel
.
LabelValue
(
v
)
.
IsValid
()
{
errs
=
append
(
errs
,
errors
.
Errorf
(
"invalid label value: %s"
,
v
))
}
if
err
:=
parseTest
(
v
);
err
!=
nil
{
errs
=
append
(
errs
,
errors
.
Errorf
(
"invalid label value: %s"
,
v
))
}
}
}
if
len
(
r
.
Annotations
)
>
0
{
for
name
,
v
:=
range
r
.
Annotations
{
if
!
prommodel
.
LabelName
(
name
)
.
IsValid
()
{
errs
=
append
(
errs
,
errors
.
Errorf
(
"invalid annotation name: %s"
,
v
))
}
if
err
:=
parseTest
(
v
);
err
!=
nil
{
errs
=
append
(
errs
,
errors
.
Errorf
(
"invalid annotation value: %s"
,
v
))
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录