applications.go 17.9 KB
Newer Older
Z
zryfish 已提交
1
/*
H
hongming 已提交
2 3 4 5 6 7 8 9 10 11 12
Copyright 2020 The KubeSphere Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
P
pengcong06 已提交
13

H
hongming 已提交
14
package openpitrix
Z
zryfish 已提交
15 16

import (
L
LiHui 已提交
17 18 19 20 21 22
	"bytes"
	"context"
	"errors"
	"fmt"
	apierrors "k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Z
zryfish 已提交
23
	"k8s.io/apimachinery/pkg/labels"
J
Jeff 已提交
24
	"k8s.io/klog"
L
LiHui 已提交
25 26 27 28 29 30
	"kubesphere.io/kubesphere/pkg/apis/application/v1alpha1"
	"kubesphere.io/kubesphere/pkg/client/clientset/versioned"
	v1alpha13 "kubesphere.io/kubesphere/pkg/client/clientset/versioned/typed/application/v1alpha1"
	"kubesphere.io/kubesphere/pkg/client/informers/externalversions"
	listers_v1alpha1 "kubesphere.io/kubesphere/pkg/client/listers/application/v1alpha1"
	"kubesphere.io/kubesphere/pkg/constants"
Z
zryfish 已提交
31
	"kubesphere.io/kubesphere/pkg/models"
J
Jeff 已提交
32
	"kubesphere.io/kubesphere/pkg/server/params"
L
LiHui 已提交
33 34 35 36 37 38 39
	"kubesphere.io/kubesphere/pkg/simple/client/openpitrix/helmrepoindex"
	"kubesphere.io/kubesphere/pkg/simple/client/s3"
	"kubesphere.io/kubesphere/pkg/utils/idutils"
	"kubesphere.io/kubesphere/pkg/utils/reposcache"
	"kubesphere.io/kubesphere/pkg/utils/stringutils"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sort"
Z
zryfish 已提交
40
	"strings"
L
LiHui 已提交
41
	"time"
Z
zryfish 已提交
42 43
)

H
hongming 已提交
44
type ApplicationInterface interface {
L
LiHui 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	ListApps(conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error)
	DescribeApp(id string) (*App, error)
	DeleteApp(id string) error
	CreateApp(req *CreateAppRequest) (*CreateAppResponse, error)
	ModifyApp(appId string, request *ModifyAppRequest) error
	DeleteAppVersion(id string) error
	ModifyAppVersion(id string, request *ModifyAppVersionRequest) error
	DescribeAppVersion(id string) (*AppVersion, error)
	CreateAppVersion(request *CreateAppVersionRequest) (*CreateAppVersionResponse, error)
	ValidatePackage(request *ValidatePackageRequest) (*ValidatePackageResponse, error)
	GetAppVersionPackage(appId, versionId string) (*GetAppVersionPackageResponse, error)
	DoAppAction(appId string, request *ActionRequest) error
	DoAppVersionAction(versionId string, request *ActionRequest) error
	ListAppVersionAudits(conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error)
	GetAppVersionFiles(versionId string, request *GetAppVersionFilesRequest) (*GetAppVersionPackageFilesResponse, error)
	ListAppVersionReviews(conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error)
	ListAppVersions(conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error)
Z
zryfish 已提交
62 63 64
}

type applicationOperator struct {
L
LiHui 已提交
65 66
	backingStoreClient s3.Interface
	informers          externalversions.SharedInformerFactory
Z
zryfish 已提交
67

L
LiHui 已提交
68 69
	appClient        v1alpha13.HelmApplicationInterface
	appVersionClient v1alpha13.HelmApplicationVersionInterface
Z
zryfish 已提交
70

L
LiHui 已提交
71 72
	appLister     listers_v1alpha1.HelmApplicationLister
	versionLister listers_v1alpha1.HelmApplicationVersionLister
Z
zryfish 已提交
73

L
LiHui 已提交
74 75 76
	repoLister listers_v1alpha1.HelmRepoLister
	ctgLister  listers_v1alpha1.HelmCategoryLister
	rlsLister  listers_v1alpha1.HelmReleaseLister
Z
zryfish 已提交
77

L
LiHui 已提交
78
	cachedRepos reposcache.ReposCache
P
pengcong06 已提交
79 80
}

L
LiHui 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
func newApplicationOperator(cached reposcache.ReposCache, informers externalversions.SharedInformerFactory, ksClient versioned.Interface, storeClient s3.Interface) ApplicationInterface {
	op := &applicationOperator{
		backingStoreClient: storeClient,
		informers:          informers,
		repoLister:         informers.Application().V1alpha1().HelmRepos().Lister(),

		appClient:        ksClient.ApplicationV1alpha1().HelmApplications(),
		appVersionClient: ksClient.ApplicationV1alpha1().HelmApplicationVersions(),

		appLister:     informers.Application().V1alpha1().HelmApplications().Lister(),
		versionLister: informers.Application().V1alpha1().HelmApplicationVersions().Lister(),

		ctgLister:   informers.Application().V1alpha1().HelmCategories().Lister(),
		rlsLister:   informers.Application().V1alpha1().HelmReleases().Lister(),
		cachedRepos: cached,
H
hongming 已提交
96
	}
L
LiHui 已提交
97 98 99 100 101 102 103

	return op
}

// save icon data and helm application
func (c *applicationOperator) createApp(app *v1alpha1.HelmApplication, iconData []byte) (*v1alpha1.HelmApplication, error) {
	exists, err := c.getHelmAppByName(app.GetWorkspace(), app.GetTrueName())
Z
zryfish 已提交
104
	if err != nil {
H
hongming 已提交
105
		return nil, err
Z
zryfish 已提交
106
	}
L
LiHui 已提交
107 108 109 110 111 112 113 114
	if exists != nil {
		return nil, appItemExists
	}

	if len(iconData) != 0 {
		// save icon attachment
		iconId := idutils.GetUuid(v1alpha1.HelmAttachmentPrefix)
		err = c.backingStoreClient.Upload(iconId, iconId, bytes.NewBuffer(iconData))
H
hongming 已提交
115
		if err != nil {
L
LiHui 已提交
116
			klog.Errorf("save icon attachment failed, error: %s", err)
H
hongming 已提交
117 118
			return nil, err
		}
L
LiHui 已提交
119
		app.Spec.Icon = iconId
Z
zryfish 已提交
120 121
	}

L
LiHui 已提交
122 123
	app, err = c.appClient.Create(context.TODO(), app, metav1.CreateOptions{})
	return app, err
Z
zryfish 已提交
124 125
}

L
LiHui 已提交
126 127 128 129 130 131 132 133 134
// get helm app by name in workspace
func (c *applicationOperator) getHelmAppByName(workspace, name string) (*v1alpha1.HelmApplication, error) {
	ls := map[string]string{
		constants.WorkspaceLabelKey: workspace,
	}

	list, err := c.appLister.List(labels.SelectorFromSet(ls))

	if err != nil && !apierrors.IsNotFound(err) {
H
hongming 已提交
135 136
		return nil, err
	}
L
LiHui 已提交
137 138 139 140 141 142 143

	if len(list) > 0 {
		for _, a := range list {
			if a.GetTrueName() == name {
				return a, nil
			}
		}
H
hongming 已提交
144
	}
L
LiHui 已提交
145 146 147 148 149 150 151 152 153 154

	return nil, nil
}

func (c *applicationOperator) ValidatePackage(request *ValidatePackageRequest) (*ValidatePackageResponse, error) {

	chrt, err := helmrepoindex.LoadPackage(request.VersionPackage)

	result := &ValidatePackageResponse{}

155
	if err != nil {
L
LiHui 已提交
156 157 158 159 160 161 162 163 164 165
		matchPackageFailedError(err, result)
		if (result.Error == "EOF" || result.Error == "") && len(result.ErrorDetails) == 0 {
			klog.Errorf("package parse failed, error: %s", err.Error())
			return nil, errors.New("package parse failed")
		}
	} else {
		result.Name = chrt.GetName()
		result.VersionName = chrt.GetVersionName()
		result.Description = chrt.GetDescription()
		result.URL = chrt.GetUrls()
H
hongming 已提交
166
	}
L
LiHui 已提交
167 168

	return result, nil
H
hongming 已提交
169 170
}

L
LiHui 已提交
171 172 173 174 175
func (c *applicationOperator) DoAppAction(appId string, request *ActionRequest) error {

	app, err := c.appLister.Get(appId)
	if err != nil {
		return err
P
pengcong06 已提交
176
	}
Z
zryfish 已提交
177

L
LiHui 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	var filterState string
	switch request.Action {
	case ActionSuspend:
		if app.Status.State != v1alpha1.StateActive {
			err = actionNotSupport
		}
		filterState = v1alpha1.StateActive
	case ActionRecover:
		if app.Status.State != v1alpha1.StateSuspended {
			err = actionNotSupport
		}
		filterState = v1alpha1.StateSuspended
	default:
		err = actionNotSupport
	}
Z
zryfish 已提交
193
	if err != nil {
L
LiHui 已提交
194
		return err
Z
zryfish 已提交
195 196
	}

L
LiHui 已提交
197 198 199
	var versions []*v1alpha1.HelmApplicationVersion
	ls := map[string]string{
		constants.ChartApplicationIdLabelKey: appId,
H
hongming 已提交
200
	}
L
LiHui 已提交
201
	versions, err = c.versionLister.List(labels.SelectorFromSet(ls))
H
hongming 已提交
202
	if err != nil {
L
LiHui 已提交
203 204
		klog.Errorf("get helm app %s version failed, error: %s", appId, err)
		return err
H
hongming 已提交
205 206
	}

L
LiHui 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
	versions = filterAppVersionByState(versions, []string{filterState})
	for _, version := range versions {
		err = c.DoAppVersionAction(version.GetHelmApplicationVersionId(), request)
		if err != nil {
			return err
		}
	}

	return nil
}

func (c *applicationOperator) CreateApp(req *CreateAppRequest) (*CreateAppResponse, error) {
	if c.backingStoreClient == nil {
		return nil, invalidS3Config
	}
	chrt, err := helmrepoindex.LoadPackage(req.VersionPackage)
	if err != nil {
		klog.Errorf("load package %s/%s failed, error: %s", req.Isv, req.Name, err)
P
pengcong06 已提交
225 226
		return nil, err
	}
L
LiHui 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

	// create helm application
	name := idutils.GetUuid36(v1alpha1.HelmApplicationIdPrefix)
	helmApp := &v1alpha1.HelmApplication{
		ObjectMeta: metav1.ObjectMeta{
			Name: name,
			Annotations: map[string]string{
				constants.CreatorAnnotationKey: req.Username,
			},
			Labels: map[string]string{
				constants.WorkspaceLabelKey: req.Isv,
			},
		},
		Spec: v1alpha1.HelmApplicationSpec{
			Name:        req.Name,
			Description: stringutils.ShortenString(chrt.GetDescription(), v1alpha1.MsgLen),
		},
	}
	app, err := c.createApp(helmApp, req.Icon)
H
hongming 已提交
246
	if err != nil {
L
LiHui 已提交
247 248 249 250
		klog.Errorf("create helm application %s/%s failed, error: %s", req.Isv, req.Name, err)
		if helmApp.Spec.Icon != "" {
			c.backingStoreClient.Delete(helmApp.Spec.Icon)
		}
H
hongming 已提交
251
		return nil, err
L
LiHui 已提交
252 253
	} else {
		klog.V(4).Infof("helm application %s/%s created, app id: %s", req.Isv, req.Name, app.Name)
H
hongming 已提交
254
	}
P
pengcong06 已提交
255

L
LiHui 已提交
256 257 258 259 260 261 262 263 264 265
	// create app version
	chartPackage := req.VersionPackage.String()
	ver := buildApplicationVersion(app, chrt, &chartPackage, req.Username)
	ver, err = c.createApplicationVersion(ver)

	if err != nil {
		klog.Errorf("create helm application %s/%s versions failed, error: %s", req.Isv, req.Name, err)
		return nil, err
	} else {
		klog.V(4).Infof("helm application version %s/%s created, app version id: %s", req.Isv, req.Name, ver.Name)
P
pengcong06 已提交
266
	}
L
LiHui 已提交
267 268 269 270 271

	return &CreateAppResponse{
		AppID:     app.GetHelmApplicationId(),
		VersionID: ver.GetHelmApplicationVersionId(),
	}, nil
Z
zryfish 已提交
272 273
}

L
LiHui 已提交
274 275
func buildLabelSelector(conditions *params.Conditions) map[string]string {
	ls := make(map[string]string)
Z
zryfish 已提交
276

L
LiHui 已提交
277 278 279 280 281 282 283 284 285 286 287 288
	repoId := conditions.Match[RepoId]
	// app store come first
	if repoId != "" {
		ls[constants.ChartRepoIdLabelKey] = repoId
	} else {
		if conditions.Match[WorkspaceLabel] != "" {
			ls[constants.WorkspaceLabelKey] = conditions.Match[WorkspaceLabel]
		}
	}
	if conditions.Match[CategoryId] != "" {
		ls[constants.CategoryIdLabelKey] = conditions.Match[CategoryId]
	}
Z
zryfish 已提交
289

L
LiHui 已提交
290 291
	return ls
}
Z
zryfish 已提交
292

L
LiHui 已提交
293
func (c *applicationOperator) ListApps(conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error) {
L
LiHui 已提交
294

L
LiHui 已提交
295 296 297 298 299 300
	apps, err := c.listApps(conditions)
	if err != nil {
		klog.Error(err)
		return nil, err
	}
	apps = filterApps(apps, conditions)
Z
zryfish 已提交
301

L
LiHui 已提交
302 303 304 305 306
	if reverse {
		sort.Sort(sort.Reverse(HelmApplicationList(apps)))
	} else {
		sort.Sort(HelmApplicationList(apps))
	}
Z
zryfish 已提交
307

L
LiHui 已提交
308
	items := make([]interface{}, 0, limit)
Z
zryfish 已提交
309

L
LiHui 已提交
310
	for i, j := offset, 0; i < len(apps) && j < limit; i, j = i+1, j+1 {
L
LiHui 已提交
311 312 313
		versions, err := c.getAppVersionsByAppId(apps[i].GetHelmApplicationId())
		if err != nil && !apierrors.IsNotFound(err) {
			return nil, err
Z
zryfish 已提交
314
		}
L
LiHui 已提交
315 316 317 318

		ctg, _ := c.ctgLister.Get(apps[i].GetHelmCategoryId())

		items = append(items, convertApp(apps[i], versions, ctg, 0))
Z
zryfish 已提交
319
	}
L
LiHui 已提交
320
	return &models.PageableResponse{Items: items, TotalCount: len(apps)}, nil
Z
zryfish 已提交
321 322
}

L
LiHui 已提交
323 324 325 326 327 328
func (c *applicationOperator) DeleteApp(id string) error {
	app, err := c.appLister.Get(id)
	if err != nil {
		if apierrors.IsNotFound(err) {
			return nil
		} else {
329 330
			klog.Errorf("get app %s failed, error: %s", id, err)
			return err
L
LiHui 已提交
331 332
		}
	}
Z
zryfish 已提交
333

L
LiHui 已提交
334 335
	ls := map[string]string{
		constants.ChartApplicationIdLabelKey: app.GetHelmApplicationId(),
Z
zryfish 已提交
336 337
	}

L
LiHui 已提交
338 339 340 341 342 343 344
	list, err := c.versionLister.List(labels.SelectorFromSet(ls))
	if err != nil {
		if apierrors.IsNotFound(err) {
			klog.V(4).Infof("versions of app %s has been deleted", id)
		} else {
			klog.Error(err)
			return err
Z
zryfish 已提交
345
		}
L
LiHui 已提交
346 347 348 349 350 351 352 353 354 355 356
	} else if len(list) > 0 {
		return fmt.Errorf("app %s has some versions not deleted", id)
	}

	err = c.appClient.Delete(context.TODO(), id, metav1.DeleteOptions{})
	if err != nil {
		klog.Errorf("delete app %s failed, error: %s", id, err)
		return err
	} else {
		c.deleteAppAttachment(app)
		klog.V(4).Infof("app %s deleted", app.Name)
Z
zryfish 已提交
357 358
	}

L
LiHui 已提交
359 360 361 362 363 364 365 366 367
	// delete application in app store
	id = fmt.Sprintf("%s%s", id, v1alpha1.HelmApplicationAppStoreSuffix)
	app, err = c.appClient.Get(context.TODO(), id, metav1.GetOptions{})
	if err != nil {
		if apierrors.IsNotFound(err) {
			return nil
		} else {
			klog.Errorf("get app %s failed, error: %s", id, err)
			return err
Z
zryfish 已提交
368 369 370
		}
	}

L
LiHui 已提交
371 372 373 374 375 376 377 378 379 380 381
	// delete application in app store
	err = c.appClient.Delete(context.TODO(), id, metav1.DeleteOptions{})
	if err != nil && !apierrors.IsNotFound(err) {
		klog.Errorf("delete app %s failed, error: %s", id, err)
		return err
	} else {
		c.deleteAppAttachment(app)
		klog.V(4).Infof("app %s deleted", app.Name)
	}

	return nil
Z
zryfish 已提交
382 383
}

L
LiHui 已提交
384 385 386 387 388 389 390 391 392
func (c *applicationOperator) ModifyApp(appId string, request *ModifyAppRequest) error {
	if c.backingStoreClient == nil {
		return invalidS3Config
	}

	app, err := c.appLister.Get(appId)
	if err != nil {
		klog.Error(err)
		return err
Z
zryfish 已提交
393 394
	}

L
LiHui 已提交
395 396 397 398 399 400 401 402 403 404
	appCopy := app.DeepCopy()
	// modify category
	if request.CategoryID != nil {
		if *request.CategoryID == "" {
			delete(appCopy.Labels, constants.CategoryIdLabelKey)
			klog.V(4).Infof("delete app %s category", app.Name)
		} else {
			appCopy.Labels[constants.CategoryIdLabelKey] = *request.CategoryID
			klog.V(4).Infof("set app %s category to %s", app.Name, *request.CategoryID)
		}
Z
zryfish 已提交
405
	}
L
LiHui 已提交
406 407 408 409

	// modify app name
	if request.Name != nil && len(*request.Name) > 0 && app.GetTrueName() != *request.Name {
		existsApp, err := c.getHelmAppByName(app.GetWorkspace(), *request.Name)
Z
zryfish 已提交
410
		if err != nil {
L
LiHui 已提交
411
			return err
Z
zryfish 已提交
412
		}
L
LiHui 已提交
413 414
		if existsApp != nil {
			return appItemExists
Z
zryfish 已提交
415
		}
L
LiHui 已提交
416 417
		klog.V(4).Infof("change app %s name from %s to %s", app.Name, app.GetTrueName(), *request.Name)
		appCopy.Spec.Name = *request.Name
Z
zryfish 已提交
418 419
	}

L
LiHui 已提交
420 421 422 423 424 425
	// save app attachment and icon
	add, err := c.modifyAppAttachment(appCopy, request)
	if err != nil {
		klog.Errorf("add app attachment %s failed, error: %s", appCopy.Name, err)
		return err
	}
Z
zryfish 已提交
426

L
LiHui 已提交
427 428 429 430 431
	if request.Description != nil {
		appCopy.Spec.Description = *request.Description
	}
	if request.Abstraction != nil {
		appCopy.Spec.Abstraction = *request.Abstraction
Z
zryfish 已提交
432 433
	}

L
LiHui 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
	if request.Home != nil {
		appCopy.Spec.AppHome = *request.Home
	}
	appCopy.Status.UpdateTime = &metav1.Time{Time: time.Now()}

	patch := client.MergeFrom(app)
	data, err := patch.Data(appCopy)
	if err != nil {
		klog.Errorf("create patch failed, error: %s", err)
		return err
	}

	_, err = c.appClient.Patch(context.TODO(), appId, patch.Type(), data, metav1.PatchOptions{})
	if err != nil {
		klog.Errorf("patch helm application: %s failed, error: %s", appId, err)
		if add != "" {
			// if patch failed, delete saved icon or attachment
			c.backingStoreClient.Delete(add)
Z
zryfish 已提交
452
		}
L
LiHui 已提交
453 454 455 456
		return err
	}
	return nil
}
Z
zryfish 已提交
457

L
LiHui 已提交
458 459 460 461
func (c *applicationOperator) deleteAppAttachment(app *v1alpha1.HelmApplication) {
	if app.Spec.Icon != "" {
		c.backingStoreClient.Delete(app.Spec.Icon)
	}
Z
zryfish 已提交
462

L
LiHui 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	for _, id := range app.Spec.Attachments {
		c.backingStoreClient.Delete(id)
	}
}

func (c *applicationOperator) modifyAppAttachment(app *v1alpha1.HelmApplication, request *ModifyAppRequest) (add string, err error) {
	if request.Type == nil {
		return "", nil
	}
	switch *request.Type {
	case v1alpha1.AttachmentTypeScreenshot:
		if request.Sequence == nil {
			return "", nil
		}
		seq := *request.Sequence
		attachments := &app.Spec.Attachments
		if len(request.AttachmentContent) == 0 {
			// delete old attachments
			if len(*attachments) > int(seq) {
				del := (*attachments)[seq]
				err = c.backingStoreClient.Delete(del)
				if err != nil {
					return "", err
				} else {
					*attachments = append((*attachments)[:seq], (*attachments)[seq+1:]...)
Z
zryfish 已提交
488 489
				}
			}
L
LiHui 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
		} else {
			if len(*attachments) < 6 {
				// add attachment to app
				add := idutils.GetUuid("att-")
				*attachments = append(*attachments, add)
				err = c.backingStoreClient.Upload(add, add, bytes.NewBuffer(request.AttachmentContent))
				if err != nil {
					return "", err
				} else {
					return add, nil
				}
			}
		}
	case v1alpha1.AttachmentTypeIcon: // modify app icon
		// delete old icon
		if app.Spec.Icon != "" {
			err = c.backingStoreClient.Delete(app.Spec.Icon)
			if err != nil {
				return "", err
Z
zryfish 已提交
509 510 511
			}
		}
	}
L
LiHui 已提交
512 513 514 515 516 517 518 519 520 521 522
	if len(request.AttachmentContent) != 0 {
		add := idutils.GetUuid("att-")
		err = c.backingStoreClient.Upload(add, add, bytes.NewBuffer(request.AttachmentContent))
		if err != nil {
			return "", err
		} else {
			app.Spec.Icon = add
			return add, nil
		}
	}
	return "", nil
Z
zryfish 已提交
523
}
H
hongming 已提交
524

L
LiHui 已提交
525 526 527 528
// modify icon or attachment of the app
// added: new attachments have been saved to store
// deleted: attachments should be deleted
func (c *applicationOperator) appAttachmentDiff(old, newApp *v1alpha1.HelmApplication) (added, deleted []string) {
H
hongming 已提交
529

L
LiHui 已提交
530 531
	added = make([]string, 0, 7)
	deleted = make([]string, 0, 7)
H
hongming 已提交
532

L
LiHui 已提交
533 534 535 536 537 538
	if old.Spec.Icon != newApp.Spec.Icon {
		if old.Spec.Icon != "" && !strings.HasPrefix(old.Spec.Icon, "http://") {
			deleted = append(deleted, old.Spec.Icon)
		}
		added = append(added, newApp.Spec.Icon)
	}
H
hongming 已提交
539

L
LiHui 已提交
540 541
	existsAtt := make(map[string]string, 6)
	newAtt := make(map[string]string, 6)
H
hongming 已提交
542

L
LiHui 已提交
543 544
	for _, id := range newApp.Spec.Attachments {
		newAtt[id] = ""
H
hongming 已提交
545
	}
L
LiHui 已提交
546 547 548

	for _, id := range old.Spec.Attachments {
		existsAtt[id] = ""
H
hongming 已提交
549 550
	}

L
LiHui 已提交
551 552 553 554 555
	for _, id := range newApp.Spec.Attachments {
		if _, exists := existsAtt[id]; !exists {
			added = append(added, id)
		}
	}
H
hongming 已提交
556

L
LiHui 已提交
557 558 559 560
	for _, id := range old.Spec.Attachments {
		if _, exists := newAtt[id]; !exists {
			deleted = append(deleted, id)
		}
H
hongming 已提交
561
	}
H
hongming 已提交
562

L
LiHui 已提交
563
	return added, deleted
H
hongming 已提交
564 565
}

L
LiHui 已提交
566 567 568 569
func (c *applicationOperator) DescribeApp(id string) (*App, error) {
	var helmApp *v1alpha1.HelmApplication
	var ctg *v1alpha1.HelmCategory
	var err error
H
hongming 已提交
570

L
LiHui 已提交
571
	helmApp, err = c.getHelmApplication(id)
572
	if err != nil {
L
LiHui 已提交
573 574
		klog.Error(err)
		return nil, err
575 576
	}

L
LiHui 已提交
577 578 579 580 581
	versions, err := c.getAppVersionsByAppId(helmApp.GetHelmApplicationId())
	if err != nil {
		klog.Error(err)
		return nil, err
	}
P
pengcong06 已提交
582

L
LiHui 已提交
583
	ctg, err = c.ctgLister.Get(helmApp.GetHelmCategoryId())
P
pengcong06 已提交
584

L
LiHui 已提交
585 586 587
	if err != nil && !apierrors.IsNotFound(err) {
		klog.Error(err)
		return nil, err
P
pengcong06 已提交
588 589
	}

L
LiHui 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
	app := convertApp(helmApp, versions, ctg, 0)
	return app, nil
}

func (c *applicationOperator) listApps(conditions *params.Conditions) (ret []*v1alpha1.HelmApplication, err error) {
	repoId := conditions.Match[RepoId]
	if repoId != "" && repoId != v1alpha1.AppStoreRepoId {
		// get helm application from helm repo
		if ret, exists := c.cachedRepos.ListApplicationsByRepoId(repoId); !exists {
			klog.Warningf("load repo failed, repo id: %s", repoId)
			return nil, loadRepoInfoFailed
		} else {
			return ret, nil
		}
	} else {
L
LiHui 已提交
605 606 607
		if c.backingStoreClient == nil {
			return []*v1alpha1.HelmApplication{}, nil
		}
L
LiHui 已提交
608 609 610 611 612 613 614 615 616 617 618 619
		ret, err = c.appLister.List(labels.SelectorFromSet(buildLabelSelector(conditions)))
	}

	return
}

func (c *applicationOperator) getHelmApplication(appId string) (*v1alpha1.HelmApplication, error) {
	if app, exists := c.cachedRepos.GetApplication(appId); exists {
		return app, nil
	} else {
		return c.appLister.Get(appId)
	}
P
pengcong06 已提交
620
}