From 1eb65ca668740e07081d3997c7e57e84ceaaa112 Mon Sep 17 00:00:00 2001 From: LidolLxf <13660354959@163.com> Date: Thu, 10 Oct 2024 10:52:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B5=84=E6=BA=90=E8=A7=86=E5=9B=BE?= =?UTF-8?q?=E8=A1=A8=E5=8D=95=20cronjob=20=E8=A1=A8=E8=BE=BE=E5=BC=8F?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=AD=A3=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pkg/i18n/locale/lc_msgs.yaml | 2 + .../pkg/resource/form/renderer/schema.go | 17 +++++ .../pkg/resource/form/renderer/schema_test.go | 72 +++++++++++++++++++ .../renderer/testdata/formdata/workload.go | 6 +- .../resource/form/tmpl/schema/workload.tpl | 1 + 5 files changed, 95 insertions(+), 3 deletions(-) diff --git a/bcs-services/cluster-resources/pkg/i18n/locale/lc_msgs.yaml b/bcs-services/cluster-resources/pkg/i18n/locale/lc_msgs.yaml index cdb17f9b7b..5909da5553 100644 --- a/bcs-services/cluster-resources/pkg/i18n/locale/lc_msgs.yaml +++ b/bcs-services/cluster-resources/pkg/i18n/locale/lc_msgs.yaml @@ -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: 标签有重复的键,请检查 diff --git a/bcs-services/cluster-resources/pkg/resource/form/renderer/schema.go b/bcs-services/cluster-resources/pkg/resource/form/renderer/schema.go index d3a0075f11..92e6e71a42 100644 --- a/bcs-services/cluster-resources/pkg/resource/form/renderer/schema.go +++ b/bcs-services/cluster-resources/pkg/resource/form/renderer/schema.go @@ -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 格式"), + }, } } diff --git a/bcs-services/cluster-resources/pkg/resource/form/renderer/schema_test.go b/bcs-services/cluster-resources/pkg/resource/form/renderer/schema_test.go index 12ec40eb5c..050fc35b29 100644 --- a/bcs-services/cluster-resources/pkg/resource/form/renderer/schema_test.go +++ b/bcs-services/cluster-resources/pkg/resource/form/renderer/schema_test.go @@ -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" @@ -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"))) + +} diff --git a/bcs-services/cluster-resources/pkg/resource/form/renderer/testdata/formdata/workload.go b/bcs-services/cluster-resources/pkg/resource/form/renderer/testdata/formdata/workload.go index 144027afe7..051f61de18 100644 --- a/bcs-services/cluster-resources/pkg/resource/form/renderer/testdata/formdata/workload.go +++ b/bcs-services/cluster-resources/pkg/resource/form/renderer/testdata/formdata/workload.go @@ -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, @@ -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, @@ -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, diff --git a/bcs-services/cluster-resources/pkg/resource/form/tmpl/schema/workload.tpl b/bcs-services/cluster-resources/pkg/resource/form/tmpl/schema/workload.tpl index 1e11b2cb2f..6ac66b4de2 100644 --- a/bcs-services/cluster-resources/pkg/resource/form/tmpl/schema/workload.tpl +++ b/bcs-services/cluster-resources/pkg/resource/form/tmpl/schema/workload.tpl @@ -454,6 +454,7 @@ jobManage: ui:rules: - required - maxLength64 + - scheduleValid concurrencyPolicy: title: {{ i18n "并发策略" .lang }} type: string