From ad69b08a757bff20e399ba7796f5175799be7b51 Mon Sep 17 00:00:00 2001 From: LiHui Date: Wed, 22 Sep 2021 15:31:42 +0800 Subject: [PATCH] add display fields Signed-off-by: LiHui --- pkg/models/openpitrix/utils.go | 14 ++++++++++++++ .../openpitrix/helmrepoindex/interface.go | 8 +++++++- .../openpitrix/helmrepoindex/load_package.go | 19 +++++++++++++++++++ pkg/utils/reposcache/repo_cahes.go | 15 +++++++++++---- 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/pkg/models/openpitrix/utils.go b/pkg/models/openpitrix/utils.go index cca74db47..f1a2a01fd 100644 --- a/pkg/models/openpitrix/utils.go +++ b/pkg/models/openpitrix/utils.go @@ -401,6 +401,18 @@ func convertAppVersion(in *v1alpha1.HelmApplicationVersion) *AppVersion { out.Icon = in.Spec.Icon } + // The field Maintainers and Sources were a string field, so I encode the helm field's maintainers and sources, + // which are array, to string. + if len(in.Spec.Maintainers) > 0 { + maintainers, _ := json.Marshal(in.Spec.Maintainers) + out.Maintainers = string(maintainers) + } + + if len(in.Spec.Sources) > 0 { + source, _ := json.Marshal(in.Spec.Sources) + out.Sources = string(source) + } + out.Status = in.State() out.Owner = in.GetCreator() out.Name = in.GetVersionName() @@ -565,6 +577,8 @@ func buildApplicationVersion(app *v1alpha1.HelmApplication, chrt helmrepoindex.V Icon: chrt.GetIcon(), Home: chrt.GetHome(), Description: stringutils.ShortenString(chrt.GetDescription(), v1alpha1.MsgLen), + Sources: chrt.GetRawSources(), + Maintainers: chrt.GetRawMaintainers(), }, Created: &t, // set data to nil before save app version to etcd diff --git a/pkg/simple/client/openpitrix/helmrepoindex/interface.go b/pkg/simple/client/openpitrix/helmrepoindex/interface.go index 14d3aceb7..c0cdf4961 100644 --- a/pkg/simple/client/openpitrix/helmrepoindex/interface.go +++ b/pkg/simple/client/openpitrix/helmrepoindex/interface.go @@ -16,7 +16,11 @@ limitations under the License. package helmrepoindex -import "time" +import ( + "time" + + "kubesphere.io/api/application/v1alpha1" +) type VersionInterface interface { GetName() string @@ -28,8 +32,10 @@ type VersionInterface interface { GetIcon() string GetHome() string GetSources() string + GetRawSources() []string GetKeywords() string GetMaintainers() string + GetRawMaintainers() []*v1alpha1.Maintainer GetScreenshots() string GetPackageName() string GetCreateTime() time.Time diff --git a/pkg/simple/client/openpitrix/helmrepoindex/load_package.go b/pkg/simple/client/openpitrix/helmrepoindex/load_package.go index e8a82a250..8fe35e164 100644 --- a/pkg/simple/client/openpitrix/helmrepoindex/load_package.go +++ b/pkg/simple/client/openpitrix/helmrepoindex/load_package.go @@ -23,6 +23,8 @@ import ( "strings" "time" + "kubesphere.io/api/application/v1alpha1" + "helm.sh/helm/v3/pkg/repo" "k8s.io/klog" @@ -64,10 +66,27 @@ func (h HelmVersionWrapper) GetSources() string { return string(s) } +func (h HelmVersionWrapper) GetRawSources() []string { + return h.Sources +} + func (h HelmVersionWrapper) GetKeywords() string { return strings.Join(h.ChartVersion.Keywords, ",") } +func (h HelmVersionWrapper) GetRawMaintainers() []*v1alpha1.Maintainer { + mt := make([]*v1alpha1.Maintainer, 0, len(h.Maintainers)) + for _, value := range h.Maintainers { + mt = append(mt, &v1alpha1.Maintainer{ + URL: value.URL, + Name: value.Name, + Email: value.Email, + }) + + } + return mt +} + func (h HelmVersionWrapper) GetMaintainers() string { if len(h.ChartVersion.Maintainers) == 0 { return "" diff --git a/pkg/utils/reposcache/repo_cahes.go b/pkg/utils/reposcache/repo_cahes.go index 81a9b8d68..e12ee44d3 100644 --- a/pkg/utils/reposcache/repo_cahes.go +++ b/pkg/utils/reposcache/repo_cahes.go @@ -273,7 +273,7 @@ func (c *cachedRepos) addRepo(repo *v1alpha1.HelmRepo, builtin bool) error { // build all the versions of this app for _, chartVersion := range app.Charts { chartsCount += 1 - + hvw := helmrepoindex.HelmVersionWrapper{ChartVersion: &chartVersion.ChartVersion} appVerName = chartVersion.ApplicationVersionId version := &v1alpha1.HelmApplicationVersion{ ObjectMeta: metav1.ObjectMeta{ @@ -287,9 +287,9 @@ func (c *cachedRepos) addRepo(repo *v1alpha1.HelmRepo, builtin bool) error { }, Spec: v1alpha1.HelmApplicationVersionSpec{ Metadata: &v1alpha1.Metadata{ - Name: chartVersion.Name, - AppVersion: chartVersion.AppVersion, - Version: chartVersion.Version, + Name: hvw.GetName(), + AppVersion: hvw.GetAppVersion(), + Version: hvw.GetVersion(), }, URLs: chartVersion.URLs, Digest: chartVersion.Digest, @@ -299,6 +299,13 @@ func (c *cachedRepos) addRepo(repo *v1alpha1.HelmRepo, builtin bool) error { State: v1alpha1.StateActive, }, } + + // It is not necessary to store these pieces of information when this is not a built-in repo. + if helmrepoindex.IsBuiltInRepo(repo.Name) { + version.Spec.Sources = hvw.GetRawSources() + version.Spec.Maintainers = hvw.GetRawMaintainers() + version.Spec.Home = hvw.GetHome() + } c.versions[chartVersion.ApplicationVersionId] = version // Find the latest version.