From f35f2c661cde7588daf810feda53bf99cc165e0e Mon Sep 17 00:00:00 2001 From: LiHui Date: Tue, 29 Jun 2021 14:44:48 +0800 Subject: [PATCH] Parse display name from chart annotation Signed-off-by: LiHui --- cmd/import-app/import.go | 27 ++++++++++++++------------- pkg/constants/constants.go | 1 + urls.txt | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/cmd/import-app/import.go b/cmd/import-app/import.go index bb9a290a..4d3c8252 100644 --- a/cmd/import-app/import.go +++ b/cmd/import-app/import.go @@ -172,22 +172,22 @@ func (wf *ImportWorkFlow) CreateApp(ctx context.Context, chrt *chart.Chart) (app for ind := range appList.Items { item := &appList.Items[ind] - if item.GetTrueName() == wf.importConfig.ReplaceAppName(chrt.Name()) { + if item.GetTrueName() == wf.importConfig.ReplaceAppName(chrt) { klog.Infof("helm application exists, name: %s, version: %s", chrt.Name(), chrt.Metadata.Version) - err = wf.UpdateAppNameInStore(ctx, item, wf.importConfig.ReplaceAppName(chrt.Name())) + err = wf.UpdateAppNameInStore(ctx, item, wf.importConfig.ReplaceAppName(chrt)) if err != nil { return nil, err } return item, nil } else if item.GetTrueName() == chrt.Name() { // we need update app name - klog.Infof("chart name: %s, replace name: %s", chrt.Name(), wf.importConfig.ReplaceAppName(chrt.Name())) - app, err := wf.UpdateAppName(ctx, item, wf.importConfig.ReplaceAppName(chrt.Name())) + klog.Infof("chart name: %s, replace name: %s", chrt.Name(), wf.importConfig.ReplaceAppName(chrt)) + app, err := wf.UpdateAppName(ctx, item, wf.importConfig.ReplaceAppName(chrt)) if err != nil { return nil, err } // update app name in store - err = wf.UpdateAppNameInStore(ctx, item, wf.importConfig.ReplaceAppName(chrt.Name())) + err = wf.UpdateAppNameInStore(ctx, item, wf.importConfig.ReplaceAppName(chrt)) if err == nil { return app, err } else { @@ -229,7 +229,7 @@ func (wf *ImportWorkFlow) CreateApp(ctx context.Context, chrt *chart.Chart) (app }, }, Spec: v1alpha1.HelmApplicationSpec{ - Name: wf.importConfig.ReplaceAppName(chrt.Name()), + Name: wf.importConfig.ReplaceAppName(chrt), Description: chrt.Metadata.Description, Icon: chrt.Metadata.Icon, }, @@ -420,15 +420,16 @@ type ImportConfig struct { AppNameReplace map[string]string `yaml:"appNameReplace"` } -func (ic *ImportConfig) ReplaceAppName(oldName string) string { - if len(ic.AppNameReplace) == 0 { - return oldName - } - - if newName, exists := ic.AppNameReplace[oldName]; exists { +func (ic *ImportConfig) ReplaceAppName(chrt *chart.Chart) string { + // import-config.yaml comes first + if newName, exists := ic.AppNameReplace[chrt.Name()]; exists { return newName } else { - return oldName + // If app.kubesphere.io/display-name exists in chart's annotation, use this value. + if chrt.Metadata.Annotations != nil && chrt.Metadata.Annotations[constants.ChartDisplayName] != "" { + return strings.TrimSpace(chrt.Metadata.Annotations[constants.ChartDisplayName]) + } + return chrt.Name() } } diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index fc076dae..a6ee02ce 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -18,6 +18,7 @@ package constants const ( CategoryKeyInChart = "app.kubesphere.io/category" + ChartDisplayName = "app.kubesphere.io/display-name" ClusterNameLabelKey = "kubesphere.io/cluster" WorkspaceLabelKey = "kubesphere.io/workspace" NamespaceLabelKey = "kubesphere.io/namespace" diff --git a/urls.txt b/urls.txt index 82a6c690..46851998 100644 --- a/urls.txt +++ b/urls.txt @@ -14,4 +14,4 @@ https://charts.kubesphere.io/test/postgresql-0.4.2.tgz https://charts.kubesphere.io/test/rabbitmq-0.3.2.tgz https://charts.kubesphere.io/test/redis-0.3.5.tgz https://charts.kubesphere.io/test/radondb-mysql-1.0.0.tgz -https://charts.kubesphere.io/test/radondb-postgresql-1.0.0.tgz +https://charts.kubesphere.io/test/radondb-postgresql-1.0.1.tgz