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
	"bytes"
	"context"
	"errors"
	"fmt"
Z
zryfish 已提交
21 22 23 24
	"sort"
	"strings"
	"time"

L
LiHui 已提交
25 26
	apierrors "k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Z
zryfish 已提交
27
	"k8s.io/apimachinery/pkg/labels"
J
Jeff 已提交
28
	"k8s.io/klog"
Z
zryfish 已提交
29 30
	"sigs.k8s.io/controller-runtime/pkg/client"

L
LiHui 已提交
31 32 33 34 35 36
	"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 已提交
37
	"kubesphere.io/kubesphere/pkg/models"
J
Jeff 已提交
38
	"kubesphere.io/kubesphere/pkg/server/params"
L
LiHui 已提交
39 40 41 42 43
	"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"
Z
zryfish 已提交
44 45
)

H
hongming 已提交
46
type ApplicationInterface interface {
L
LiHui 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	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 已提交
64 65 66
}

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

L
LiHui 已提交
70 71
	appClient        v1alpha13.HelmApplicationInterface
	appVersionClient v1alpha13.HelmApplicationVersionInterface
Z
zryfish 已提交
72

L
LiHui 已提交
73 74
	appLister     listers_v1alpha1.HelmApplicationLister
	versionLister listers_v1alpha1.HelmApplicationVersionLister
Z
zryfish 已提交
75

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

L
LiHui 已提交
80
	cachedRepos reposcache.ReposCache
P
pengcong06 已提交
81 82
}

L
LiHui 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
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 已提交
98
	}
L
LiHui 已提交
99 100 101 102 103 104 105

	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 已提交
106
	if err != nil {
H
hongming 已提交
107
		return nil, err
Z
zryfish 已提交
108
	}
L
LiHui 已提交
109 110 111 112 113 114 115 116
	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 已提交
117
		if err != nil {
L
LiHui 已提交
118
			klog.Errorf("save icon attachment failed, error: %s", err)
H
hongming 已提交
119 120
			return nil, err
		}
L
LiHui 已提交
121
		app.Spec.Icon = iconId
Z
zryfish 已提交
122 123
	}

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

L
LiHui 已提交
128 129 130 131 132 133 134 135 136
// 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 已提交
137 138
		return nil, err
	}
L
LiHui 已提交
139 140 141 142 143 144 145

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

	return nil, nil
}

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

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

	result := &ValidatePackageResponse{}

157
	if err != nil {
L
LiHui 已提交
158 159 160 161 162 163 164 165 166 167
		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 已提交
168
	}
L
LiHui 已提交
169 170

	return result, nil
H
hongming 已提交
171 172
}

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

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

L
LiHui 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	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 已提交
195
	if err != nil {
L
LiHui 已提交
196
		return err
Z
zryfish 已提交
197 198
	}

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

L
LiHui 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	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 已提交
227 228
		return nil, err
	}
L
LiHui 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

	// 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 已提交
248
	if err != nil {
L
LiHui 已提交
249 250 251 252
		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 已提交
253
		return nil, err
L
LiHui 已提交
254 255
	} else {
		klog.V(4).Infof("helm application %s/%s created, app id: %s", req.Isv, req.Name, app.Name)
H
hongming 已提交
256
	}
P
pengcong06 已提交
257

L
LiHui 已提交
258 259 260 261 262 263 264 265 266 267
	// 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 已提交
268
	}
L
LiHui 已提交
269 270 271 272 273

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

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

L
LiHui 已提交
279 280 281 282 283 284 285 286 287 288 289 290
	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 已提交
291

L
LiHui 已提交
292 293
	return ls
}
Z
zryfish 已提交
294

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

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

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

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

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

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

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

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

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

L
LiHui 已提交
340 341 342 343 344 345 346
	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 已提交
347
		}
L
LiHui 已提交
348 349 350 351 352 353 354 355 356 357 358
	} 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 已提交
359 360
	}

L
LiHui 已提交
361 362 363 364 365 366 367 368 369
	// 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 已提交
370 371 372
		}
	}

L
LiHui 已提交
373 374 375 376 377 378 379 380 381 382 383
	// 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 已提交
384 385
}

L
LiHui 已提交
386 387 388 389 390 391 392 393 394
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 已提交
395 396
	}

L
LiHui 已提交
397 398 399 400 401 402 403 404 405 406
	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 已提交
407
	}
L
LiHui 已提交
408 409 410 411

	// 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 已提交
412
		if err != nil {
L
LiHui 已提交
413
			return err
Z
zryfish 已提交
414
		}
L
LiHui 已提交
415 416
		if existsApp != nil {
			return appItemExists
Z
zryfish 已提交
417
		}
L
LiHui 已提交
418 419
		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 已提交
420 421
	}

L
LiHui 已提交
422 423 424 425 426 427
	// 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 已提交
428

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

L
LiHui 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
	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 已提交
454
		}
L
LiHui 已提交
455 456 457 458
		return err
	}
	return nil
}
Z
zryfish 已提交
459

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

L
LiHui 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
	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 已提交
490 491
				}
			}
L
LiHui 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
		} 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 已提交
511 512 513
			}
		}
	}
L
LiHui 已提交
514 515 516 517 518 519 520 521 522 523 524
	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 已提交
525
}
H
hongming 已提交
526

L
LiHui 已提交
527 528 529 530
// 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 已提交
531

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

L
LiHui 已提交
535 536 537 538 539 540
	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 已提交
541

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

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

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

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

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

L
LiHui 已提交
565
	return added, deleted
H
hongming 已提交
566 567
}

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

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

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

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

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

L
LiHui 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
	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 已提交
607 608 609
		if c.backingStoreClient == nil {
			return []*v1alpha1.HelmApplication{}, nil
		}
L
LiHui 已提交
610 611 612 613 614 615 616 617 618 619 620 621
		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 已提交
622
}