update chart data

Signed-off-by: LiHui <andrewli@yunify.com>
This commit is contained in:
LiHui
2021-04-08 10:54:44 +08:00
parent 64fc89212f
commit c023fb13a0
2 changed files with 93 additions and 19 deletions

View File

@@ -159,13 +159,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 {
@@ -380,28 +439,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 {

View File

@@ -373,11 +373,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
@@ -386,6 +391,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
}