Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 资源视图表单 cronjob 表达式支持正则 #3540

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1035,9 +1035,10 @@ func getSharedCluster(projectId string, bizId string, model store.ClusterManager
continue
}
// 是否共享给当前项目/业务
if clusterList[i].SharedRanges != nil && ((len(clusterList[i].SharedRanges.GetProjectIdOrCodes()) > 0 && utils.StringContainInSlice(projectId,
clusterList[i].SharedRanges.ProjectIdOrCodes)) ||
(len(clusterList[i].SharedRanges.GetBizs()) > 0 && utils.StringContainInSlice(bizId, clusterList[i].SharedRanges.GetBizs()))) {
if clusterList[i].SharedRanges != nil && ((len(clusterList[i].SharedRanges.GetProjectIdOrCodes()) > 0 &&
utils.StringContainInSlice(projectId, clusterList[i].SharedRanges.ProjectIdOrCodes)) ||
(len(clusterList[i].SharedRanges.GetBizs()) > 0 &&
utils.StringContainInSlice(bizId, clusterList[i].SharedRanges.GetBizs()))) {
clusters = append(clusters, shieldClusterInfo(&clusterList[i]))

continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
)

// ClusterInfo info
// nolint revive
type ClusterInfo struct {
ClusterName string
ClusterID string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package operationlog

import (
"context"

"github.com/Tencent/bk-bcs/bcs-common/common/blog"
"github.com/Tencent/bk-bcs/bcs-common/pkg/odm/operator"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ func (la *GetCustomSettingAction) Handle(ctx context.Context, req *cmproto.GetBa

// GetBizInstanceTopoAction action for get biz topo
type GetBizInstanceTopoAction struct {
ctx context.Context
req *cmproto.GetBizTopologyHostRequest
resp *cmproto.GetBizTopologyHostResponse
ctx context.Context
req *cmproto.GetBizTopologyHostRequest
resp *cmproto.GetBizTopologyHostResponse
}

// NewGetBizInstanceTopoAction create action
Expand Down
14 changes: 7 additions & 7 deletions bcs-services/bcs-cluster-manager/internal/remote/cmdb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,27 @@ var (
// Options for cmdb client
type Options struct {
// Enable enable client
Enable bool
Enable bool
// AppCode app code
AppCode string
AppCode string
// AppSecret app secret
AppSecret string
AppSecret string
// BKUserName bk username
BKUserName string
// Server server
Server string
Server string
// Debug debug
Debug bool
Debug bool
}

// AuthInfo auth user
type AuthInfo struct {
// BkAppCode bk app code
BkAppCode string `json:"bk_app_code"`
BkAppCode string `json:"bk_app_code"`
// BkAppSecret bk app secret
BkAppSecret string `json:"bk_app_secret"`
// BkUserName bk username
BkUserName string `json:"bk_username"`
BkUserName string `json:"bk_username"`
}

// Client for cc
Expand Down
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 @@ -204,7 +204,7 @@ func (r *ManifestRenderer) initTemplate() (err error) {
func (r *ManifestRenderer) render2Map() error {
// 渲染,转换并写入数据(模板名称格式:{r.kind}.yaml)
var buf bytes.Buffer
err := r.tmpl.ExecuteTemplate(&buf, r.kind+".yaml", r.formData)
err := r.tmpl.ExecuteTemplate(&buf, r.kind+".yaml", r.formData) // nolint goconst
if err != nil {
log.Warn(r.ctx, "failed to render template:%v", err)
return errorx.New(errcode.General, i18n.GetMsg(r.ctx, "渲染模板失败:%v"), err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func (r *SchemaRenderer) renderSubTypeTmpl2Map(dir string, ret interface{}) erro
}

// genSchemaRules 生成 JsonSchema 校验规则
// nolint lll
func genSchemaRules(ctx context.Context) map[string]interface{} {
return map[string]interface{}{
"required": map[string]interface{}{
Expand Down Expand Up @@ -206,5 +207,22 @@ func genSchemaRules(ctx context.Context) map[string]interface{} {
"validator": "{{ $self.value.length > 0 }}",
"message": i18n.GetMsg(ctx, "不应少于 1 个项"),
},
"scheduleValid": map[string]interface{}{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个正则表达式写个单测吧,顺便把 lint 修下

"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
Loading