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: 新增获取模板文件关联labels接口 #3599

Merged
merged 3 commits into from
Dec 5, 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 @@ -15,8 +15,10 @@ package templateversion

import (
"context"
"errors"
"sort"

"github.com/Tencent/bk-bcs/bcs-common/pkg/odm/drivers"
"github.com/Tencent/bk-bcs/bcs-common/pkg/odm/operator"
"github.com/coreos/go-semver/semver"

Expand Down Expand Up @@ -127,6 +129,35 @@ func (t *TemplateVersionAction) GetContent(ctx context.Context, templateSpace, t
return templateVersion[0].ToMap(), nil
}

// GetTemplateAssociateLabels xxx
func (t *TemplateVersionAction) GetTemplateAssociateLabels(
ctx context.Context, in *clusterRes.GetTemplateAssociateLabelsReq) (map[string]interface{}, error) {

if err := t.checkAccess(ctx); err != nil {
return nil, err
}

p, err := project.FromContext(ctx)
if err != nil {
return nil, err
}

templateVersion, err := t.model.GetTemplateVersion(ctx, in.Id)
if err != nil {
if errors.Is(err, drivers.ErrTableRecordNotFound) {
return map[string]interface{}{}, nil
}
return nil, err
}

// 只能查看当前项目的版本
if templateVersion.ProjectCode != p.Code {
return nil, errorx.New(errcode.NoPerm, i18n.GetMsg(ctx, "无权限访问"))
}

return parser.GetLablesFromManifest(templateVersion.Content, in.Kind, in.AssociateName), nil
}

// List xxx
func (t *TemplateVersionAction) List(
ctx context.Context, templateID string) ([]map[string]interface{}, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,20 @@ func (h *Handler) GetTemplateVersion(
return nil
}

// GetTemplateAssociateLabels 获取模板文件关联labels
func (h *Handler) GetTemplateAssociateLabels(
ctx context.Context, in *clusterRes.GetTemplateAssociateLabelsReq, out *clusterRes.CommonResp) error {
action := templateversion.NewTemplateVersionAction(h.model)
data, err := action.GetTemplateAssociateLabels(ctx, in)
if err != nil {
return err
}
if out.Data, err = pbstruct.Map2pbStruct(data); err != nil {
return err
}
return nil
}

// GetTemplateContent 获取模板文件详情
func (h *Handler) GetTemplateContent(
ctx context.Context, in *clusterRes.GetTemplateContentReq, out *clusterRes.CommonResp) error {
Expand Down
4 changes: 4 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 @@ -492,6 +492,10 @@
en: LabelSelector
- msgID: "若没有设置选择器,则不会自动创建 Endpoints,需要手动创建"
en: "if no set label selector, endpoints will not be created automatically and need to be created manually"
- msgID: 关联资源
en: AssociatedResources
- msgID: 关联应用
en: AssociatedApplications
- msgID: "Session 亲和性"
en: SessionAffinity
- msgID: 最大会话停留时间
Expand Down
48 changes: 46 additions & 2 deletions bcs-services/cluster-resources/pkg/resource/form/parser/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"gopkg.in/yaml.v2"

"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/action"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/common/errcode"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/i18n"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/errorx"
Expand Down Expand Up @@ -63,8 +64,15 @@ func SplitManifests(bigFile string) []string {

// SimpleHead defines what the structure of the head of a manifest file
type SimpleHead struct {
Version string `yaml:"apiVersion"`
Kind string `yaml:"kind,omitempty"`
Version string `yaml:"apiVersion"`
Kind string `yaml:"kind,omitempty"`
Metadata Metadata `yaml:"metadata"`
}

// Metadata defines what the structure of the metadata of a manifest file
type Metadata struct {
Name string `yaml:"name"`
Labels map[string]interface{}
}

// GetManifestMetadata 获取 Manifest metadata
Expand All @@ -88,3 +96,39 @@ func GetResourceTypesFromManifest(manifest string) []string {
}
return slice.RemoveDuplicateValues(resourceType)
}

// GetLablesFromManifest get labels from manifest
func GetLablesFromManifest(manifest string, kind, associateName string) map[string]interface{} {
resp := make([]map[string]interface{}, 0)
manifests := SplitManifests(manifest)
for _, v := range manifests {
metadata := GetManifestMetadata(v)
if kind != metadata.Kind {
continue
}
// 筛选关联资源名称列表
if associateName == "" {
resp = append(resp, map[string]interface{}{
"label": metadata.Metadata.Name,
"value": metadata.Metadata.Name,
"disabled": false,
"tips": "",
})
continue
}
// 筛选关联应用的labels
if associateName == metadata.Metadata.Name {
for kk, vv := range metadata.Metadata.Labels {
resp = append(resp, map[string]interface{}{
"key": kk,
"value": vv,
})
}
}
}

if associateName != "" {
return map[string]interface{}{action.FormDataFormat: resp}
}
return map[string]interface{}{action.SelectItemsFormat: resp}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
prop: ports
prop: portConf
- - group:
- - group:
- [ "."]
container:
grid-template-columns: "1fr auto"
prop: associatedResources
- - group:
- [ "."]
container:
grid-template-columns: "1fr auto"
prop: associatedApplications
- - group:
- [ "key", "value", "." ]
container:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,46 @@ portConf:
selector:
title: {{ i18n "选择器" .lang }}
type: object
required:
- associatedResources
- associatedApplications
- labels
properties:
associatedResources:
title: {{ i18n "关联资源" .lang }}
type: string
default: Deployment
ui:component:
name: select
props:
clearable: false
datasource:
- label: Deployment
value: Deployment
- label: StatefulSet
value: StatefulSet
associatedApplications:
title: {{ i18n "关联应用" .lang }}
type: string
ui:component:
name: select
props:
clearable: false
remoteConfig:
url: "{{`{{`}} `${$context.baseUrl}/projects/${$context.projectID}/template/${$context.versionID}/labels?kind=${$self.getValue('spec.selector.associatedResources')}` {{`}}`}}"
ui:reactions:
- lifetime: init
then:
actions:
- "{{`{{`}} $loadDataSource {{`}}`}}"
- source: "spec.selector.associatedResources"
then:
state:
value: ""
actions:
- "{{`{{`}} $loadDataSource {{`}}`}}"
ui:rules:
- required
labels:
title: {{ i18n "标签选择器" .lang }}
type: array
Expand Down
Loading
Loading