Skip to content

Commit 4b8d72f

Browse files
authored
Merge pull request #6642 from devtron-labs/chart-service-refactoring
chore: chart service crud and configProperties
2 parents 7c6fbaf + fbb4af5 commit 4b8d72f

34 files changed

+365
-279
lines changed

api/restHandler/app/appList/AppListingRestHandler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ func (handler AppListingRestHandlerImpl) FetchResourceTree(w http.ResponseWriter
567567
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), nil, http.StatusForbidden)
568568
return
569569
}
570-
envDeploymentConfig, err := handler.deploymentConfigService.GetConfigForDevtronApps(appId, envId)
570+
envDeploymentConfig, err := handler.deploymentConfigService.GetConfigForDevtronApps(nil, appId, envId)
571571
if err != nil {
572572
handler.logger.Errorw("error in fetching deployment config", "appId", appId, "envId", envId, "err", err)
573573
common.WriteJsonResp(w, fmt.Errorf("error in getting deployment config for env"), nil, http.StatusInternalServerError)
@@ -887,7 +887,7 @@ func (handler AppListingRestHandlerImpl) GetHostUrlsByBatch(w http.ResponseWrite
887887
}
888888

889889
cdPipeline := pipelines[0]
890-
envDeploymentConfig, err := handler.deploymentConfigService.GetConfigForDevtronApps(appId, envId)
890+
envDeploymentConfig, err := handler.deploymentConfigService.GetConfigForDevtronApps(nil, appId, envId)
891891
if err != nil {
892892
handler.logger.Errorw("error in fetching deployment config", "appId", appId, "envId", envId, "err", err)
893893
common.WriteJsonResp(w, fmt.Errorf("error in getting deployment config for env"), nil, http.StatusInternalServerError)

cmd/external-app/wire_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/sql/repository/chartConfig/EnvConfigOverrideRepository.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/devtron-labs/devtron/pkg/cluster/environment/repository"
2323
"github.com/devtron-labs/devtron/pkg/sql"
2424
"github.com/go-pg/pg"
25+
"github.com/go-pg/pg/orm"
2526
"github.com/juju/errors"
2627
)
2728

@@ -58,16 +59,16 @@ type EnvConfigOverrideRepository interface {
5859
ActiveEnvConfigOverride(appId, environmentId int) (*EnvConfigOverride, error) //successful env config
5960
GetByIdIncludingInactive(id int) (*EnvConfigOverride, error)
6061
//this api updates only EnvOverrideValues, EnvMergedValues, Status, ManualReviewed, active based on id
61-
UpdateProperties(config *EnvConfigOverride) error
62+
UpdateProperties(tx *pg.Tx, config *EnvConfigOverride) error
6263
GetByEnvironment(targetEnvironmentId int) ([]EnvConfigOverride, error)
6364

6465
GetEnvConfigByChartId(chartId int) ([]EnvConfigOverride, error)
6566
UpdateEnvConfigStatus(config *EnvConfigOverride) error
6667
Delete(envConfigOverride *EnvConfigOverride) error
67-
FindLatestChartForAppByAppIdAndEnvId(appId, targetEnvironmentId int) (*EnvConfigOverride, error)
68+
FindLatestChartForAppByAppIdAndEnvId(tx *pg.Tx, appId, targetEnvironmentId int) (*EnvConfigOverride, error)
6869
FindChartRefIdsForLatestChartForAppByAppIdAndEnvIds(appId int, targetEnvironmentIds []int) (map[int]int, error)
6970
FindChartByAppIdAndEnvIdAndChartRefId(appId, targetEnvironmentId int, chartRefId int) (*EnvConfigOverride, error)
70-
Update(envConfigOverride *EnvConfigOverride) (*EnvConfigOverride, error)
71+
Update(tx *pg.Tx, envConfigOverride *EnvConfigOverride) (*EnvConfigOverride, error)
7172
FindChartForAppByAppIdAndEnvId(appId, targetEnvironmentId int) (*EnvConfigOverride, error)
7273
SaveWithTxn(model *EnvConfigOverride, tx *pg.Tx) error
7374
UpdateWithTxn(envConfigOverride *EnvConfigOverride, tx *pg.Tx) (*EnvConfigOverride, error)
@@ -78,6 +79,7 @@ type EnvConfigOverrideRepository interface {
7879
// EnvConfigOverride.Chart is not populated,
7980
// as the chartRepoRepository.Chart contains the reference chart(in bytes).
8081
GetAllOverridesForApp(appId int) ([]*EnvConfigOverride, error)
82+
GetDbConnection() *pg.DB
8183
}
8284

8385
type EnvConfigOverrideRepositoryImpl struct {
@@ -205,8 +207,13 @@ func (r EnvConfigOverrideRepositoryImpl) GetByIdIncludingInactive(id int) (*EnvC
205207

206208
// this api updates only EnvOverrideValues, EnvMergedValues, Status, ManualReviewed, active
207209
// based on id
208-
func (r EnvConfigOverrideRepositoryImpl) UpdateProperties(config *EnvConfigOverride) error {
209-
_, err := r.dbConnection.Model(config).
210+
func (r EnvConfigOverrideRepositoryImpl) UpdateProperties(tx *pg.Tx, config *EnvConfigOverride) error {
211+
var connection orm.DB
212+
connection = tx
213+
if tx == nil {
214+
connection = r.dbConnection
215+
}
216+
_, err := connection.Model(config).
210217
Set("env_override_yaml = ?", config.EnvOverrideValues).
211218
Set("status =?", config.Status).
212219
Set("reviewed =?", config.ManualReviewed).
@@ -272,9 +279,14 @@ func (r EnvConfigOverrideRepositoryImpl) Delete(envConfigOverride *EnvConfigOver
272279
return err
273280
}
274281

275-
func (r EnvConfigOverrideRepositoryImpl) FindLatestChartForAppByAppIdAndEnvId(appId, targetEnvironmentId int) (*EnvConfigOverride, error) {
282+
func (r EnvConfigOverrideRepositoryImpl) FindLatestChartForAppByAppIdAndEnvId(tx *pg.Tx, appId, targetEnvironmentId int) (*EnvConfigOverride, error) {
276283
eco := &EnvConfigOverride{}
277-
err := r.dbConnection.
284+
var connection orm.DB
285+
connection = tx
286+
if tx == nil {
287+
connection = r.dbConnection
288+
}
289+
err := connection.
278290
Model(eco).
279291
Where("env_config_override.target_environment = ?", targetEnvironmentId).
280292
Where("env_config_override.latest = ?", true).
@@ -321,8 +333,13 @@ func (r EnvConfigOverrideRepositoryImpl) FindChartByAppIdAndEnvIdAndChartRefId(a
321333
return eco, err
322334
}
323335

324-
func (r EnvConfigOverrideRepositoryImpl) Update(envConfigOverride *EnvConfigOverride) (*EnvConfigOverride, error) {
325-
err := r.dbConnection.Update(envConfigOverride)
336+
func (r EnvConfigOverrideRepositoryImpl) Update(tx *pg.Tx, envConfigOverride *EnvConfigOverride) (*EnvConfigOverride, error) {
337+
var connection orm.DB
338+
connection = tx
339+
if tx == nil {
340+
connection = r.dbConnection
341+
}
342+
err := connection.Update(envConfigOverride)
326343
return envConfigOverride, err
327344
}
328345

@@ -380,3 +397,7 @@ func (r EnvConfigOverrideRepositoryImpl) GetAllOverridesForApp(appId int) ([]*En
380397
Select()
381398
return eco, err
382399
}
400+
401+
func (r EnvConfigOverrideRepositoryImpl) GetDbConnection() *pg.DB {
402+
return r.dbConnection
403+
}

internal/sql/repository/deploymentConfig/repository.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ func (impl *RepositoryImpl) UpdateAll(tx *pg.Tx, configs []*DeploymentConfig) ([
123123

124124
func (impl *RepositoryImpl) GetByAppIdAndEnvId(tx *pg.Tx, appId, envId int) (*DeploymentConfig, error) {
125125
result := &DeploymentConfig{}
126-
var query *orm.Query
126+
var connection orm.DB
127127
if tx != nil {
128-
query = tx.Model(result)
128+
connection = tx
129129
} else {
130-
query = impl.dbConnection.Model(result)
130+
connection = impl.dbConnection
131131
}
132-
err := query.
132+
err := connection.Model(result).
133133
Join("INNER JOIN app a").
134134
JoinOn("deployment_config.app_id = a.id").
135135
Join("INNER JOIN environment e").
@@ -146,13 +146,13 @@ func (impl *RepositoryImpl) GetByAppIdAndEnvId(tx *pg.Tx, appId, envId int) (*De
146146

147147
func (impl *RepositoryImpl) GetAppLevelConfigForDevtronApps(tx *pg.Tx, appId int) (*DeploymentConfig, error) {
148148
result := &DeploymentConfig{}
149-
var query *orm.Query
149+
var connection orm.DB
150150
if tx != nil {
151-
query = tx.Model(result)
151+
connection = tx
152152
} else {
153-
query = impl.dbConnection.Model(result)
153+
connection = impl.dbConnection
154154
}
155-
err := query.
155+
err := connection.Model(result).
156156
Join("INNER JOIN app a").
157157
JoinOn("deployment_config.app_id = a.id").
158158
Where("a.active = ?", true).

pkg/app/AppListingService.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ func (impl AppListingServiceImpl) FetchOtherEnvironment(ctx context.Context, app
760760
return envs, err
761761
}
762762
newCtx, span = otel.Tracer("chartRepository").Start(newCtx, "FindLatestChartForAppByAppId")
763-
chart, err := impl.chartRepository.FindLatestChartForAppByAppId(appId)
763+
chart, err := impl.chartRepository.FindLatestChartForAppByAppId(nil, appId)
764764
span.End()
765765
if err != nil && err != pg.ErrNoRows {
766766
impl.Logger.Errorw("error in fetching latest chart", "err", err)
@@ -781,7 +781,7 @@ func (impl AppListingServiceImpl) FetchOtherEnvironment(ctx context.Context, app
781781
}
782782
for _, env := range envs {
783783
newCtx, span = otel.Tracer("envOverrideRepository").Start(newCtx, "FindLatestChartForAppByAppIdAndEnvId")
784-
envOverride, err := impl.envConfigOverrideReadService.FindLatestChartForAppByAppIdAndEnvId(appId, env.EnvironmentId)
784+
envOverride, err := impl.envConfigOverrideReadService.FindLatestChartForAppByAppIdAndEnvId(nil, appId, env.EnvironmentId)
785785
span.End()
786786
if err != nil && !errors2.IsNotFound(err) {
787787
impl.Logger.Errorw("error in fetching latest chart by appId and envId", "err", err, "appId", appId, "envId", env.EnvironmentId)

pkg/app/AppService.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ func (impl *AppServiceImpl) CheckIfPipelineUpdateEventIsValid(app *v1alpha1.Appl
488488
// drop event
489489
return isValid, pipeline, cdWfr, pipelineOverride, nil
490490
}
491-
deploymentConfig, err := impl.deploymentConfigService.GetConfigForDevtronApps(pipeline.AppId, pipeline.EnvironmentId)
491+
deploymentConfig, err := impl.deploymentConfigService.GetConfigForDevtronApps(nil, pipeline.AppId, pipeline.EnvironmentId)
492492
if err != nil {
493493
impl.logger.Errorw("error in getting deployment config by appId and environmentId", "appId", pipeline.AppId, "environmentId", pipeline.EnvironmentId, "err", err)
494494
return isValid, pipeline, cdWfr, pipelineOverride, err
@@ -787,7 +787,7 @@ type ValuesOverrideResponse struct {
787787

788788
func (impl *AppServiceImpl) CreateGitOpsRepo(app *app.App, targetRevision string, userId int32) (gitOpsRepoName string, chartGitAttr *commonBean.ChartGitAttribute, err error) {
789789

790-
deploymentConfig, err := impl.deploymentConfigService.GetConfigForDevtronApps(app.Id, 0)
790+
deploymentConfig, err := impl.deploymentConfigService.GetConfigForDevtronApps(nil, app.Id, 0)
791791
if err != nil {
792792
impl.logger.Errorw("error in getting deployment config for devtron apps", "appId", app.Id, "err", err)
793793
return "", nil, err
@@ -951,7 +951,7 @@ func (impl *AppServiceImpl) UpdateCdWorkflowRunnerByACDObject(app *v1alpha1.Appl
951951
}
952952
appId := wfr.CdWorkflow.Pipeline.AppId
953953
envId := wfr.CdWorkflow.Pipeline.EnvironmentId
954-
envDeploymentConfig, err := impl.deploymentConfigService.GetConfigForDevtronApps(appId, envId)
954+
envDeploymentConfig, err := impl.deploymentConfigService.GetConfigForDevtronApps(nil, appId, envId)
955955
if err != nil {
956956
impl.logger.Errorw("error in fetching environment deployment config by appId and envId", "appId", appId, "envId", envId, "err", err)
957957
return err

pkg/app/status/PipelineStatusTimelineService.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func (impl *PipelineStatusTimelineServiceImpl) FetchTimelines(appId, envId, wfrI
227227
triggeredBy = wfr.TriggeredBy
228228
wfrStatus = wfr.Status
229229

230-
envDeploymentConfig, err := impl.deploymentConfigService.GetConfigForDevtronApps(appId, envId)
230+
envDeploymentConfig, err := impl.deploymentConfigService.GetConfigForDevtronApps(nil, appId, envId)
231231
if err != nil {
232232
impl.logger.Errorw("error in fetching environment deployment config by appId and envId", "appId", appId, "envId", envId, "err", err)
233233
return nil, err

pkg/appWorkflow/AppWorkflowService.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ func (impl AppWorkflowServiceImpl) FindCdPipelinesByAppId(appId int) (*bean.CdPi
820820
}
821821

822822
for _, pipeline := range dbPipelines {
823-
envDeploymentConfig, err := impl.deploymentConfigService.GetConfigForDevtronApps(appId, pipeline.EnvironmentId)
823+
envDeploymentConfig, err := impl.deploymentConfigService.GetConfigForDevtronApps(nil, appId, pipeline.EnvironmentId)
824824
if err != nil {
825825
impl.Logger.Errorw("error in fetching environment deployment config by appId and envId", "appId", appId, "envId", pipeline.EnvironmentId, "err", err)
826826
return nil, err

0 commit comments

Comments
 (0)