Skip to content

Commit fd39e29

Browse files
Revert "fix: Helm apps entries in Ea mode (#5652)"
This reverts commit f1aa1fc.
1 parent 7f5db23 commit fd39e29

File tree

4 files changed

+7
-149
lines changed

4 files changed

+7
-149
lines changed

pkg/app/AppCrudOperationService.go

Lines changed: 5 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package app
1919
import (
2020
"context"
2121
"encoding/json"
22-
"errors"
2322
"fmt"
2423
"github.com/caarlos0/env"
2524
client "github.com/devtron-labs/devtron/api/helm-app/service"
@@ -458,67 +457,13 @@ func convertUrlToHttpsIfSshType(url string) string {
458457
return httpsURL
459458
}
460459

461-
// handleDuplicateAppEntries identifies and resolves duplicate app entries based on creation time.
462-
// It marks the most recent duplicate entry as inactive and updates the corresponding installed app.
463-
func (impl AppCrudOperationServiceImpl) handleDuplicateAppEntries(appNameUniqueIdentifier string) (*appRepository.App, error) {
464-
// Fetch app IDs by name
465-
appIds, err := impl.getAppIdsByName(appNameUniqueIdentifier)
466-
if err != nil {
467-
impl.logger.Errorw("error in fetching app Ids by appIdentifier", "appNameUniqueIdentifier", appNameUniqueIdentifier, "err", err)
468-
return nil, err
469-
}
470-
471-
// Fetch apps by IDs from App table for duplicated entries
472-
apps, err := impl.appRepository.FindByIds(appIds)
473-
if err != nil || errors.Is(err, pg.ErrNoRows) {
474-
impl.logger.Errorw("error in fetching app List by appIds", "appIds", appIds, "err", err)
475-
return nil, err
476-
}
477-
478-
// Identify the earliest and duplicated app entries
479-
earliestApp, duplicatedApp := identifyDuplicateApps(apps)
480-
481-
// Fetch the installed app associated with the duplicated app
482-
installedApp, err := impl.installedAppRepository.GetInstalledAppsByAppId(duplicatedApp.Id)
483-
if err != nil {
484-
impl.logger.Errorw("error in fetching installed app by appId", "appId", duplicatedApp.Id, "err", err)
485-
return nil, err
486-
}
487-
// Update duplicated app entries
488-
err = impl.installedAppDbService.UpdateDuplicatedEntriesInAppAndInstalledApps(earliestApp, duplicatedApp, &installedApp)
489-
if err != nil {
490-
impl.logger.Errorw("error in updating duplicated entries", "earliestApp", earliestApp, "duplicatedApp", duplicatedApp, "err", err)
491-
return nil, err
492-
}
493-
494-
impl.logger.Debug("Successfully resolved duplicate app entries", "earliestApp", earliestApp, "duplicatedApp", duplicatedApp)
495-
return earliestApp, nil
496-
497-
}
498-
499-
// getAppIdsByName fetches app IDs by the app name unique identifier [for duplicated active app]
500-
func (impl AppCrudOperationServiceImpl) getAppIdsByName(appNameUniqueIdentifier string) ([]*int, error) {
501-
slice := []string{appNameUniqueIdentifier}
502-
appIds, err := impl.appRepository.FindIdsByNames(slice)
503-
if err != nil {
504-
return nil, err
505-
}
506-
507-
// Convert each element to a pointer and store in a slice of pointers
508-
ids := make([]*int, len(appIds))
509-
for i := range appIds {
510-
ids[i] = &appIds[i]
511-
}
512-
return ids, nil
513-
}
514-
515460
// getAppAndProjectForAppIdentifier, returns app db model for an app unique identifier or from display_name if both exists else it throws pg.ErrNoRows
516461
func (impl AppCrudOperationServiceImpl) getAppAndProjectForAppIdentifier(appIdentifier *helmBean.AppIdentifier) (*appRepository.App, error) {
517462
app := &appRepository.App{}
518463
var err error
519464
appNameUniqueIdentifier := appIdentifier.GetUniqueAppNameIdentifier()
520465
app, err = impl.appRepository.FindAppAndProjectByAppName(appNameUniqueIdentifier)
521-
if err != nil && !errors.Is(err, pg.ErrNoRows) && !errors.Is(err, pg.ErrMultiRows) {
466+
if err != nil && err != pg.ErrNoRows {
522467
impl.logger.Errorw("error in fetching app meta data by unique app identifier", "appNameUniqueIdentifier", appNameUniqueIdentifier, "err", err)
523468
return app, err
524469
}
@@ -530,14 +475,6 @@ func (impl AppCrudOperationServiceImpl) getAppAndProjectForAppIdentifier(appIden
530475
return app, err
531476
}
532477
}
533-
if errors.Is(err, pg.ErrMultiRows) {
534-
535-
app, err = impl.handleDuplicateAppEntries(appNameUniqueIdentifier)
536-
if err != nil {
537-
impl.logger.Errorw("error in handling Duplicate entries in the app", "appNameUniqueIdentifier", appNameUniqueIdentifier, "err", err)
538-
return app, err
539-
}
540-
}
541478
return app, nil
542479
}
543480

@@ -595,17 +532,17 @@ func (impl AppCrudOperationServiceImpl) GetHelmAppMetaInfo(appId string) (*bean.
595532
return nil, err
596533
}
597534
// if app.DisplayName is empty then that app_name is not yet migrated to app name unique identifier
598-
if app != nil && app.Id > 0 && len(app.DisplayName) == 0 {
535+
if app.Id > 0 && len(app.DisplayName) == 0 {
599536
err = impl.updateAppNameToUniqueAppIdentifierInApp(app, appIdDecoded)
600537
if err != nil {
601538
impl.logger.Errorw("GetHelmAppMetaInfo, error in migrating displayName and appName to unique identifier for external apps", "appIdentifier", appIdDecoded, "err", err)
602539
//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
603540
}
604541
}
605-
if app != nil && app.Id == 0 {
542+
if app.Id == 0 {
606543
app.AppName = appIdDecoded.ReleaseName
607544
}
608-
if app != nil && util2.IsExternalChartStoreApp(app.DisplayName) {
545+
if util2.IsExternalChartStoreApp(app.DisplayName) {
609546
displayName = app.DisplayName
610547
}
611548

@@ -631,14 +568,9 @@ func (impl AppCrudOperationServiceImpl) GetHelmAppMetaInfo(appId string) (*bean.
631568
displayName = InstalledApp.App.DisplayName
632569
}
633570
}
634-
// Safeguard against nil app cases
635-
if app == nil {
636-
impl.logger.Errorw("no rows found for the requested app", "appId", appId, "error", err)
637-
return nil, fmt.Errorf("no rows found for the requested app, %q", pg.ErrNoRows)
638-
}
639571

640572
user, err := impl.userRepository.GetByIdIncludeDeleted(app.CreatedBy)
641-
if err != nil && !errors.Is(err, pg.ErrNoRows) {
573+
if err != nil && err != pg.ErrNoRows {
642574
impl.logger.Errorw("error in fetching user for app meta info", "error", err)
643575
return nil, err
644576
}

pkg/app/helper.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
package app
1818

19-
import (
20-
appRepository "github.com/devtron-labs/devtron/internal/sql/repository/app"
21-
"strings"
22-
)
19+
import "strings"
2320

2421
// LabelMatchingRegex is the official k8s label matching regex, pls refer https://github.com/kubernetes/apimachinery/blob/bfd2aff97e594f6aad77acbe2cbbe190acc93cbc/pkg/util/validation/validation.go#L167
2522
const LabelMatchingRegex = "^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$"
@@ -46,21 +43,3 @@ func sanitizeLabels(extraAppLabels map[string]string) map[string]string {
4643
}
4744
return extraAppLabels
4845
}
49-
50-
// identifyDuplicateApps identifies the earliest created app and the most recent duplicate app.
51-
func identifyDuplicateApps(apps []*appRepository.App) (earliestApp *appRepository.App, duplicatedApp *appRepository.App) {
52-
if len(apps) == 0 {
53-
return nil, nil
54-
}
55-
earliestApp = apps[0]
56-
duplicatedApp = apps[0]
57-
for _, app := range apps[1:] {
58-
if app.AuditLog.CreatedOn.Before(earliestApp.AuditLog.CreatedOn) {
59-
earliestApp = app
60-
}
61-
if app.AuditLog.CreatedOn.After(duplicatedApp.AuditLog.CreatedOn) {
62-
duplicatedApp = app
63-
}
64-
}
65-
return earliestApp, duplicatedApp
66-
}

pkg/appStore/installedApp/service/AppStoreDeploymentDBService.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func (impl *AppStoreDeploymentDBServiceImpl) AppStoreDeployOperationDB(installRe
133133
}
134134
// setting additional env data required in appStoreBean.InstallAppVersionDTO
135135
adapter.UpdateAdditionalEnvDetails(installRequest, environment)
136+
136137
impl.appStoreValidator.Validate(installRequest, environment)
137138

138139
// Stage 1: Create App in tx (Only if AppId is not set already)

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

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ type InstalledAppDBService interface {
5858
GetReleaseInfo(appIdentifier *helmBean.AppIdentifier) (*appStoreBean.InstallAppVersionDTO, error)
5959
IsExternalAppLinkedToChartStore(appId int) (bool, []*appStoreRepo.InstalledApps, error)
6060
CreateNewAppEntryForAllInstalledApps(installedApps []*appStoreRepo.InstalledApps) error
61-
UpdateDuplicatedEntriesInAppAndInstalledApps(earlyApp *app.App, duplicatedApp *app.App, installedApp *appStoreRepo.InstalledApps) error
6261
}
6362

6463
type InstalledAppDBServiceImpl struct {
@@ -400,17 +399,6 @@ func (impl *InstalledAppDBServiceImpl) CreateNewAppEntryForAllInstalledApps(inst
400399
// Rollback tx on error.
401400
defer tx.Rollback()
402401
for _, installedApp := range installedApps {
403-
404-
//check if there is any app from its appName is exits and active ...if yes then we will not insert any extra entry in the db
405-
appMetadataByAppName, err := impl.AppRepository.FindActiveByName(installedApp.App.AppName)
406-
if err != nil && !util.IsErrNoRows(err) {
407-
impl.Logger.Errorw("error in fetching app by unique app identifier", "appNameUniqueIdentifier", installedApp.GetUniqueAppNameIdentifier(), "err", err)
408-
return err
409-
}
410-
if appMetadataByAppName != nil && appMetadataByAppName.Id > 0 {
411-
//app already exists for this unique identifier hence not creating new app entry for this as it will get modified after this function
412-
continue
413-
}
414402
//check if for this unique identifier name an app already exists, if yes then continue
415403
appMetadata, err := impl.AppRepository.FindActiveByName(installedApp.GetUniqueAppNameIdentifier())
416404
if err != nil && !util.IsErrNoRows(err) {
@@ -449,45 +437,3 @@ func (impl *InstalledAppDBServiceImpl) CreateNewAppEntryForAllInstalledApps(inst
449437
tx.Commit()
450438
return nil
451439
}
452-
453-
// UpdateDuplicatedEntriesInAppAndInstalledApps performs the updation in app table and installedApps table for the cases when multiple active app found [typically two due to migration], here we are updating the db with its previous value in the installedApps table and early created app id
454-
func (impl *InstalledAppDBServiceImpl) UpdateDuplicatedEntriesInAppAndInstalledApps(earlyApp *app.App, duplicatedApp *app.App, installedApp *appStoreRepo.InstalledApps) error {
455-
// db operations
456-
dbConnection := impl.InstalledAppRepository.GetConnection()
457-
tx, err := dbConnection.Begin()
458-
if err != nil {
459-
return err
460-
}
461-
// Rollback tx on error.
462-
defer func(tx *pg.Tx) {
463-
err := tx.Rollback()
464-
if err != nil {
465-
impl.Logger.Errorw("Rollback error", "err", err)
466-
}
467-
}(tx)
468-
469-
//updated the app table with active column as false for the duplicated app
470-
duplicatedApp.Active = false
471-
duplicatedApp.CreateAuditLog(bean3.SystemUserId)
472-
err = impl.AppRepository.UpdateWithTxn(duplicatedApp, tx)
473-
if err != nil {
474-
impl.Logger.Errorw("error saving appModel", "err", err)
475-
return err
476-
}
477-
478-
// updating the installedApps table with its appId column with the previous app
479-
installedApp.AppId = earlyApp.Id
480-
installedApp.UpdateAuditLog(bean3.SystemUserId)
481-
_, err = impl.InstalledAppRepository.UpdateInstalledApp(installedApp, tx)
482-
if err != nil {
483-
impl.Logger.Errorw("error saving updating installed app with new appId", "installedAppId", installedApp.Id, "err", err)
484-
return err
485-
}
486-
487-
err = tx.Commit()
488-
if err != nil {
489-
impl.Logger.Errorw("error saving appModel", "err", err)
490-
return err
491-
}
492-
return nil
493-
}

0 commit comments

Comments
 (0)