Skip to content

Commit 15c9f3a

Browse files
Merge pull request #6389 from devtron-labs/argo-cd-corrupt-data-fix-from-main
fix: when a helm app is managed by argocd then skip argo app update when same name ext helm app is installed
2 parents dcb9f98 + beceb53 commit 15c9f3a

File tree

5 files changed

+73
-9
lines changed

5 files changed

+73
-9
lines changed

internal/sql/repository/deploymentConfig/repository.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package deploymentConfig
1818

1919
import (
20+
"github.com/devtron-labs/devtron/internal/sql/repository/helper"
2021
"github.com/devtron-labs/devtron/pkg/sql"
2122
"github.com/go-pg/pg"
2223
"github.com/go-pg/pg/orm"
@@ -63,6 +64,7 @@ type Repository interface {
6364
GetByAppIdAndEnvIdEvenIfInactive(appId, envId int) (*DeploymentConfig, error)
6465
UpdateRepoUrlByAppIdAndEnvId(repoUrl string, appId, envId int) error
6566
GetConfigByAppIds(appIds []int) ([]*DeploymentConfig, error)
67+
GetDeploymentAppTypeForChartStoreAppByAppId(appId int) (string, error)
6668
}
6769

6870
type RepositoryImpl struct {
@@ -203,3 +205,16 @@ func (impl *RepositoryImpl) GetConfigByAppIds(appIds []int) ([]*DeploymentConfig
203205
Select()
204206
return results, err
205207
}
208+
209+
func (impl *RepositoryImpl) GetDeploymentAppTypeForChartStoreAppByAppId(appId int) (string, error) {
210+
result := &DeploymentConfig{}
211+
err := impl.dbConnection.Model(result).
212+
Join("inner join app a").
213+
JoinOn("deployment_config.app_id = a.id").
214+
Where("deployment_config.app_id = ? ", appId).
215+
Where("deployment_config.active = ?", true).
216+
Where("a.active = ?", true).
217+
Where("a.app_type = ? ", helper.ChartStoreApp).
218+
Select()
219+
return result.DeploymentAppType, err
220+
}

pkg/app/AppCrudOperationService.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -463,22 +463,22 @@ func convertUrlToHttpsIfSshType(url string) string {
463463
}
464464

465465
// getAppAndProjectForAppIdentifier, returns app db model for an app unique identifier or from display_name if both exists else it throws pg.ErrNoRows
466-
func (impl AppCrudOperationServiceImpl) getAppAndProjectForAppIdentifier(appIdentifier *helmBean.AppIdentifier) (*appRepository.App, error) {
466+
func (impl AppCrudOperationServiceImpl) getAppAndProjectForAppIdentifier(appIdentifier *helmBean.AppIdentifier) (*appRepository.App, bool, error) {
467467
app := &appRepository.App{}
468468
var err error
469469
appNameUniqueIdentifier := appIdentifier.GetUniqueAppNameIdentifier()
470470
app, err = impl.appRepository.FindAppAndProjectByAppName(appNameUniqueIdentifier)
471471
if err != nil && err != pg.ErrNoRows && err != pg.ErrMultiRows {
472472
impl.logger.Errorw("error in fetching app meta data by unique app identifier", "appNameUniqueIdentifier", appNameUniqueIdentifier, "err", err)
473-
return app, err
473+
return app, false, err
474474
}
475475
if err == pg.ErrMultiRows {
476476
validApp, err := impl.dbMigration.FixMultipleAppsForInstalledApp(appNameUniqueIdentifier)
477477
if err != nil {
478478
impl.logger.Errorw("error in fixing multiple installed app entries", "appName", appNameUniqueIdentifier, "err", err)
479-
return app, err
479+
return app, false, err
480480
}
481-
return validApp, err
481+
return validApp, false, err
482482
}
483483
if util.IsErrNoRows(err) {
484484
//find app by display name if not found by unique identifier
@@ -487,16 +487,28 @@ func (impl AppCrudOperationServiceImpl) getAppAndProjectForAppIdentifier(appIden
487487
validApp, err := impl.dbMigration.FixMultipleAppsForInstalledApp(appNameUniqueIdentifier)
488488
if err != nil {
489489
impl.logger.Errorw("error in fixing multiple installed app entries", "appName", appNameUniqueIdentifier, "err", err)
490-
return app, err
490+
return app, false, err
491491
}
492-
return validApp, err
492+
return validApp, false, err
493493
}
494494
if err != nil {
495495
impl.logger.Errorw("error in fetching app meta data by display name", "displayName", appIdentifier.ReleaseName, "err", err)
496-
return app, err
496+
return app, false, err
497+
}
498+
// there can be a case when an app whose installed_app is deployed via argocd and same appName is also deployed externally
499+
// via helm then we need to check if app model found is not deployed by argocd.
500+
isManagedByArgocd, err := impl.installedAppDbService.IsChartStoreAppManagedByArgoCd(app.Id)
501+
if err != nil {
502+
impl.logger.Errorw("error in checking if installed app linked to this app is managed via argocd or not ", "appId", app.Id, "err", err)
503+
return app, false, err
504+
}
505+
if isManagedByArgocd {
506+
// if this helm app is managed by argocd then we don't want to process this req. any further.
507+
return app, true, nil
497508
}
498509
}
499-
return app, nil
510+
511+
return app, false, nil
500512
}
501513

502514
// updateAppNameToUniqueAppIdentifierInApp, migrates values of app_name col. in app table to unique identifier and also updates display_name with releaseName
@@ -540,18 +552,25 @@ func (impl AppCrudOperationServiceImpl) GetHelmAppMetaInfo(appId string) (*bean.
540552
app := &appRepository.App{}
541553
var err error
542554
var displayName string
555+
var isManagedByArgocd bool
543556
impl.logger.Info("request payload, appId", appId)
544557
if len(appIdSplitted) > 1 {
545558
appIdDecoded, err := client.DecodeExternalAppAppId(appId)
546559
if err != nil {
547560
impl.logger.Errorw("error in decoding app id for external app", "appId", appId, "err", err)
548561
return nil, err
549562
}
550-
app, err = impl.getAppAndProjectForAppIdentifier(appIdDecoded)
563+
app, isManagedByArgocd, err = impl.getAppAndProjectForAppIdentifier(appIdDecoded)
551564
if err != nil && !util.IsErrNoRows(err) {
552565
impl.logger.Errorw("GetHelmAppMetaInfo, error in getAppAndProjectForAppIdentifier for external apps", "appIdentifier", appIdDecoded, "err", err)
553566
return nil, err
554567
}
568+
if isManagedByArgocd {
569+
info := &bean.AppMetaInfoDto{
570+
AppName: appIdDecoded.ReleaseName,
571+
}
572+
return info, nil
573+
}
555574
// if app.DisplayName is empty then that app_name is not yet migrated to app name unique identifier
556575
if app.Id > 0 && len(app.DisplayName) == 0 {
557576
appNameUniqueIdentifier := appIdDecoded.GetUniqueAppNameIdentifier()

pkg/appStore/installedApp/read/InstalledAppReadEAService.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package read
22

33
import (
4+
"github.com/devtron-labs/devtron/internal/util"
45
"github.com/devtron-labs/devtron/pkg/appStore/installedApp/read/adapter"
56
"github.com/devtron-labs/devtron/pkg/appStore/installedApp/read/bean"
67
"github.com/devtron-labs/devtron/pkg/appStore/installedApp/repository"
@@ -32,6 +33,8 @@ type InstalledAppReadServiceEA interface {
3233
// Additional details like app store details are also fetched.
3334
// Refer bean.InstalledAppVersionWithAppStoreDetails for more details.
3435
GetInstalledAppVersionIncludingDeleted(installedAppVersionId int) (*bean.InstalledAppVersionWithAppStoreDetails, error)
36+
// IsChartStoreAppManagedByArgoCd returns if a chart store app is deployed via argo-cd or not
37+
IsChartStoreAppManagedByArgoCd(appId int) (bool, error)
3538
}
3639

3740
type InstalledAppReadServiceEAImpl struct {
@@ -97,3 +100,12 @@ func (impl *InstalledAppReadServiceEAImpl) GetInstalledAppVersionIncludingDelete
97100
}
98101
return adapter.GetInstalledAppVersionWithAppStoreDetails(installedAppVersionModel), nil
99102
}
103+
104+
func (impl *InstalledAppReadServiceEAImpl) IsChartStoreAppManagedByArgoCd(appId int) (bool, error) {
105+
installedAppModel, err := impl.installedAppRepository.GetInstalledAppsMinByAppId(appId)
106+
if err != nil {
107+
impl.logger.Errorw("error while fetching installed apps by app id", "appId", appId, "error", err)
108+
return false, err
109+
}
110+
return util.IsAcdApp(installedAppModel.DeploymentAppType), nil
111+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ type InstalledAppDBService interface {
6969
GetAppStoreApplicationVersionIdByInstalledAppVersionHistoryId(version int) (int, error)
7070
GetInstalledAppVersionHistoryByVersionId(id int) ([]*appStoreRepo.InstalledAppVersionHistory, error)
7171
UpdateDeploymentHistoryMessage(installedAppVersionHistoryId int, message string) error
72+
73+
IsChartStoreAppManagedByArgoCd(appId int) (bool, error)
7274
}
7375

7476
type InstalledAppDBServiceImpl struct {
@@ -533,3 +535,7 @@ func (impl *InstalledAppDBServiceImpl) MarkInstalledAppVersionModelInActive(inst
533535
}
534536
return nil
535537
}
538+
539+
func (impl *InstalledAppDBServiceImpl) IsChartStoreAppManagedByArgoCd(appId int) (bool, error) {
540+
return impl.deploymentConfigService.IsChartStoreAppManagedByArgoCd(appId)
541+
}

pkg/deployment/common/deploymentConfigService.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type DeploymentConfigService interface {
3939
GetConfigForDevtronApps(appId, envId int) (*bean.DeploymentConfig, error)
4040
GetAndMigrateConfigIfAbsentForDevtronApps(appId, envId int) (*bean.DeploymentConfig, error)
4141
GetConfigForHelmApps(appId, envId int) (*bean.DeploymentConfig, error)
42+
IsChartStoreAppManagedByArgoCd(appId int) (bool, error)
4243
GetConfigEvenIfInactive(appId, envId int) (*bean.DeploymentConfig, error)
4344
GetAndMigrateConfigIfAbsentForHelmApp(appId, envId int) (*bean.DeploymentConfig, error)
4445
GetAppLevelConfigForDevtronApp(appId int) (*bean.DeploymentConfig, error)
@@ -387,6 +388,17 @@ func (impl *DeploymentConfigServiceImpl) GetConfigForHelmApps(appId, envId int)
387388
return ConvertDeploymentConfigDbObjToDTO(helmDeploymentConfig), nil
388389
}
389390

391+
func (impl *DeploymentConfigServiceImpl) IsChartStoreAppManagedByArgoCd(appId int) (bool, error) {
392+
deploymentAppType, err := impl.deploymentConfigRepository.GetDeploymentAppTypeForChartStoreAppByAppId(appId)
393+
if err != nil && !util2.IsErrNoRows(err) {
394+
impl.logger.Errorw("error in GetDeploymentAppTypeForChartStoreAppByAppId", "appId", appId, "err", err)
395+
return false, err
396+
} else if util2.IsErrNoRows(err) {
397+
return impl.installedAppReadService.IsChartStoreAppManagedByArgoCd(appId)
398+
}
399+
return util2.IsAcdApp(deploymentAppType), nil
400+
}
401+
390402
func (impl *DeploymentConfigServiceImpl) GetConfigEvenIfInactive(appId, envId int) (*bean.DeploymentConfig, error) {
391403
config, err := impl.deploymentConfigRepository.GetByAppIdAndEnvIdEvenIfInactive(appId, envId)
392404
if err != nil {

0 commit comments

Comments
 (0)