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: 模板文件部署annotation 注入版本和模板名,同时资源视图列表增加版本和模板名解析 #3310

Merged
merged 2 commits into from
Jun 26, 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 @@ -167,8 +167,8 @@ func selectZoneAvailableSubnet(vpcId string, zoneIpCnt map[string]int,

blog.Infof("selectZoneAvailableSubnet vpc[%s] zone[%s] begin to allocate subnet", vpcId, zone)
subnetName, errLocal := getVpcNextSubnetName(vpcId, zone, "", opt)
if err != nil {
return nil, err
if errLocal != nil {
return nil, errLocal
}

// default allocate 256 ips
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func FilterNodesByDataDisk(instanceIDs []string, opt *cloudprovider.CommonOption
return filter, nil
}

// nolint : type `subnetIpNum` is unused
type subnetIpNum struct {
subnetId string
cnt uint64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func checkIfWhiteImageOsNames(opt *cloudprovider.ClusterGroupOption) bool {

func clusterSupportNodeNum(tkeCls *tke.Cluster, cluster *proto.Cluster) (uint32, uint32, uint32) {
var (
ipNum uint32 = 0
ipNum uint32
clusterCidrNum uint32
)
cidrs, err := business.GetCidrsFromCluster(tkeCls)
Expand Down
9 changes: 6 additions & 3 deletions bcs-services/cluster-resources/pkg/action/template/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ func buildTemplateSetsAnnotation(templates []*clusterRes.TemplateID) string {
}

// parseMultiTemplateFileVar parse template file variables from multiple templates
func parseMultiTemplateFileVar(templates []string) []string {
func parseMultiTemplateFileVar(templates []entity.TemplateDeploy) []string {
vars := make([]string, 0)
for _, template := range templates {
vars = append(vars, parseTemplateFileVar(template)...)
vars = append(vars, parseTemplateFileVar(template.Content)...)
}
return vars
}
Expand Down Expand Up @@ -118,7 +118,8 @@ func replaceTemplateFileVar(template string, values map[string]string) string {
}

// patchTemplateAnnotations patch template annotations
func patchTemplateAnnotations(manifest map[string]interface{}, username string) map[string]interface{} {
func patchTemplateAnnotations(
manifest map[string]interface{}, username, templateName, templateVersion string) map[string]interface{} {
annos := mapx.GetMap(manifest, "metadata.annotations")
if len(annos) == 0 {
_ = mapx.SetItems(manifest, "metadata.annotations", map[string]interface{}{})
Expand All @@ -130,6 +131,8 @@ func patchTemplateAnnotations(manifest map[string]interface{}, username string)
_ = mapx.SetItems(manifest, []string{"metadata", "annotations", resCsts.UpdaterAnnoKey}, username)
}
_ = mapx.SetItems(manifest, []string{"metadata", "annotations", resCsts.TemplateSourceType}, "template")
_ = mapx.SetItems(manifest, []string{"metadata", "annotations", resCsts.TemplateNameAnnoKey}, templateName)
_ = mapx.SetItems(manifest, []string{"metadata", "annotations", resCsts.TemplateVersionAnnoKey}, templateVersion)
return manifest
}

Expand Down
21 changes: 13 additions & 8 deletions bcs-services/cluster-resources/pkg/action/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ func (t *TemplateAction) CreateTemplateSet(ctx context.Context, req *clusterRes.
}

func getTemplateContents(ctx context.Context, model store.ClusterResourcesModel, versions []string,
projectCode string) ([]string, error) {
templates := make([]string, 0)
projectCode string) ([]entity.TemplateDeploy, error) {
templates := make([]entity.TemplateDeploy, 0)
for _, v := range versions {
vv, err := model.GetTemplateVersion(ctx, v)
if err != nil {
Expand All @@ -379,7 +379,11 @@ func getTemplateContents(ctx context.Context, model store.ClusterResourcesModel,
if vv.ProjectCode != projectCode {
return nil, errorx.New(errcode.NoPerm, i18n.GetMsg(ctx, "无权限访问"))
}
templates = append(templates, vv.Content)
templates = append(templates, entity.TemplateDeploy{
TemplateName: vv.TemplateName,
TemplateVersion: vv.Version,
Content: vv.Content,
})
}
return templates, nil
}
Expand Down Expand Up @@ -528,19 +532,20 @@ func (t *TemplateAction) DeployTemplateFile(ctx context.Context, req *clusterRes
}

// renderTemplates render templates
func (t *TemplateAction) renderTemplates(ctx context.Context, templates []string, vars map[string]string, ns string) (
[]map[string]interface{}, error) {
func (t *TemplateAction) renderTemplates(ctx context.Context, templates []entity.TemplateDeploy,
vars map[string]string, ns string) ([]map[string]interface{}, error) {
manifests := make([]map[string]interface{}, 0)
for i := range templates {
templates[i] = replaceTemplateFileVar(templates[i], vars)
mm := parser.SplitManifests(templates[i])
templates[i].Content = replaceTemplateFileVar(templates[i].Content, vars)
mm := parser.SplitManifests(templates[i].Content)
for _, v := range mm {
manifest := map[string]interface{}{}
if errr := yaml.Unmarshal([]byte(v), &manifest); errr != nil {
return nil, errr
}
manifest = mapx.CleanUpMap(manifest)
manifest = patchTemplateAnnotations(manifest, ctxkey.GetUsernameFromCtx(ctx))
manifest = patchTemplateAnnotations(
manifest, ctxkey.GetUsernameFromCtx(ctx), templates[i].TemplateName, templates[i].TemplateVersion)
// patch ns
kind := mapx.GetStr(manifest, "kind")
if mapx.GetStr(manifest, "metadata.namespace") != "" || isNSRequired(kind) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ const (
// UpdaterAnnoKey 更新者,为保持与 bcs-ui 中的一致,还是使用 updator(typo)
UpdaterAnnoKey = "io.tencent.paas.updator"

// TemplateNameAnnoKey 模板名称annotation key
TemplateNameAnnoKey = "io.tencent.paas.template_name"
// TemplateVersionAnnoKey 模板版本annotation key
TemplateVersionAnnoKey = "io.tencent.paas.template_version"

// TemplateSourceType 模板来源
TemplateSourceType = "io.tencent.paas.source_type"

Expand Down
10 changes: 6 additions & 4 deletions bcs-services/cluster-resources/pkg/resource/formatter/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ func CommonFormatRes(manifest map[string]interface{}) map[string]interface{} {
"editMode": mapx.Get(
manifest, []string{"metadata", "annotations", resCsts.EditModeAnnoKey}, resCsts.EditModeYaml,
),
"creator": mapx.GetStr(manifest, []string{"metadata", "annotations", resCsts.CreatorAnnoKey}),
"updater": mapx.GetStr(manifest, []string{"metadata", "annotations", resCsts.UpdaterAnnoKey}),
"immutable": immutable,
"createSource": createSource,
"creator": mapx.GetStr(manifest, []string{"metadata", "annotations", resCsts.CreatorAnnoKey}),
"updater": mapx.GetStr(manifest, []string{"metadata", "annotations", resCsts.UpdaterAnnoKey}),
"immutable": immutable,
"createSource": createSource,
"templateName": mapx.GetStr(manifest, []string{"metadata", "annotations", resCsts.TemplateNameAnnoKey}),
"templateVersion": mapx.GetStr(manifest, []string{"metadata", "annotations", resCsts.TemplateVersionAnnoKey}),
}
return ret
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ func (r VersionsSortByVersion) Less(i, j int) bool {

// Swap xxx
func (r VersionsSortByVersion) Swap(i, j int) { r[i], r[j] = r[j], r[i] }

// TemplateDeploy 定义了模板部署的一些标识
type TemplateDeploy struct {
TemplateName string `json:"templateName"`
TemplateVersion string `json:"templateVersion"`
Content string `json:"content"`
}
Loading