diff --git a/pkg/kapis/openpitrix/v1/handler.go b/pkg/kapis/openpitrix/v1/handler.go index 9ac9c4980..761b88f93 100644 --- a/pkg/kapis/openpitrix/v1/handler.go +++ b/pkg/kapis/openpitrix/v1/handler.go @@ -713,24 +713,19 @@ func (h *openpitrixHandler) ListApplications(req *restful.Request, resp *restful clusterName := req.PathParameter("cluster") namespace := req.PathParameter("namespace") workspace := req.PathParameter("workspace") - orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime) - reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false) + orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.StatusTime) conditions, err := params.ParseConditions(req) - - if offset < 0 { - offset = 0 - } - - if limit <= 0 { - limit = 10 - } - if err != nil { klog.V(4).Infoln(err) api.HandleBadRequest(resp, nil, err) return } + reverse := false + if conditions.Match[openpitrix.Ascending] == "true" { + reverse = true + } + result, err := h.openpitrix.ListApplications(workspace, clusterName, namespace, conditions, limit, offset, orderBy, reverse) if err != nil { diff --git a/pkg/models/openpitrix/applicationversions.go b/pkg/models/openpitrix/applicationversions.go index c134d4a6e..2f6d6cac6 100644 --- a/pkg/models/openpitrix/applicationversions.go +++ b/pkg/models/openpitrix/applicationversions.go @@ -161,13 +161,72 @@ func (c *applicationOperator) ModifyAppVersion(id string, request *ModifyAppVers versionCopy := version.DeepCopy() spec := &versionCopy.Spec - if request.Name != nil && *request.Name != "" { - spec.Version, spec.AppVersion = parseChartVersionName(*request.Name) + + // extract information from chart package + if len(request.Package) > 0 { + if version.Status.State != v1alpha1.StateDraft { + return actionNotPermitted + } + // 1. Parse the chart package + chart, err := helmrepoindex.LoadPackage(request.Package) + if err != nil { + klog.Errorf("load package failed, error: %s", err) + return err + } + + // chart name must match with the original one + if spec.Name != chart.GetName() { + return fmt.Errorf("chart name not match, current name: %s, original name: %s", chart.GetName(), spec.Name) + } + + // new version name + if chart.GetVersionName() != version.GetVersionName() { + existsVersion, err := c.getAppVersionByVersionName(version.GetHelmApplicationId(), chart.GetVersionName()) + if err != nil { + return err + } + if existsVersion != nil { + return appVersionItemExists + } + } + + // 2. update crd info + spec.Version = chart.GetVersion() + spec.AppVersion = chart.GetAppVersion() + spec.Icon = chart.GetIcon() + spec.Home = chart.GetHome() + spec.Description = stringutils.ShortenString(chart.GetDescription(), v1alpha1.MsgLen) + + now := metav1.Now() + spec.Created = &now + + // 3. save chart data to s3 storage, just overwrite the legacy data + err = c.backingStoreClient.Upload(dataKeyInStorage(versionCopy.GetWorkspace(), versionCopy.Name), versionCopy.Name, bytes.NewReader(request.Package)) + if err != nil { + klog.Errorf("upload chart for app version: %s/%s failed, error: %s", versionCopy.GetWorkspace(), + versionCopy.GetTrueName(), err) + return uploadChartDataFailed + } else { + klog.V(4).Infof("chart data uploaded for app version: %s/%s", versionCopy.GetWorkspace(), versionCopy.GetTrueName()) + } + } else { + // new version name + if request.Name != nil && *request.Name != "" && version.GetVersionName() != *request.Name { + spec.Version, spec.AppVersion = parseChartVersionName(*request.Name) + existsVersion, err := c.getAppVersionByVersionName(version.GetHelmApplicationId(), *request.Name) + if err != nil { + return err + } + if existsVersion != nil { + return appVersionItemExists + } + } + + if request.Description != nil && *request.Description != "" { + spec.Description = stringutils.ShortenString(*request.Description, v1alpha1.MsgLen) + } } - if request.Description != nil && *request.Description != "" { - spec.Description = stringutils.ShortenString(*request.Description, v1alpha1.MsgLen) - } patch := client.MergeFrom(version) data, err := patch.Data(versionCopy) if err != nil { @@ -386,28 +445,37 @@ func (c *applicationOperator) DoAppVersionAction(versionId string, request *Acti return nil } -// Create helmApplicationVersion and helmAudit -func (c *applicationOperator) createApplicationVersion(ver *v1alpha1.HelmApplicationVersion) (*v1alpha1.HelmApplicationVersion, error) { +func (c *applicationOperator) getAppVersionByVersionName(appId, verName string) (*v1alpha1.HelmApplicationVersion, error) { ls := map[string]string{ - constants.ChartApplicationIdLabelKey: ver.GetHelmApplicationId(), + constants.ChartApplicationIdLabelKey: appId, } - list, err := c.versionLister.List(labels.SelectorFromSet(ls)) + versions, err := c.versionLister.List(labels.SelectorFromSet(ls)) if err != nil && !apierrors.IsNotFound(err) { return nil, err } - if len(list) > 0 { - verName := ver.GetVersionName() - for _, v := range list { - if verName == v.GetVersionName() { - klog.V(2).Infof("helm application version: %s exist", verName) - return nil, appVersionItemExists - } + for _, ver := range versions { + if verName == ver.GetVersionName() { + return ver, nil } } + return nil, nil +} + +// Create helmApplicationVersion and helmAudit +func (c *applicationOperator) createApplicationVersion(ver *v1alpha1.HelmApplicationVersion) (*v1alpha1.HelmApplicationVersion, error) { + existsVersion, err := c.getAppVersionByVersionName(ver.GetHelmApplicationId(), ver.GetVersionName()) + if err != nil { + return nil, err + } + if existsVersion != nil { + klog.V(2).Infof("helm application version: %s exist", ver.GetVersionName()) + return nil, appVersionItemExists + } + // save chart data to s3 storage _, err = base64.StdEncoding.Decode(ver.Spec.Data, ver.Spec.Data) if err != nil { diff --git a/pkg/models/openpitrix/types.go b/pkg/models/openpitrix/types.go index 169c23b62..3bff4d2ef 100644 --- a/pkg/models/openpitrix/types.go +++ b/pkg/models/openpitrix/types.go @@ -909,5 +909,6 @@ const ( ActionReject = "reject" ActionSubmit = "submit" ActionRelease = "release" + Ascending = "ascending" ActionIndex = "index" ) diff --git a/pkg/models/openpitrix/utils.go b/pkg/models/openpitrix/utils.go index 1b4d63753..78399cfd5 100644 --- a/pkg/models/openpitrix/utils.go +++ b/pkg/models/openpitrix/utils.go @@ -376,11 +376,16 @@ func convertAppVersion(in *v1alpha1.HelmApplicationVersion) *AppVersion { out.CreateTime = &date if len(in.Status.Audit) > 0 { t = in.Status.Audit[0].Time.Time - updateDate := strfmt.DateTime(t) - out.UpdateTime = &updateDate + changeTime := strfmt.DateTime(t) + out.StatusTime = &changeTime } else { - out.UpdateTime = &date + out.StatusTime = &date } + + // chart create time or update time + updateTime := strfmt.DateTime(in.Spec.Created.Time) + out.UpdateTime = &updateTime + if in.Spec.Metadata != nil { out.Description = in.Spec.Description out.Icon = in.Spec.Icon @@ -389,6 +394,7 @@ func convertAppVersion(in *v1alpha1.HelmApplicationVersion) *AppVersion { out.Status = in.State() out.Owner = in.GetCreator() out.Name = in.GetVersionName() + out.PackageName = fmt.Sprintf("%s-%s.tgz", in.GetTrueName(), in.GetChartVersion()) out.VersionId = in.GetHelmApplicationVersionId() return &out }