Skip to content

Commit

Permalink
feat: 新增获取模板文件关联labels接口
Browse files Browse the repository at this point in the history
  • Loading branch information
LidolLxf committed Nov 8, 2024
1 parent 3c417b6 commit 02ff5f7
Show file tree
Hide file tree
Showing 12 changed files with 3,746 additions and 3,043 deletions.
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)
// 筛选关联应用的labels
if associateName != "" {
if associateName == metadata.Metadata.Name && kind == metadata.Kind {
for kk, vv := range metadata.Metadata.Labels {
resp = append(resp, map[string]interface{}{
"key": kk,
"value": vv,
})
}
}
continue
}

if kind == metadata.Kind {
resp = append(resp, map[string]interface{}{
"label": metadata.Metadata.Name,
"value": metadata.Metadata.Name,
"disabled": false,
"tips": "",
})

}
}

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/labels/${$context.versionID}?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

0 comments on commit 02ff5f7

Please sign in to comment.