Skip to content

Commit 2a2305c

Browse files
prakash100198komalreddy3
authored andcommitted
fix: fix for gitops repo url configured in argocd and stored in db are out of sync, then 3 commits are being done (#4912)
* handle gitops error case for monorepo migration * fixes * code review changes * code review changes- II * nil check for argoApplication and it's child for panic handling
1 parent 4f906d1 commit 2a2305c

File tree

4 files changed

+34
-68
lines changed

4 files changed

+34
-68
lines changed

pkg/appStore/installedApp/service/AppStoreDeploymentService.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,9 @@ func (impl *AppStoreDeploymentServiceImpl) UpdateInstalledApp(ctx context.Contex
671671
gitOpsResponse := &bean2.AppStoreGitOpsResponse{}
672672

673673
if installedAppDeploymentAction.PerformGitOps {
674+
// manifest contains ChartRepoName where the valuesConfig and requirementConfig files will get committed
675+
// and that gitOpsRepoUrl is extracted from db inside GenerateManifest func and not from the current
676+
// orchestrator cm prefix and appName.
674677
manifest, err := impl.fullModeDeploymentService.GenerateManifest(upgradeAppRequest)
675678
if err != nil {
676679
impl.logger.Errorw("error in generating manifest for helm apps", "err", err)
@@ -721,6 +724,10 @@ func (impl *AppStoreDeploymentServiceImpl) UpdateInstalledApp(ctx context.Contex
721724
}
722725
installedApp.UpdateStatus(appStoreBean.DEPLOY_SUCCESS)
723726
installedApp.UpdateAuditLog(upgradeAppRequest.UserId)
727+
if monoRepoMigrationRequired {
728+
//if monorepo case is true then repoUrl is changed then also update repo url in database
729+
installedApp.UpdateGitOpsRepository(gitOpsResponse.ChartGitAttribute.RepoUrl, installedApp.IsCustomRepository)
730+
}
724731
installedApp, err = impl.installedAppRepository.UpdateInstalledApp(installedApp, tx)
725732
if err != nil {
726733
impl.logger.Errorw("error in updating installed app", "err", err)

pkg/appStore/installedApp/service/FullMode/deployment/InstalledAppArgoCdService.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,30 @@ func (impl *FullModeDeploymentServiceImpl) CheckIfArgoAppExists(acdAppName strin
8787
return isFound, nil
8888
}
8989

90-
func (impl *FullModeDeploymentServiceImpl) UpdateAndSyncACDApps(installAppVersionRequest *appStoreBean.InstallAppVersionDTO, ChartGitAttribute *commonBean.ChartGitAttribute, isMonoRepoMigrationRequired bool, ctx context.Context, tx *pg.Tx) error {
91-
if isMonoRepoMigrationRequired {
92-
// update repo details on ArgoCD as repo is changed
93-
err := impl.UpgradeDeployment(installAppVersionRequest, ChartGitAttribute, 0, ctx)
94-
if err != nil {
95-
return err
96-
}
90+
func isArgoCdGitOpsRepoUrlOutOfSync(argoApplication *v1alpha1.Application, gitOpsRepoURLInDb string) bool {
91+
if argoApplication != nil && argoApplication.Spec.Source != nil {
92+
return argoApplication.Spec.Source.RepoURL != gitOpsRepoURLInDb
9793
}
94+
return false
95+
}
96+
97+
func (impl *FullModeDeploymentServiceImpl) UpdateAndSyncACDApps(installAppVersionRequest *appStoreBean.InstallAppVersionDTO, ChartGitAttribute *commonBean.ChartGitAttribute, isMonoRepoMigrationRequired bool, ctx context.Context, tx *pg.Tx) error {
9898
acdAppName := installAppVersionRequest.ACDAppName
9999
argoApplication, err := impl.acdClient.Get(ctx, &application.ApplicationQuery{Name: &acdAppName})
100100
if err != nil {
101101
impl.Logger.Errorw("Service err:UpdateAndSyncACDApps - error in acd app by name", "acdAppName", acdAppName, "err", err)
102102
return err
103103
}
104+
//if either monorepo case is true or there is diff. in git-ops repo url registered with argo-cd and git-ops repo url saved in db,
105+
//then sync argo with git-ops repo url from db because we have already pushed changes to that repo
106+
isArgoRepoUrlOutOfSync := isArgoCdGitOpsRepoUrlOutOfSync(argoApplication, installAppVersionRequest.GitOpsRepoURL)
107+
if isMonoRepoMigrationRequired || isArgoRepoUrlOutOfSync {
108+
// update repo details on ArgoCD as repo is changed
109+
err := impl.UpgradeDeployment(installAppVersionRequest, ChartGitAttribute, 0, ctx)
110+
if err != nil {
111+
return err
112+
}
113+
}
104114

105115
err = impl.argoClientWrapperService.UpdateArgoCDSyncModeIfNeeded(ctx, argoApplication)
106116
if err != nil {

pkg/appStore/installedApp/service/FullMode/deployment/InstalledAppGitOpsService.go

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919

2020
//"github.com/xanzy/go-gitlab"
2121
"net/http"
22-
"regexp"
2322
)
2423

2524
type InstalledAppGitOpsService interface {
@@ -47,18 +46,6 @@ func (impl *FullModeDeploymentServiceImpl) GitOpsOperations(manifestResponse *be
4746
impl.Logger.Errorw("Error in pushing chart to git", "err", err)
4847
return appStoreGitOpsResponse, err
4948
}
50-
space := regexp.MustCompile(`\s+`)
51-
appStoreName := space.ReplaceAllString(installAppVersionRequest.AppName, "-")
52-
53-
// Checking this is the first time chart has been pushed , if yes requirements.yaml has been already pushed with chart as there was sync-delay with github api.
54-
// step-2 commit dependencies and values in git
55-
if !installAppVersionRequest.IsNewGitOpsRepo {
56-
githash, err = impl.gitOperationService.CommitRequirementsAndValues(appStoreName, chartGitAttribute.RepoUrl, manifestResponse.RequirementsConfig, manifestResponse.ValuesConfig)
57-
if err != nil {
58-
impl.Logger.Errorw("error in committing config to git", "err", err)
59-
return appStoreGitOpsResponse, err
60-
}
61-
}
6249
appStoreGitOpsResponse.ChartGitAttribute = chartGitAttribute
6350
appStoreGitOpsResponse.GitHash = githash
6451
return appStoreGitOpsResponse, nil
@@ -73,6 +60,8 @@ func (impl *FullModeDeploymentServiceImpl) GenerateManifest(installAppVersionReq
7360
impl.Logger.Errorw("Error in building chart while generating manifest", "err", err)
7461
return manifestResponse, err
7562
}
63+
// valuesConfig and dependencyConfig's ChartConfig object contains ChartRepoName which is extracted from gitOpsRepoUrl
64+
// that resides in the db and not from the current orchestrator cm prefix and appName.
7665
valuesConfig, dependencyConfig, err := impl.getValuesAndRequirementForGitConfig(installAppVersionRequest)
7766
if err != nil {
7867
impl.Logger.Errorw("error in fetching values and requirements.yaml config while generating manifest", "err", err)
@@ -127,14 +116,18 @@ func (impl *FullModeDeploymentServiceImpl) UpdateAppGitOpsOperations(manifest *b
127116
noTargetFoundForRequirements, _ := impl.parseGitRepoErrorResponse(requirementsCommitErr)
128117
if noTargetFoundForRequirements || noTargetFoundForValues {
129118
//create repo again and try again - auto fix
130-
*monoRepoMigrationRequired = true // since repo is created again, will use this flag to check if ACD patch operation required
131-
installAppVersionRequest.GitOpsRepoURL = ""
119+
_, _, err := impl.createGitOpsRepo(impl.gitOpsConfigReadService.GetGitOpsRepoNameFromUrl(installAppVersionRequest.GitOpsRepoURL), installAppVersionRequest.UserId)
120+
if err != nil {
121+
impl.Logger.Errorw("error in creating gitops repo for valuesCommitErr or requirementsCommitErr", "gitRepoUrl", installAppVersionRequest.GitOpsRepoURL)
122+
return nil, err
123+
}
132124
return impl.GitOpsOperations(manifest, installAppVersionRequest)
133125
}
134126
impl.Logger.Errorw("error in performing GitOps for upgrade deployment", "ValuesCommitErr", valuesCommitErr, "RequirementsCommitErr", requirementsCommitErr)
135127
return nil, fmt.Errorf("error in committing values and requirements to git repository")
136128
}
137129
gitOpsResponse.GitHash = gitHash
130+
gitOpsResponse.ChartGitAttribute = &commonBean.ChartGitAttribute{RepoUrl: installAppVersionRequest.GitOpsRepoURL, ChartLocation: installAppVersionRequest.ACDAppName}
138131
return gitOpsResponse, nil
139132
}
140133

@@ -169,6 +162,7 @@ func (impl *FullModeDeploymentServiceImpl) parseGitRepoErrorResponse(err error)
169162

170163
// createGitOpsRepoAndPushChart is a wrapper for creating GitOps repo and pushing chart to created repo
171164
func (impl *FullModeDeploymentServiceImpl) createGitOpsRepoAndPushChart(installAppVersionRequest *appStoreBean.InstallAppVersionDTO, builtChartPath string, requirementsConfig *git.ChartConfig, valuesConfig *git.ChartConfig) (*commonBean.ChartGitAttribute, string, error) {
165+
// in case of monorepo migration installAppVersionRequest.GitOpsRepoURL is ""
172166
if len(installAppVersionRequest.GitOpsRepoURL) == 0 {
173167
gitOpsConfigStatus, err := impl.gitOpsConfigReadService.GetGitOpsConfigActive()
174168
if err != nil {
@@ -191,24 +185,7 @@ func (impl *FullModeDeploymentServiceImpl) createGitOpsRepoAndPushChart(installA
191185
installAppVersionRequest.GitOpsRepoURL = gitopsRepoURL
192186
installAppVersionRequest.IsCustomRepository = false
193187
installAppVersionRequest.IsNewGitOpsRepo = isNew
194-
dbConnection := impl.installedAppRepository.GetConnection()
195-
tx, err := dbConnection.Begin()
196-
if err != nil {
197-
return nil, "", err
198-
}
199-
// Rollback tx on error.
200-
defer tx.Rollback()
201-
InstalledApp.UpdateGitOpsRepository(gitopsRepoURL, false)
202-
_, err = impl.installedAppRepository.UpdateInstalledApp(InstalledApp, tx)
203-
if err != nil {
204-
impl.Logger.Errorw("error while fetching from db", "error", err)
205-
return nil, "", err
206-
}
207-
err = tx.Commit()
208-
if err != nil {
209-
impl.Logger.Errorw("error while commit db transaction to db", "error", err)
210-
return nil, "", err
211-
}
188+
212189
}
213190
pushChartToGitRequest := adapter.ParseChartGitPushRequest(installAppVersionRequest, builtChartPath)
214191
chartGitAttribute, commitHash, err := impl.gitOperationService.PushChartToGitOpsRepoForHelmApp(pushChartToGitRequest, requirementsConfig, valuesConfig)

pkg/deployment/gitOps/git/GitOperationService.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ type GitOperationService interface {
2525
GitPull(clonedDir string, repoUrl string, appStoreName string) error
2626

2727
CommitValues(chartGitAttr *ChartConfig) (commitHash string, commitTime time.Time, err error)
28-
CommitRequirementsAndValues(appStoreName, repoUrl string, requirementsConfig *ChartConfig,
29-
valuesConfig *ChartConfig) (gitHash string, err error)
3028
CommitAndPushAllChanges(clonedDir, commitMsg, userName, userEmailId string) (commitHash string, err error)
3129
PushChartToGitRepo(gitOpsRepoName, referenceTemplate, version,
3230
tempReferenceTemplateDir string, repoUrl string, userId int32) (err error)
@@ -331,32 +329,6 @@ func (impl *GitOperationServiceImpl) PushChartToGitOpsRepoForHelmApp(PushChartTo
331329
return &commonBean.ChartGitAttribute{RepoUrl: PushChartToGitRequest.RepoURL, ChartLocation: acdAppName}, commit, err
332330
}
333331

334-
func (impl *GitOperationServiceImpl) CommitRequirementsAndValues(appStoreName, repoUrl string, requirementsConfig *ChartConfig, valuesConfig *ChartConfig) (gitHash string, err error) {
335-
clonedDir := GIT_WORKING_DIR + appStoreName
336-
_, _, err = impl.CommitValues(requirementsConfig)
337-
if err != nil {
338-
impl.logger.Errorw("error in committing dependency config to git", "err", err)
339-
return gitHash, err
340-
}
341-
err = impl.GitPull(clonedDir, repoUrl, appStoreName)
342-
if err != nil {
343-
impl.logger.Errorw("error in git pull", "err", err)
344-
return gitHash, err
345-
}
346-
347-
gitHash, _, err = impl.CommitValues(valuesConfig)
348-
if err != nil {
349-
impl.logger.Errorw("error in committing values config to git", "err", err)
350-
return gitHash, err
351-
}
352-
err = impl.GitPull(clonedDir, repoUrl, appStoreName)
353-
if err != nil {
354-
impl.logger.Errorw("error in git pull", "err", err)
355-
return gitHash, err
356-
}
357-
return gitHash, nil
358-
}
359-
360332
func (impl *GitOperationServiceImpl) GetClonedDir(chartDir, repoUrl string) (string, error) {
361333
clonedDir := impl.gitFactory.GitOpsHelper.GetCloneDirectory(chartDir)
362334
if _, err := os.Stat(clonedDir); os.IsNotExist(err) {

0 commit comments

Comments
 (0)