Skip to content

Commit

Permalink
feat: 资源视图表单 cronjob 表达式支持正则
Browse files Browse the repository at this point in the history
  • Loading branch information
LidolLxf committed Oct 10, 2024
1 parent 5cd9e2e commit 1eb65ca
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 3 deletions.
2 changes: 2 additions & 0 deletions bcs-services/cluster-resources/pkg/i18n/locale/lc_msgs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
en: "must start and end with alphanumeric, can contain '-', '_', '.' and alphanumeric"
- msgID: "不应少于 1 个项"
en: "Should not be less than 1 item."
- msgID: "格式无效,请参考 Cron 格式"
en: "Invalid format, please refer to Cron format."
- msgID: "资源类型 %s APIVersion %s 不在受支持的版本列表 %v 中,请改用 Yaml 模式而非表单化"
en: "resource kind %s apiVersion %s not in the list of supported versions %v, please use yaml rather than form edit mode"
- msgID: 标签有重复的键,请检查
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,22 @@ func genSchemaRules(ctx context.Context) map[string]interface{} {
"validator": "{{ $self.value.length > 0 }}",
"message": i18n.GetMsg(ctx, "不应少于 1 个项"),
},
"scheduleValid": map[string]interface{}{
"validator": `/^(\*|([0-5]?\d)(,[0-5]?\d)*|([0-5]?\d)-([0-5]?\d))(\/([0-5]?\d)(,[0-5]?\d)*)?\s+` +
`(\*|([01]?\d|2[0-3])(,[01]?\d|2[0-3])*|([01]?\d|2[0-3])-` +
`([01]?\d|2[0-3]))(\/([01]?\d|2[0-3])(,[01]?\d|2[0-3])*)?\s+` +
`(\*|(0?[1-9]|[12][0-9]|3[01])(,(0?[1-9]|[12][0-9]|3[01]))*|` +
`(0?[1-9]|[12][0-9]|3[01])-(0?[1-9]|[12][0-9]|3[01])|\?)` +
`(\/(0?[1-9]|[12][0-9]|3[01])(,(0?[1-9]|[12][0-9]|3[01]))*)?\s+` +
`(\*|(0?[1-9]|1[0-2]|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)` +
`(,(0?[1-9]|1[0-2]|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))*|` +
`(0?[1-9]|1[0-2]|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)-` +
`(0?[1-9]|1[0-2]|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))` +
`(\/((0?[1-9]|1[0-2])(,((0?[1-9]|1[0-2]|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)))*))?\s+` +
`(\*|([0-6]|SUN|MON|TUE|WED|THU|FRI|SAT)(,[0-6]|SUN|MON|TUE|WED|THU|FRI|SAT)*|` +
`([0-6]|SUN|MON|TUE|WED|THU|FRI|SAT)-([0-6]|SUN|MON|TUE|WED|THU|FRI|SAT)|\?)` +
`(\/([0-6](,([0-6]|SUN|MON|TUE|WED|THU|FRI|SAT))*))?$/`,
"message": i18n.GetMsg(ctx, "格式无效,请参考 Cron 格式"),
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ package renderer

import (
"context"
"fmt"
"regexp"
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/common/ctxkey"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/common/envs"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/common/runmode"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/common/runtime"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/i18n"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/resource/form/validator"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/schema"
Expand Down Expand Up @@ -64,3 +69,70 @@ func TestSchemaRenderer(t *testing.T) {
assert.Nil(t, err)
}
}

func TestGenSchemaRulesScheduleValid(t *testing.T) {
// 不打印其他
runtime.RunMode = runmode.UnitTest
rules := genSchemaRules(context.Background())
validator, ok := rules["scheduleValid"].(map[string]interface{})["validator"].(string)
if !ok {
t.Errorf("schedule validator is null")
return
}
validator = strings.Trim(validator, "/")
testSample := []string{
// valid schudule cron
"* * * * *",
"1 * * * *",
"1,2 * * * *",
"1/1,2 * * * *",
"1-2/1,2 * * * *",

"* 1 * * *",
"* 1,2 * * *",
"* 1/1,2 * * *",
"* 1-2/1,2 * * *",

"* * 1 * *",
"* * 1,2 * *",
"* * 1/1,2 * *",
"* * 1-2/1,2 * *",
"* * ?/1,2 * *",

"* * * 1 *",
"* * * 1,2 *",
"* * * 1/1,2 *",
"* * * 1-2/1,2 *",
"* * * JAN-2/1,2 *",
"* * * JAN-2/1,FEB *",

"* * * * 1",
"* * * * 1,2",
"* * * * 1/1,2",
"* * * * 1-2/1,2",
"* * * * SUN-2/1,2",
"* * * * SAT-2/1,SUN",

"1-2/1,2 23/1,2 ?/1,2 JAN/1,DEC SUN/0,SAT",

// invalid schudule cron
"1/1-2 * * * *",
"* * L * *",
"* * W * *",
"* * * * L",
"* * * * W",
"* * * ?/1,2 *",
"1-2/1,2 23/1,2 ?/1,2 JAN/JAN,DEC SUN/0,SAT",
"1-2/1,2 23/1,2 ?/1,2 JAN/JAN,DEC SUN/SUN,SAT",
}
scheduleReg := regexp.MustCompile(validator)
var invalidSchedule []string
for _, v := range testSample {
if !scheduleReg.MatchString(v) {
invalidSchedule = append(invalidSchedule, v)
}
}

fmt.Println(fmt.Sprintf("invalid schedule cron:\n %s", strings.Join(invalidSchedule, "\n")))

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var DeployComplex = model.Deploy{
},
Spec: model.DeploySpec{
Replicas: model.DeployReplicas{
Cnt: 2,
Cnt: "2",
UpdateStrategy: resCsts.DefaultUpdateStrategy,
MaxSurge: 0,
MSUnit: util.UnitCnt,
Expand Down Expand Up @@ -76,7 +76,7 @@ var DeploySimple = model.Deploy{
},
Spec: model.DeploySpec{
Replicas: model.DeployReplicas{
Cnt: 2,
Cnt: "2",
UpdateStrategy: resCsts.DefaultUpdateStrategy,
MaxSurge: 1,
MSUnit: util.UnitCnt,
Expand Down Expand Up @@ -114,7 +114,7 @@ var STSComplex = model.STS{
Spec: model.STSSpec{
Replicas: model.STSReplicas{
SVCName: "svc-complex-y3xk1r9vg9",
Cnt: 2,
Cnt: "2",
UpdateStrategy: resCsts.DefaultUpdateStrategy,
PodManPolicy: "OrderedReady",
Partition: 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ jobManage:
ui:rules:
- required
- maxLength64
- scheduleValid
concurrencyPolicy:
title: {{ i18n "并发策略" .lang }}
type: string
Expand Down

0 comments on commit 1eb65ca

Please sign in to comment.