Skip to content

fix: fix for gitops repo url configured in argocd and stored in db are out of sync, then 3 commits are being done #4912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 19, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,9 @@ func (impl *AppStoreDeploymentServiceImpl) UpdateInstalledApp(ctx context.Contex
gitOpsResponse := &bean2.AppStoreGitOpsResponse{}

if installedAppDeploymentAction.PerformGitOps {
// manifest contains ChartRepoName where the valuesConfig and requirementConfig files will get committed
// and that gitOpsRepoUrl is extracted from db inside GenerateManifest func and not from the current
// orchestrator cm prefix and appName.
manifest, err := impl.fullModeDeploymentService.GenerateManifest(upgradeAppRequest)
if err != nil {
impl.logger.Errorw("error in generating manifest for helm apps", "err", err)
Expand Down Expand Up @@ -721,6 +724,10 @@ func (impl *AppStoreDeploymentServiceImpl) UpdateInstalledApp(ctx context.Contex
}
installedApp.UpdateStatus(appStoreBean.DEPLOY_SUCCESS)
installedApp.UpdateAuditLog(upgradeAppRequest.UserId)
if monoRepoMigrationRequired {
//if monorepo case is true then repoUrl is changed then also update repo url in database
installedApp.UpdateGitOpsRepository(gitOpsResponse.ChartGitAttribute.RepoUrl, installedApp.IsCustomRepository)
}
installedApp, err = impl.installedAppRepository.UpdateInstalledApp(installedApp, tx)
if err != nil {
impl.logger.Errorw("error in updating installed app", "err", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,30 @@ func (impl *FullModeDeploymentServiceImpl) CheckIfArgoAppExists(acdAppName strin
return isFound, nil
}

func (impl *FullModeDeploymentServiceImpl) UpdateAndSyncACDApps(installAppVersionRequest *appStoreBean.InstallAppVersionDTO, ChartGitAttribute *commonBean.ChartGitAttribute, isMonoRepoMigrationRequired bool, ctx context.Context, tx *pg.Tx) error {
if isMonoRepoMigrationRequired {
// update repo details on ArgoCD as repo is changed
err := impl.UpgradeDeployment(installAppVersionRequest, ChartGitAttribute, 0, ctx)
if err != nil {
return err
}
func isArgoCdGitOpsRepoUrlOutOfSync(argoApplication *v1alpha1.Application, gitOpsRepoURLInDb string) bool {
if argoApplication != nil && argoApplication.Spec.Source != nil {
return argoApplication.Spec.Source.RepoURL != gitOpsRepoURLInDb
}
return false
}

func (impl *FullModeDeploymentServiceImpl) UpdateAndSyncACDApps(installAppVersionRequest *appStoreBean.InstallAppVersionDTO, ChartGitAttribute *commonBean.ChartGitAttribute, isMonoRepoMigrationRequired bool, ctx context.Context, tx *pg.Tx) error {
acdAppName := installAppVersionRequest.ACDAppName
argoApplication, err := impl.acdClient.Get(ctx, &application.ApplicationQuery{Name: &acdAppName})
if err != nil {
impl.Logger.Errorw("Service err:UpdateAndSyncACDApps - error in acd app by name", "acdAppName", acdAppName, "err", err)
return err
}
//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,
//then sync argo with git-ops repo url from db because we have already pushed changes to that repo
isArgoRepoUrlOutOfSync := isArgoCdGitOpsRepoUrlOutOfSync(argoApplication, installAppVersionRequest.GitOpsRepoURL)
if isMonoRepoMigrationRequired || isArgoRepoUrlOutOfSync {
// update repo details on ArgoCD as repo is changed
err := impl.UpgradeDeployment(installAppVersionRequest, ChartGitAttribute, 0, ctx)
if err != nil {
return err
}
}

err = impl.argoClientWrapperService.UpdateArgoCDSyncModeIfNeeded(ctx, argoApplication)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

//"github.com/xanzy/go-gitlab"
"net/http"
"regexp"
)

type InstalledAppGitOpsService interface {
Expand Down Expand Up @@ -47,18 +46,6 @@ func (impl *FullModeDeploymentServiceImpl) GitOpsOperations(manifestResponse *be
impl.Logger.Errorw("Error in pushing chart to git", "err", err)
return appStoreGitOpsResponse, err
}
space := regexp.MustCompile(`\s+`)
appStoreName := space.ReplaceAllString(installAppVersionRequest.AppName, "-")

// 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.
// step-2 commit dependencies and values in git
if !installAppVersionRequest.IsNewGitOpsRepo {
githash, err = impl.gitOperationService.CommitRequirementsAndValues(appStoreName, chartGitAttribute.RepoUrl, manifestResponse.RequirementsConfig, manifestResponse.ValuesConfig)
if err != nil {
impl.Logger.Errorw("error in committing config to git", "err", err)
return appStoreGitOpsResponse, err
}
}
appStoreGitOpsResponse.ChartGitAttribute = chartGitAttribute
appStoreGitOpsResponse.GitHash = githash
return appStoreGitOpsResponse, nil
Expand All @@ -73,6 +60,8 @@ func (impl *FullModeDeploymentServiceImpl) GenerateManifest(installAppVersionReq
impl.Logger.Errorw("Error in building chart while generating manifest", "err", err)
return manifestResponse, err
}
// valuesConfig and dependencyConfig's ChartConfig object contains ChartRepoName which is extracted from gitOpsRepoUrl
// that resides in the db and not from the current orchestrator cm prefix and appName.
valuesConfig, dependencyConfig, err := impl.getValuesAndRequirementForGitConfig(installAppVersionRequest)
if err != nil {
impl.Logger.Errorw("error in fetching values and requirements.yaml config while generating manifest", "err", err)
Expand Down Expand Up @@ -127,14 +116,18 @@ func (impl *FullModeDeploymentServiceImpl) UpdateAppGitOpsOperations(manifest *b
noTargetFoundForRequirements, _ := impl.parseGitRepoErrorResponse(requirementsCommitErr)
if noTargetFoundForRequirements || noTargetFoundForValues {
//create repo again and try again - auto fix
*monoRepoMigrationRequired = true // since repo is created again, will use this flag to check if ACD patch operation required
installAppVersionRequest.GitOpsRepoURL = ""
_, _, err := impl.createGitOpsRepo(impl.gitOpsConfigReadService.GetGitOpsRepoNameFromUrl(installAppVersionRequest.GitOpsRepoURL), installAppVersionRequest.UserId)
if err != nil {
impl.Logger.Errorw("error in creating gitops repo for valuesCommitErr or requirementsCommitErr", "gitRepoUrl", installAppVersionRequest.GitOpsRepoURL)
return nil, err
}
return impl.GitOpsOperations(manifest, installAppVersionRequest)
}
impl.Logger.Errorw("error in performing GitOps for upgrade deployment", "ValuesCommitErr", valuesCommitErr, "RequirementsCommitErr", requirementsCommitErr)
return nil, fmt.Errorf("error in committing values and requirements to git repository")
}
gitOpsResponse.GitHash = gitHash
gitOpsResponse.ChartGitAttribute = &commonBean.ChartGitAttribute{RepoUrl: installAppVersionRequest.GitOpsRepoURL, ChartLocation: installAppVersionRequest.ACDAppName}
return gitOpsResponse, nil
}

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

// createGitOpsRepoAndPushChart is a wrapper for creating GitOps repo and pushing chart to created repo
func (impl *FullModeDeploymentServiceImpl) createGitOpsRepoAndPushChart(installAppVersionRequest *appStoreBean.InstallAppVersionDTO, builtChartPath string, requirementsConfig *git.ChartConfig, valuesConfig *git.ChartConfig) (*commonBean.ChartGitAttribute, string, error) {
// in case of monorepo migration installAppVersionRequest.GitOpsRepoURL is ""
if len(installAppVersionRequest.GitOpsRepoURL) == 0 {
gitOpsConfigStatus, err := impl.gitOpsConfigReadService.GetGitOpsConfigActive()
if err != nil {
Expand All @@ -191,24 +185,7 @@ func (impl *FullModeDeploymentServiceImpl) createGitOpsRepoAndPushChart(installA
installAppVersionRequest.GitOpsRepoURL = gitopsRepoURL
installAppVersionRequest.IsCustomRepository = false
installAppVersionRequest.IsNewGitOpsRepo = isNew
dbConnection := impl.installedAppRepository.GetConnection()
tx, err := dbConnection.Begin()
if err != nil {
return nil, "", err
}
// Rollback tx on error.
defer tx.Rollback()
InstalledApp.UpdateGitOpsRepository(gitopsRepoURL, false)
_, err = impl.installedAppRepository.UpdateInstalledApp(InstalledApp, tx)
if err != nil {
impl.Logger.Errorw("error while fetching from db", "error", err)
return nil, "", err
}
err = tx.Commit()
if err != nil {
impl.Logger.Errorw("error while commit db transaction to db", "error", err)
return nil, "", err
}

}
pushChartToGitRequest := adapter.ParseChartGitPushRequest(installAppVersionRequest, builtChartPath)
chartGitAttribute, commitHash, err := impl.gitOperationService.PushChartToGitOpsRepoForHelmApp(pushChartToGitRequest, requirementsConfig, valuesConfig)
Expand Down
28 changes: 0 additions & 28 deletions pkg/deployment/gitOps/git/GitOperationService.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ type GitOperationService interface {
GitPull(clonedDir string, repoUrl string, appStoreName string) error

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

func (impl *GitOperationServiceImpl) CommitRequirementsAndValues(appStoreName, repoUrl string, requirementsConfig *ChartConfig, valuesConfig *ChartConfig) (gitHash string, err error) {
clonedDir := GIT_WORKING_DIR + appStoreName
_, _, err = impl.CommitValues(requirementsConfig)
if err != nil {
impl.logger.Errorw("error in committing dependency config to git", "err", err)
return gitHash, err
}
err = impl.GitPull(clonedDir, repoUrl, appStoreName)
if err != nil {
impl.logger.Errorw("error in git pull", "err", err)
return gitHash, err
}

gitHash, _, err = impl.CommitValues(valuesConfig)
if err != nil {
impl.logger.Errorw("error in committing values config to git", "err", err)
return gitHash, err
}
err = impl.GitPull(clonedDir, repoUrl, appStoreName)
if err != nil {
impl.logger.Errorw("error in git pull", "err", err)
return gitHash, err
}
return gitHash, nil
}

func (impl *GitOperationServiceImpl) GetClonedDir(chartDir, repoUrl string) (string, error) {
clonedDir := impl.gitFactory.GitOpsHelper.GetCloneDirectory(chartDir)
if _, err := os.Stat(clonedDir); os.IsNotExist(err) {
Expand Down
Loading