Skip to content

Commit 0926759

Browse files
committed
argo app listing api updated
1 parent b0f9e5a commit 0926759

File tree

6 files changed

+121
-25
lines changed

6 files changed

+121
-25
lines changed

internal/sql/repository/deploymentConfig/repository.go

+24-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ type Repository interface {
6262
GetAppAndEnvLevelConfigsInBulk(appIdToEnvIdsMap map[int][]int) ([]*DeploymentConfig, error)
6363
GetByAppIdAndEnvIdEvenIfInactive(appId, envId int) (*DeploymentConfig, error)
6464
GetConfigByAppIds(appIds []int) ([]*DeploymentConfig, error)
65-
GetAllConfigs() ([]*DeploymentConfig, error)
65+
GetAllAppLevelConfigs() ([]*DeploymentConfig, error)
66+
GetAllEnvLevelConfigsWithReleaseMode(releaseMode string) ([]*DeploymentConfig, error)
6667
}
6768

6869
type RepositoryImpl struct {
@@ -236,11 +237,11 @@ func (impl *RepositoryImpl) GetConfigByAppIds(appIds []int) ([]*DeploymentConfig
236237
return results, err
237238
}
238239

239-
// GetAllConfigs returns all deployment configs for active apps
240+
// GetAllAppLevelConfigs returns all deployment configs for active apps
240241
// INNER JOIN app a is used to filter out inactive apps
241242
// NOTE: earlier we were not deleting the deployment configs on app delete,
242243
// so we need to filter out inactive deployment configs
243-
func (impl *RepositoryImpl) GetAllConfigs() ([]*DeploymentConfig, error) {
244+
func (impl *RepositoryImpl) GetAllAppLevelConfigs() ([]*DeploymentConfig, error) {
244245
result := make([]*DeploymentConfig, 0)
245246
err := impl.dbConnection.Model(&result).
246247
Join("INNER JOIN app a").
@@ -250,3 +251,23 @@ func (impl *RepositoryImpl) GetAllConfigs() ([]*DeploymentConfig, error) {
250251
Select()
251252
return result, err
252253
}
254+
255+
// GetAllEnvLevelConfigsWithReleaseMode returns all deployment configs for active apps and envs
256+
// INNER JOIN app a is used to filter out inactive apps
257+
// INNER JOIN environment e is used to filter out inactive envs
258+
// NOTE: earlier we were not deleting the deployment configs on app delete,
259+
// so we need to filter out inactive deployment configs
260+
func (impl *RepositoryImpl) GetAllEnvLevelConfigsWithReleaseMode(releaseMode string) ([]*DeploymentConfig, error) {
261+
result := make([]*DeploymentConfig, 0)
262+
err := impl.dbConnection.Model(&result).
263+
Join("INNER JOIN app a").
264+
JoinOn("deployment_config.app_id = a.id").
265+
Join("INNER JOIN environment e").
266+
JoinOn("deployment_config.environment_id = e.id").
267+
Where("a.active = ?", true).
268+
Where("e.active = ?", true).
269+
Where("deployment_config.active = ?", true).
270+
Where("deployment_config.release_mode = ?", releaseMode).
271+
Select()
272+
return result, err
273+
}

internal/sql/repository/pipelineConfig/PipelineRepository.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ type PipelineRepository interface {
134134
FindDeploymentAppTypeByAppIdAndEnvId(appId, envId int) (string, error)
135135
FindByAppIdToEnvIdsMapping(appIdToEnvIds map[int][]int) ([]*Pipeline, error)
136136
FindDeploymentAppTypeByIds(ids []int) (pipelines []*Pipeline, err error)
137-
GetAllArgoAppNamesByCluster(clusterIds []int) ([]string, error)
137+
GetAllArgoAppNamesByCluster(clusterIds []int) ([]*PipelineDeploymentConfigObj, error)
138138
}
139139

140140
type CiArtifactDTO struct {
@@ -153,6 +153,12 @@ type DeploymentObject struct {
153153
Status string `sql:"status"`
154154
}
155155

156+
type PipelineDeploymentConfigObj struct {
157+
DeploymentAppName string `json:"deployment_app_name"`
158+
AppId int `json:"app_id"`
159+
EnvironmentId int `json:"environment_id"`
160+
}
161+
156162
type PipelineRepositoryImpl struct {
157163
dbConnection *pg.DB
158164
logger *zap.SugaredLogger
@@ -862,14 +868,16 @@ func (impl *PipelineRepositoryImpl) FindDeploymentAppTypeByIds(ids []int) (pipel
862868
return pipelines, err
863869
}
864870

865-
func (impl *PipelineRepositoryImpl) GetAllArgoAppNamesByCluster(clusterIds []int) ([]string, error) {
866-
result := make([]string, 0)
871+
func (impl *PipelineRepositoryImpl) GetAllArgoAppNamesByCluster(clusterIds []int) ([]*PipelineDeploymentConfigObj, error) {
872+
result := make([]*PipelineDeploymentConfigObj, 0)
867873
if len(clusterIds) == 0 {
868874
return result, nil
869875
}
870876
err := impl.dbConnection.Model().
871877
Table("pipeline").
872-
ColumnExpr("deployment_app_name").
878+
ColumnExpr("pipeline.deployment_app_name AS deployment_app_name").
879+
ColumnExpr("pipeline.app_id AS app_id").
880+
ColumnExpr("pipeline.environment_id AS environment_id").
873881
// inner join with app
874882
Join("INNER JOIN app").
875883
JoinOn("pipeline.app_id = app.id").

pkg/argoApplication/ArgoApplicationService.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ import (
3131
"github.com/devtron-labs/devtron/pkg/cluster/adapter"
3232
clusterRepository "github.com/devtron-labs/devtron/pkg/cluster/repository"
3333
"github.com/devtron-labs/devtron/pkg/deployment/common"
34+
commonBean "github.com/devtron-labs/devtron/pkg/deployment/common/bean"
3435
"github.com/devtron-labs/devtron/pkg/k8s/application"
3536
k8s2 "github.com/devtron-labs/devtron/pkg/k8s/bean"
3637
"github.com/devtron-labs/devtron/util"
3738
"github.com/devtron-labs/devtron/util/sliceUtil"
3839
"go.uber.org/zap"
3940
"k8s.io/apimachinery/pkg/api/errors"
4041
"net/http"
41-
"slices"
4242
)
4343

4444
type ArgoApplicationService interface {
@@ -141,14 +141,19 @@ func (impl *ArgoApplicationServiceImpl) ListApplications(clusterIds []int) ([]*b
141141
appListClusterIds := sliceUtil.NewSliceFromFuncExec(appListFinal, func(app *bean.ArgoApplicationListDto) int {
142142
return app.ClusterId
143143
})
144-
allDevtronManagedArgoAppNames, err := impl.deploymentConfigService.GetAllArgoAppNamesByCluster(appListClusterIds)
144+
allDevtronManagedArgoAppsInfo, err := impl.deploymentConfigService.GetAllArgoAppNamesByCluster(appListClusterIds)
145145
if err != nil {
146146
impl.logger.Errorw("error in getting all argo app names by cluster", "err", err, "clusterIds", appListClusterIds)
147147
return nil, err
148148
}
149149
filteredAppList := make([]*bean.ArgoApplicationListDto, 0)
150150
filteredAppList = sliceUtil.Filter(filteredAppList, appListFinal, func(app *bean.ArgoApplicationListDto) bool {
151-
return !slices.Contains(allDevtronManagedArgoAppNames, app.Name)
151+
_, found := sliceUtil.Find(allDevtronManagedArgoAppsInfo, func(info *commonBean.DevtronArgoCdAppInfo) bool {
152+
return info.ArgoAppClusterId == app.ClusterId &&
153+
info.ArgoAppNamespace == app.Namespace &&
154+
info.ArgoCdAppName == app.Name
155+
})
156+
return !found
152157
})
153158
return filteredAppList, nil
154159
}

pkg/deployment/common/adapter/adapter.go

+8
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,11 @@ func GetDeploymentConfigType(isCustomGitOpsRepo bool) string {
7979
}
8080
return string(bean.SYSTEM_GENERATED)
8181
}
82+
83+
func GetDevtronArgoCdAppInfo(acdAppName string, acdAppClusterId int, acdDefaultNamespace string) *bean.DevtronArgoCdAppInfo {
84+
return &bean.DevtronArgoCdAppInfo{
85+
ArgoCdAppName: acdAppName,
86+
ArgoAppClusterId: acdAppClusterId,
87+
ArgoAppNamespace: acdDefaultNamespace,
88+
}
89+
}

pkg/deployment/common/bean/bean.go

+6
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,9 @@ func (d *DeploymentConfig) GetMigratedFrom() (migratedFrom ExternalReleaseType,
383383
}
384384
return Undefined, false
385385
}
386+
387+
type DevtronArgoCdAppInfo struct {
388+
ArgoCdAppName string
389+
ArgoAppClusterId int
390+
ArgoAppNamespace string
391+
}

pkg/deployment/common/deploymentConfigService.go

+63-15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package common
1818

1919
import (
2020
"errors"
21+
"fmt"
2122
"github.com/devtron-labs/common-lib/utils/k8s/commonBean"
2223
"github.com/devtron-labs/devtron/client/argocdServer"
2324
appRepository "github.com/devtron-labs/devtron/internal/sql/repository/app"
@@ -27,16 +28,16 @@ import (
2728
installedAppReader "github.com/devtron-labs/devtron/pkg/appStore/installedApp/read"
2829
bean3 "github.com/devtron-labs/devtron/pkg/auth/user/bean"
2930
chartRepoRepository "github.com/devtron-labs/devtron/pkg/chartRepo/repository"
30-
bean2 "github.com/devtron-labs/devtron/pkg/cluster/bean"
31+
clusterBean "github.com/devtron-labs/devtron/pkg/cluster/bean"
3132
bean4 "github.com/devtron-labs/devtron/pkg/cluster/environment/bean"
3233
"github.com/devtron-labs/devtron/pkg/cluster/environment/repository"
3334
"github.com/devtron-labs/devtron/pkg/deployment/common/adapter"
3435
"github.com/devtron-labs/devtron/pkg/deployment/common/bean"
3536
commonErr "github.com/devtron-labs/devtron/pkg/deployment/common/errors"
3637
read2 "github.com/devtron-labs/devtron/pkg/deployment/common/read"
3738
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate/read"
39+
util3 "github.com/devtron-labs/devtron/pkg/util"
3840
"github.com/devtron-labs/devtron/util"
39-
"github.com/devtron-labs/devtron/util/sliceUtil"
4041
"github.com/go-pg/pg"
4142
"go.uber.org/zap"
4243
"path/filepath"
@@ -53,7 +54,7 @@ type DeploymentConfigService interface {
5354
UpdateRepoUrlForAppAndEnvId(repoURL string, appId, envId int) error
5455
GetConfigsByAppIds(appIds []int) ([]*bean.DeploymentConfig, error)
5556
UpdateChartLocationInDeploymentConfig(appId, envId, chartRefId int, userId int32, chartVersion string) error
56-
GetAllArgoAppNamesByCluster(clusterIds []int) ([]string, error)
57+
GetAllArgoAppNamesByCluster(clusterIds []int) ([]*bean.DevtronArgoCdAppInfo, error)
5758
GetExternalReleaseType(appId, environmentId int) (bean.ExternalReleaseType, error)
5859
CheckIfURLAlreadyPresent(repoURL string) (bool, error)
5960
FilterPipelinesByApplicationClusterIdAndNamespace(pipelines []pipelineConfig.Pipeline, applicationObjectClusterId int, applicationObjectNamespace string) (pipelineConfig.Pipeline, error)
@@ -71,6 +72,7 @@ type DeploymentConfigServiceImpl struct {
7172
environmentRepository repository.EnvironmentRepository
7273
chartRefRepository chartRepoRepository.ChartRefRepository
7374
deploymentConfigReadService read2.DeploymentConfigReadService
75+
acdAuthConfig *util3.ACDAuthConfig
7476
}
7577

7678
func NewDeploymentConfigServiceImpl(
@@ -85,6 +87,7 @@ func NewDeploymentConfigServiceImpl(
8587
environmentRepository repository.EnvironmentRepository,
8688
chartRefRepository chartRepoRepository.ChartRefRepository,
8789
deploymentConfigReadService read2.DeploymentConfigReadService,
90+
acdAuthConfig *util3.ACDAuthConfig,
8891
) *DeploymentConfigServiceImpl {
8992

9093
return &DeploymentConfigServiceImpl{
@@ -99,6 +102,7 @@ func NewDeploymentConfigServiceImpl(
99102
environmentRepository: environmentRepository,
100103
chartRefRepository: chartRefRepository,
101104
deploymentConfigReadService: deploymentConfigReadService,
105+
acdAuthConfig: acdAuthConfig,
102106
}
103107
}
104108

@@ -327,21 +331,47 @@ func (impl *DeploymentConfigServiceImpl) UpdateChartLocationInDeploymentConfig(a
327331
return nil
328332
}
329333

330-
func (impl *DeploymentConfigServiceImpl) GetAllArgoAppNamesByCluster(clusterIds []int) ([]string, error) {
331-
allDevtronManagedArgoAppNames := make([]string, 0)
332-
devtronArgoAppNames, err := impl.pipelineRepository.GetAllArgoAppNamesByCluster(clusterIds)
334+
func (impl *DeploymentConfigServiceImpl) GetAllArgoAppNamesByCluster(clusterIds []int) ([]*bean.DevtronArgoCdAppInfo, error) {
335+
allDevtronManagedArgoAppsInfo := make([]*bean.DevtronArgoCdAppInfo, 0)
336+
linkedReleaseConfig, err := impl.getAllEnvLevelConfigsForLinkedReleases()
337+
if err != nil {
338+
impl.logger.Errorw("error while fetching linked release configs", "clusterIds", clusterIds, "error", err)
339+
return allDevtronManagedArgoAppsInfo, err
340+
}
341+
linkedReleaseConfigMap := make(map[string]*bean.DeploymentConfig)
342+
for _, config := range linkedReleaseConfig {
343+
uniqueKey := fmt.Sprintf("%d-%d", config.AppId, config.EnvironmentId)
344+
linkedReleaseConfigMap[uniqueKey] = config
345+
}
346+
devtronArgoAppsInfo, err := impl.pipelineRepository.GetAllArgoAppNamesByCluster(clusterIds)
333347
if err != nil {
334348
impl.logger.Errorw("error while fetching argo app names", "clusterIds", clusterIds, "error", err)
335-
return allDevtronManagedArgoAppNames, err
349+
return allDevtronManagedArgoAppsInfo, err
350+
}
351+
for _, acdAppInfo := range devtronArgoAppsInfo {
352+
uniqueKey := fmt.Sprintf("%d-%d", acdAppInfo.AppId, acdAppInfo.EnvironmentId)
353+
var devtronArgoCdAppInfo *bean.DevtronArgoCdAppInfo
354+
if config, ok := linkedReleaseConfigMap[uniqueKey]; ok &&
355+
config.IsAcdRelease() && config.IsLinkedRelease() {
356+
acdAppClusterId := config.GetApplicationObjectClusterId()
357+
acdDefaultNamespace := config.GetApplicationObjectNamespace()
358+
devtronArgoCdAppInfo = adapter.GetDevtronArgoCdAppInfo(acdAppInfo.DeploymentAppName, acdAppClusterId, acdDefaultNamespace)
359+
} else {
360+
devtronArgoCdAppInfo = adapter.GetDevtronArgoCdAppInfo(acdAppInfo.DeploymentAppName, clusterBean.DefaultClusterId, impl.acdAuthConfig.ACDConfigMapNamespace)
361+
}
362+
allDevtronManagedArgoAppsInfo = append(allDevtronManagedArgoAppsInfo, devtronArgoCdAppInfo)
336363
}
337-
allDevtronManagedArgoAppNames = append(allDevtronManagedArgoAppNames, devtronArgoAppNames...)
338364
chartStoreArgoAppNames, err := impl.installedAppReadService.GetAllArgoAppNamesByCluster(clusterIds)
339365
if err != nil {
340366
impl.logger.Errorw("error while fetching argo app names from chart store", "clusterIds", clusterIds, "error", err)
341-
return allDevtronManagedArgoAppNames, err
367+
return allDevtronManagedArgoAppsInfo, err
342368
}
343-
allDevtronManagedArgoAppNames = append(allDevtronManagedArgoAppNames, chartStoreArgoAppNames...)
344-
return sliceUtil.GetUniqueElements(allDevtronManagedArgoAppNames), nil
369+
for _, chartStoreArgoAppName := range chartStoreArgoAppNames {
370+
// NOTE: Chart Store doesn't support linked releases
371+
chartStoreArgoCdAppInfo := adapter.GetDevtronArgoCdAppInfo(chartStoreArgoAppName, clusterBean.DefaultClusterId, impl.acdAuthConfig.ACDConfigMapNamespace)
372+
allDevtronManagedArgoAppsInfo = append(allDevtronManagedArgoAppsInfo, chartStoreArgoCdAppInfo)
373+
}
374+
return allDevtronManagedArgoAppsInfo, nil
345375
}
346376

347377
func (impl *DeploymentConfigServiceImpl) GetExternalReleaseType(appId, environmentId int) (bean.ExternalReleaseType, error) {
@@ -358,7 +388,7 @@ func (impl *DeploymentConfigServiceImpl) GetExternalReleaseType(appId, environme
358388

359389
func (impl *DeploymentConfigServiceImpl) CheckIfURLAlreadyPresent(repoURL string) (bool, error) {
360390
//TODO: optimisation
361-
configs, err := impl.getAllConfigsWithCustomGitOpsURL()
391+
configs, err := impl.getAllAppLevelConfigsWithCustomGitOpsURL()
362392
if err != nil {
363393
impl.logger.Errorw("error in getting all configs", "err", err)
364394
return false, err
@@ -486,7 +516,7 @@ func (impl *DeploymentConfigServiceImpl) parseReleaseConfigForHelmApps(appId int
486516
Version: bean.Version,
487517
ArgoCDSpec: bean.ArgoCDSpec{
488518
Metadata: bean.ApplicationMetadata{
489-
ClusterId: bean2.DefaultClusterId,
519+
ClusterId: clusterBean.DefaultClusterId,
490520
Namespace: argocdServer.DevtronInstalationNs,
491521
},
492522
Spec: bean.ApplicationSpec{
@@ -509,8 +539,8 @@ func (impl *DeploymentConfigServiceImpl) parseReleaseConfigForHelmApps(appId int
509539
return releaseConfig, nil
510540
}
511541

512-
func (impl *DeploymentConfigServiceImpl) getAllConfigsWithCustomGitOpsURL() ([]*bean.DeploymentConfig, error) {
513-
dbConfigs, err := impl.deploymentConfigRepository.GetAllConfigs()
542+
func (impl *DeploymentConfigServiceImpl) getAllAppLevelConfigsWithCustomGitOpsURL() ([]*bean.DeploymentConfig, error) {
543+
dbConfigs, err := impl.deploymentConfigRepository.GetAllAppLevelConfigs()
514544
if err != nil {
515545
impl.logger.Errorw("error in getting all configs with custom gitops url", "err", err)
516546
return nil, err
@@ -527,6 +557,24 @@ func (impl *DeploymentConfigServiceImpl) getAllConfigsWithCustomGitOpsURL() ([]*
527557
return configs, nil
528558
}
529559

560+
func (impl *DeploymentConfigServiceImpl) getAllEnvLevelConfigsForLinkedReleases() ([]*bean.DeploymentConfig, error) {
561+
dbConfigs, err := impl.deploymentConfigRepository.GetAllEnvLevelConfigsWithReleaseMode(util2.PIPELINE_RELEASE_MODE_LINK)
562+
if err != nil {
563+
impl.logger.Errorw("error in getting all env level configs with custom gitops url", "err", err)
564+
return nil, err
565+
}
566+
configs := make([]*bean.DeploymentConfig, 0)
567+
for _, dbConfig := range dbConfigs {
568+
config, err := adapter.ConvertDeploymentConfigDbObjToDTO(dbConfig)
569+
if err != nil {
570+
impl.logger.Error("error in converting dbObj to dto", "err", err)
571+
return nil, err
572+
}
573+
configs = append(configs, config)
574+
}
575+
return configs, nil
576+
}
577+
530578
func (impl *DeploymentConfigServiceImpl) GetConfigDBObj(appId, envId int) (*deploymentConfig.DeploymentConfig, error) {
531579
var configDbObj *deploymentConfig.DeploymentConfig
532580
var err error

0 commit comments

Comments
 (0)