Skip to content

Commit 6d10fff

Browse files
authored
wip (#5865)
1 parent 85710ec commit 6d10fff

File tree

4 files changed

+124
-9
lines changed

4 files changed

+124
-9
lines changed

api/helm-app/service/HelmAppService.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,12 @@ func (impl *HelmAppServiceImpl) appListRespProtoTransformer(deployedApps *gRPC.D
11591159
}
11601160
// end
11611161
lastDeployed := deployedapp.LastDeployed.AsTime()
1162-
appDetails, appFetchErr := impl.appRepository.FindActiveByName(deployedapp.AppName)
1162+
appDetails, appFetchErr := impl.getAppForAppIdentifier(
1163+
&helmBean.AppIdentifier{
1164+
ClusterId: int(deployedapp.EnvironmentDetail.ClusterId),
1165+
Namespace: deployedapp.EnvironmentDetail.Namespace,
1166+
ReleaseName: deployedapp.AppName,
1167+
})
11631168
projectId := int32(0)
11641169
if appFetchErr == nil {
11651170
projectId = int32(appDetails.TeamId)

internal/sql/repository/app/AppRepository.go

+73-5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ type App struct {
4040
sql.AuditLog
4141
}
4242

43+
const (
44+
SYSTEM_USER_ID = 1
45+
)
46+
4347
func (r *App) IsAppJobOrExternalType() bool {
4448
return len(r.DisplayName) > 0
4549
}
@@ -129,16 +133,37 @@ func (repo AppRepositoryImpl) SetDescription(id int, description string, userId
129133
}
130134

131135
func (repo AppRepositoryImpl) FindActiveByName(appName string) (*App, error) {
132-
pipelineGroup := &App{}
136+
var apps []*App
133137
err := repo.dbConnection.
134-
Model(pipelineGroup).
138+
Model(&apps).
135139
Where("app_name = ?", appName).
136140
Where("active = ?", true).
137-
Order("id DESC").Limit(1).
141+
Order("id DESC").
138142
Select()
139-
// there is only single active app will be present in db with a same name.
140-
return pipelineGroup, err
143+
if len(apps) == 1 {
144+
return apps[0], nil
145+
} else if len(apps) > 1 {
146+
isHelmApp := true
147+
for _, app := range apps {
148+
if app.AppType != helper.ChartStoreApp && app.AppType != helper.ExternalChartStoreApp {
149+
isHelmApp = false
150+
break
151+
}
152+
}
153+
if isHelmApp {
154+
err := repo.fixMultipleHelmAppsWithSameName(appName)
155+
if err != nil {
156+
repo.logger.Errorw("error in fixing duplicate helm apps with same name")
157+
return nil, err
158+
}
159+
}
160+
return apps[0], nil
161+
} else {
162+
err = pg.ErrNoRows
163+
}
164+
return nil, err
141165
}
166+
142167
func (repo AppRepositoryImpl) FindAppIdByName(appName string) (int, error) {
143168
app := &App{}
144169
err := repo.dbConnection.
@@ -324,9 +349,52 @@ func (repo AppRepositoryImpl) FindAppAndProjectByAppName(appName string) (*App,
324349
Where("app.app_name = ?", appName).
325350
Where("app.active=?", true).
326351
Select()
352+
353+
if err == pg.ErrMultiRows && (app.AppType == helper.ChartStoreApp || app.AppType == helper.ExternalChartStoreApp) {
354+
// this case can arise in helms apps only
355+
356+
err := repo.fixMultipleHelmAppsWithSameName(appName)
357+
if err != nil {
358+
repo.logger.Errorw("error in fixing duplicate helm apps with same name")
359+
return nil, err
360+
}
361+
362+
err = repo.dbConnection.Model(app).Column("Team").
363+
Where("app.app_name = ?", appName).
364+
Where("app.active=?", true).
365+
Select()
366+
if err != nil {
367+
repo.logger.Errorw("error in fetching apps by name", "appName", appName, "err", err)
368+
return nil, err
369+
}
370+
}
327371
return app, err
328372
}
329373

374+
func (repo AppRepositoryImpl) fixMultipleHelmAppsWithSameName(appName string) error {
375+
// updating installed apps setting app_id = max app_id
376+
installAppUpdateQuery := `update installed_apps set
377+
app_id=(select max(id) as id from app where app_name = ?)
378+
where app_id in (select id from app where app_name= ? )`
379+
380+
_, err := repo.dbConnection.Exec(installAppUpdateQuery, appName, appName)
381+
if err != nil {
382+
repo.logger.Errorw("error in updating maxAppId in installedApps", "appName", appName, "err", err)
383+
return err
384+
}
385+
386+
maxAppIdQuery := repo.dbConnection.Model((*App)(nil)).ColumnExpr("max(id)").
387+
Where("app_name = ? ", appName).
388+
Where("active = ? ", true)
389+
390+
// deleting all apps other than app with max id
391+
_, err = repo.dbConnection.Model((*App)(nil)).
392+
Set("active = ?", false).Set("updated_by = ?", SYSTEM_USER_ID).Set("updated_on = ?", time.Now()).
393+
Where("id not in (?) ", maxAppIdQuery).Update()
394+
395+
return nil
396+
}
397+
330398
func (repo AppRepositoryImpl) FindAllMatchesByAppName(appName string, appType helper.AppType) ([]*App, error) {
331399
var apps []*App
332400
var err error

pkg/app/AppCrudOperationService.go

+34-3
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,15 @@ func (impl AppCrudOperationServiceImpl) GetHelmAppMetaInfo(appId string) (*bean.
533533
}
534534
// if app.DisplayName is empty then that app_name is not yet migrated to app name unique identifier
535535
if app.Id > 0 && len(app.DisplayName) == 0 {
536-
err = impl.updateAppNameToUniqueAppIdentifierInApp(app, appIdDecoded)
536+
appNameUniqueIdentifier := appIdDecoded.GetUniqueAppNameIdentifier()
537+
app.AppName = appNameUniqueIdentifier
538+
app.DisplayName = appIdDecoded.ReleaseName
539+
app.UpdatedBy = bean2.SystemUserId
540+
app.UpdatedOn = time.Now()
541+
err = impl.appRepository.Update(app)
537542
if err != nil {
538-
impl.logger.Errorw("GetHelmAppMetaInfo, error in migrating displayName and appName to unique identifier for external apps", "appIdentifier", appIdDecoded, "err", err)
539-
//not returning from here as we need to show helm app metadata even if migration of app_name fails, then migration can happen on project update
543+
impl.logger.Errorw("error in migrating displayName and appName to unique identifier", "appNameUniqueIdentifier", appNameUniqueIdentifier, "err", err)
544+
return nil, err
540545
}
541546
}
542547
if app.Id == 0 {
@@ -569,6 +574,12 @@ func (impl AppCrudOperationServiceImpl) GetHelmAppMetaInfo(appId string) (*bean.
569574
}
570575
}
571576

577+
err = impl.fixMultipleInstalledAppForSingleApp(app)
578+
if err != nil {
579+
impl.logger.Errorw("GetHelmAppMetaInfo, error in fixing multiple installed apps linked to same app", "appId", appId, "err", err)
580+
return nil, err
581+
}
582+
572583
user, err := impl.userRepository.GetByIdIncludeDeleted(app.CreatedBy)
573584
if err != nil && err != pg.ErrNoRows {
574585
impl.logger.Errorw("error in fetching user for app meta info", "error", err)
@@ -598,6 +609,26 @@ func (impl AppCrudOperationServiceImpl) GetHelmAppMetaInfo(appId string) (*bean.
598609
return info, nil
599610
}
600611

612+
// fixMultipleInstalledAppForSingleApp fixes multiple entries of installed app for single app
613+
func (impl AppCrudOperationServiceImpl) fixMultipleInstalledAppForSingleApp(app *appRepository.App) error {
614+
isLinked, installedApps, err := impl.installedAppDbService.IsExternalAppLinkedToChartStore(app.Id)
615+
if err != nil {
616+
impl.logger.Errorw("error in checking IsExternalAppLinkedToChartStore", "appId", app.Id, "err", err)
617+
return err
618+
}
619+
//if isLinked is true and more than one installed app is found for that app, we will create new app for each installed app
620+
if isLinked && len(installedApps) > 1 {
621+
// if installed_apps are already present for that display_name then migrate the app_name to unique identifier with installedApp's ns and clusterId.
622+
// creating new entry for app all installedApps with uniqueAppNameIdentifier and display name
623+
err := impl.installedAppDbService.CreateNewAppEntryForAllInstalledApps(installedApps)
624+
if err != nil {
625+
impl.logger.Errorw("error in CreateNewAppEntryForAllInstalledApps", "appName", app.AppName, "err", err)
626+
//not returning from here as we have to migrate the app for requested ext-app and return the response for meta info
627+
}
628+
}
629+
return nil
630+
}
631+
601632
func (impl AppCrudOperationServiceImpl) getLabelsByAppIdForDeployment(appId int) (map[string]string, error) {
602633
labelsDto := make(map[string]string)
603634
labels, err := impl.appLabelRepository.FindAllByAppId(appId)

pkg/appStore/installedApp/service/EAMode/InstalledAppDBService.go

+11
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,17 @@ func (impl *InstalledAppDBServiceImpl) CreateNewAppEntryForAllInstalledApps(inst
401401
if err != nil {
402402
return err
403403
}
404+
currApp, err := impl.AppRepository.FindById(installedApps[0].AppId)
405+
if err != nil {
406+
impl.Logger.Errorw("error in fetching app by id", "appId", currApp.Id, "err", err)
407+
return err
408+
}
409+
currApp.Active = false
410+
err = impl.AppRepository.UpdateWithTxn(currApp, tx)
411+
if err != nil {
412+
impl.Logger.Errorw("error in marking current app inactive while creating new apps", "currentAppId", currApp.Id, "err", err)
413+
return err
414+
}
404415
// Rollback tx on error.
405416
defer tx.Rollback()
406417
for _, installedApp := range installedApps {

0 commit comments

Comments
 (0)