diff --git a/pkg/controller/openpitrix/helmapplication/helm_application_controller.go b/pkg/controller/openpitrix/helmapplication/helm_application_controller.go index ce2bd8d950704d6a6b9376a561405ddfc2d90240..a33b4a040318c411f5e34f04451a45f5a72c53f4 100644 --- a/pkg/controller/openpitrix/helmapplication/helm_application_controller.go +++ b/pkg/controller/openpitrix/helmapplication/helm_application_controller.go @@ -52,7 +52,7 @@ const ( ) func (r *ReconcileHelmApplication) Reconcile(request reconcile.Request) (reconcile.Result, error) { - klog.V(4).Info("sync helm application") + klog.V(4).Infof("sync helm application: %s ", request.String()) rootCtx := context.Background() app := &v1alpha1.HelmApplication{} diff --git a/pkg/controller/openpitrix/helmapplication/helm_application_version_controller.go b/pkg/controller/openpitrix/helmapplication/helm_application_version_controller.go index 502f12ddd8e0fa1088aeee42015c304ebbd39191..7f063912d4cf8ad965aeb5129040933a5e46b55d 100644 --- a/pkg/controller/openpitrix/helmapplication/helm_application_version_controller.go +++ b/pkg/controller/openpitrix/helmapplication/helm_application_version_controller.go @@ -210,41 +210,42 @@ func (r *ReconcileHelmApplicationVersion) updateStatus(appVersion *v1alpha1.Helm return nil } +// getLatestVersionName get the latest version of versions. +// if inAppStore is false, get the latest version name of all of the versions +// if inAppStore is true, get the latest version name of the ACTIVE versions. func getLatestVersionName(versions v1alpha1.HelmApplicationVersionList, inAppStore bool) string { - l := versions.Items - if len(l) == 0 { + if len(versions.Items) == 0 { return "" } - verInd := 0 - if inAppStore { - // only check active app version - for ; verInd < len(l); verInd++ { - if l[verInd].Status.State == v1alpha1.StateActive { - break - } - } - } - - if verInd == len(l) { - return "" - } + var latestVersionName string + var latestSemver *semver.Version - latestSemver, _ := semver.NewVersion(l[verInd].GetSemver()) + for _, version := range versions.Items { + // If the appVersion is being deleted, ignore it. + // If inAppStore is true, we just need ACTIVE appVersion. + if version.DeletionTimestamp != nil || (inAppStore && version.Status.State != v1alpha1.StateActive) { + continue + } - for i := verInd + 1; i < len(l); i++ { - curr, _ := semver.NewVersion(l[i].GetSemver()) - if inAppStore { - if l[i].Status.State != v1alpha1.StateActive { - continue + currSemver, err := semver.NewVersion(version.GetSemver()) + if err == nil { + if latestSemver == nil { + // the first valid semver + latestSemver = currSemver + latestVersionName = version.GetVersionName() + } else if latestSemver.LessThan(currSemver) { + // find a newer valid semver + latestSemver = currSemver + latestVersionName = version.GetVersionName() } - } - if latestSemver.LessThan(curr) { - verInd = i + } else { + // If the semver is invalid, just ignore it. + klog.V(2).Infof("parse version failed, id: %s, err: %s", version.Name, err) } } - return l[verInd].GetVersionName() + return latestVersionName } func mergeApplicationVersionState(versions v1alpha1.HelmApplicationVersionList) string { @@ -257,7 +258,7 @@ func mergeApplicationVersionState(versions v1alpha1.HelmApplicationVersionList) } } - // If there is on active appVersion, the helm application is active + // If there is one or more active appVersion, the helm application is active if states[v1alpha1.StateActive] > 0 { return v1alpha1.StateActive } @@ -267,6 +268,7 @@ func mergeApplicationVersionState(versions v1alpha1.HelmApplicationVersionList) return v1alpha1.StateDraft } + // No active appVersion or draft appVersion, then the app state is suspended if states[v1alpha1.StateSuspended] > 0 { return v1alpha1.StateSuspended } diff --git a/pkg/models/openpitrix/utils.go b/pkg/models/openpitrix/utils.go index 9ca7c4cfece963ee52548d73f1e652545d942dee..6dc1341910c0995b530fccd1b1fc42f8e37d0a22 100644 --- a/pkg/models/openpitrix/utils.go +++ b/pkg/models/openpitrix/utils.go @@ -32,7 +32,6 @@ import ( "kubesphere.io/kubesphere/pkg/utils/stringutils" "path" "regexp" - "sort" "strings" "time" ) @@ -320,10 +319,16 @@ func convertApp(app *v1alpha1.HelmApplication, versions []*v1alpha1.HelmApplicat } else { out.CategorySet = AppCategorySet{} } - if versions != nil && len(versions) > 0 { - sort.Sort(AppVersions(versions)) - out.LatestAppVersion = convertAppVersion(versions[len(versions)-1]) - } else { + + for _, version := range versions { + if app.Status.LatestVersion == version.GetVersionName() { + // find the latest version, and convert its format + out.LatestAppVersion = convertAppVersion(version) + break + } + } + + if out.LatestAppVersion == nil { out.LatestAppVersion = &AppVersion{} }