Skip to content
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

feat: migrate external argo cd application #6303

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/external-app/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import (
"github.com/devtron-labs/devtron/internal/sql/repository"
app2 "github.com/devtron-labs/devtron/internal/sql/repository/app"
"github.com/devtron-labs/devtron/internal/sql/repository/appStatus"
"github.com/devtron-labs/devtron/internal/sql/repository/chartConfig"
"github.com/devtron-labs/devtron/internal/sql/repository/deploymentConfig"
dockerRegistryRepository "github.com/devtron-labs/devtron/internal/sql/repository/dockerRegistry"
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
Expand All @@ -76,6 +77,7 @@ import (
delete2 "github.com/devtron-labs/devtron/pkg/delete"
"github.com/devtron-labs/devtron/pkg/deployment/common"
"github.com/devtron-labs/devtron/pkg/deployment/gitOps"
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate/read"
"github.com/devtron-labs/devtron/pkg/deployment/providerConfig"
"github.com/devtron-labs/devtron/pkg/kubernetesResourceAuditLogs"
repository2 "github.com/devtron-labs/devtron/pkg/kubernetesResourceAuditLogs/repository"
Expand Down Expand Up @@ -269,6 +271,12 @@ func InitializeApp() (*App, error) {

dbMigration.NewDbMigrationServiceImpl,
wire.Bind(new(dbMigration.DbMigration), new(*dbMigration.DbMigrationServiceImpl)),

read.NewEnvConfigOverrideReadServiceImpl,
wire.Bind(new(read.EnvConfigOverrideService), new(*read.EnvConfigOverrideReadServiceImpl)),

chartConfig.NewEnvConfigOverrideRepository,
wire.Bind(new(chartConfig.EnvConfigOverrideRepository), new(*chartConfig.EnvConfigOverrideRepositoryImpl)),
)
return &App{}, nil
}
20 changes: 12 additions & 8 deletions cmd/external-app/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion internal/sql/repository/AppListingRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/devtron-labs/devtron/internal/sql/repository/deploymentConfig"
"github.com/devtron-labs/devtron/internal/util"
repository2 "github.com/devtron-labs/devtron/pkg/cluster/environment/repository"
"github.com/devtron-labs/devtron/pkg/deployment/common/adapter"
"go.opentelemetry.io/otel"
"strings"
"time"
Expand Down Expand Up @@ -624,7 +625,22 @@ func (impl AppListingRepositoryImpl) FetchAppStageStatus(appId int, appType int)
if model != nil && model.Id > 0 && model.AllowCustomRepository {
isCustomGitopsRepoUrl = true
}
if (gitOps.IsGitOpsRepoNotConfigured(stages.ChartGitRepoUrl) && gitOps.IsGitOpsRepoNotConfigured(stages.DeploymentConfigRepoURL)) && stages.CiPipelineId == 0 {

deploymentConfigDB, err := impl.deploymentConfigRepository.GetByAppIdAndEnvId(appId, 0)
if err != nil && err != pg.ErrNoRows {
impl.Logger.Errorw("error while getting deploymentConfig", "appId", appId, "err", err)
return appStageStatus, err
}

dc, err := adapter.ConvertDeploymentConfigDbObjToDTO(deploymentConfigDB)
if err != nil {
impl.Logger.Errorw("error while converting DeploymentConfigDbObjToDTO", "err", err)
return nil, err
}

if (gitOps.IsGitOpsRepoNotConfigured(stages.ChartGitRepoUrl) &&
gitOps.IsGitOpsRepoNotConfigured(stages.DeploymentConfigRepoURL) &&
(dc != nil && gitOps.IsGitOpsRepoNotConfigured(dc.GetRepoURL()))) && stages.CiPipelineId == 0 {
stages.ChartGitRepoUrl = ""
stages.DeploymentConfigRepoURL = ""
}
Expand Down
12 changes: 11 additions & 1 deletion internal/sql/repository/deploymentConfig/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type ConfigType string

const (
Custom ConfigType = "custom"
SystemGenerated = "system_generated"
SystemGenerated ConfigType = "system_generated"
)

type DeploymentConfig struct {
Expand All @@ -46,6 +46,7 @@ type DeploymentConfig struct {
RepoUrl string `sql:"repo_url"`
RepoName string `sql:"repo_name"`
ReleaseMode string `sql:"release_mode"`
ReleaseConfig []byte `sql:"release_config"`
Active bool `sql:"active,notnull"`
sql.AuditLog
}
Expand All @@ -63,6 +64,7 @@ type Repository interface {
GetByAppIdAndEnvIdEvenIfInactive(appId, envId int) (*DeploymentConfig, error)
UpdateRepoUrlByAppIdAndEnvId(repoUrl string, appId, envId int) error
GetConfigByAppIds(appIds []int) ([]*DeploymentConfig, error)
GetAllConfigsWithCustomGitOpsURL() ([]*DeploymentConfig, error)
}

type RepositoryImpl struct {
Expand Down Expand Up @@ -203,3 +205,11 @@ func (impl *RepositoryImpl) GetConfigByAppIds(appIds []int) ([]*DeploymentConfig
Select()
return results, err
}

func (impl *RepositoryImpl) GetAllConfigsWithCustomGitOpsURL() ([]*DeploymentConfig, error) {
result := make([]*DeploymentConfig, 0)
err := impl.dbConnection.Model(&result).
Where("active = ? and config_type = ? ", true, Custom).
Select()
return result, err
}
2 changes: 1 addition & 1 deletion pkg/appStore/adapter/Adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func UpdateInstallAppDetails(request *appStoreBean.InstallAppVersionDTO, install
request.Status = installedApp.Status
request.DeploymentAppType = config.DeploymentAppType
if util.IsAcdApp(config.DeploymentAppType) {
request.GitOpsRepoURL = config.RepoURL
request.GitOpsRepoURL = config.GetRepoURL()
}
}

Expand Down
33 changes: 29 additions & 4 deletions pkg/appStore/bean/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ package appStoreBean
import (
"encoding/json"
"github.com/argoproj/gitops-engine/pkg/health"
"github.com/devtron-labs/common-lib/utils/k8s/commonBean"
apiBean "github.com/devtron-labs/devtron/api/bean/gitOps"
openapi "github.com/devtron-labs/devtron/api/helm-app/openapiClient"
bean3 "github.com/devtron-labs/devtron/api/helm-app/service/bean"
"github.com/devtron-labs/devtron/client/argocdServer"
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig/bean/workflow/cdWorkflow"
util2 "github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/pkg/cluster/environment/bean"
bean2 "github.com/devtron-labs/devtron/pkg/deployment/common/bean"
"github.com/devtron-labs/devtron/util"
"github.com/devtron-labs/devtron/util/gitUtil"
"slices"
"time"
)
Expand Down Expand Up @@ -218,9 +220,32 @@ func (chart *InstallAppVersionDTO) GetDeploymentConfig() *bean2.DeploymentConfig
EnvironmentId: chart.EnvironmentId,
ConfigType: configType,
DeploymentAppType: chart.DeploymentAppType,
RepoURL: chart.GitOpsRepoURL,
RepoName: gitUtil.GetGitRepoNameFromGitRepoUrl(chart.GitOpsRepoURL),
Active: true,
ReleaseMode: util2.PIPELINE_RELEASE_MODE_CREATE,
ReleaseConfiguration: &bean2.ReleaseConfiguration{
Version: bean2.Version,
ArgoCDSpec: bean2.ArgoCDSpec{
Metadata: bean2.ApplicationMetadata{
ClusterId: DEFAULT_CLUSTER_ID,
Namespace: argocdServer.DevtronInstalationNs,
},
Spec: bean2.ApplicationSpec{
Destination: &bean2.Destination{
Namespace: chart.Namespace,
Server: commonBean.DefaultClusterUrl,
},
Source: &bean2.ApplicationSource{
RepoURL: chart.GitOpsRepoURL,
Path: util.BuildDeployedAppName(chart.AppName, chart.EnvironmentName),
Helm: &bean2.ApplicationSourceHelm{
ValueFiles: []string{"values.yaml"},
},
TargetRevision: "master",
},
SyncPolicy: nil,
},
},
},
Active: true,
}
}

Expand Down
68 changes: 6 additions & 62 deletions pkg/appStore/installedApp/service/AppStoreDeploymentService.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,14 +592,14 @@ func (impl *AppStoreDeploymentServiceImpl) updateInstalledApp(ctx context.Contex
installedAppDeploymentAction := adapter.NewInstalledAppDeploymentAction(deploymentConfig.DeploymentAppType)
// migrate installedApp.GitOpsRepoName to installedApp.GitOpsRepoUrl
if util.IsAcdApp(deploymentConfig.DeploymentAppType) &&
len(deploymentConfig.RepoURL) == 0 {
len(deploymentConfig.GetRepoURL()) == 0 {
gitRepoUrl, err := impl.fullModeDeploymentService.GetAcdAppGitOpsRepoURL(installedApp.App.AppName, installedApp.Environment.Name)
if err != nil {
impl.logger.Errorw("error in GitOps repository url migration", "err", err)
return nil, err
}
deploymentConfig.RepoURL = gitRepoUrl
installedApp.GitOpsRepoUrl = gitRepoUrl
deploymentConfig.SetRepoURL(gitRepoUrl)
//installedApp.GitOpsRepoUrl = gitRepoUrl
installedApp.GitOpsRepoName = impl.gitOpsConfigReadService.GetGitOpsRepoNameFromUrl(gitRepoUrl)
}
// migration ends
Expand Down Expand Up @@ -754,7 +754,7 @@ func (impl *AppStoreDeploymentServiceImpl) updateInstalledApp(ctx context.Contex
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)
deploymentConfig.RepoURL = gitOpsResponse.ChartGitAttribute.RepoUrl
deploymentConfig.SetRepoURL(gitOpsResponse.ChartGitAttribute.RepoUrl)
}
installedApp, err = impl.installedAppRepository.UpdateInstalledApp(installedApp, tx)
if err != nil {
Expand Down Expand Up @@ -998,11 +998,11 @@ func (impl *AppStoreDeploymentServiceImpl) linkHelmApplicationToChartStore(insta
// checkIfMonoRepoMigrationRequired checks if gitOps repo name is changed
func (impl *AppStoreDeploymentServiceImpl) checkIfMonoRepoMigrationRequired(installedApp *repository.InstalledApps, deploymentConfig *bean5.DeploymentConfig) bool {
monoRepoMigrationRequired := false
if !util.IsAcdApp(deploymentConfig.DeploymentAppType) || gitOps.IsGitOpsRepoNotConfigured(deploymentConfig.RepoURL) || deploymentConfig.ConfigType == bean5.CUSTOM.String() {
if !util.IsAcdApp(deploymentConfig.DeploymentAppType) || gitOps.IsGitOpsRepoNotConfigured(deploymentConfig.GetRepoURL()) || deploymentConfig.ConfigType == bean5.CUSTOM.String() {
return false
}
var err error
gitOpsRepoName := impl.gitOpsConfigReadService.GetGitOpsRepoNameFromUrl(deploymentConfig.RepoURL)
gitOpsRepoName := impl.gitOpsConfigReadService.GetGitOpsRepoNameFromUrl(deploymentConfig.GetRepoURL())
if len(gitOpsRepoName) == 0 {
gitOpsRepoName, err = impl.fullModeDeploymentService.GetAcdAppGitOpsRepoName(installedApp.App.AppName, installedApp.Environment.Name)
if err != nil || gitOpsRepoName == "" {
Expand All @@ -1019,62 +1019,6 @@ func (impl *AppStoreDeploymentServiceImpl) checkIfMonoRepoMigrationRequired(inst
return monoRepoMigrationRequired
}

// handleGitOpsRepoUrlMigration will migrate git_ops_repo_name to git_ops_repo_url
func (impl *AppStoreDeploymentServiceImpl) handleGitOpsRepoUrlMigration(tx *pg.Tx, installedApp *repository.InstalledApps, deploymentConfig *bean5.DeploymentConfig, userId int32) error {
var (
localTx *pg.Tx
err error
)

if tx == nil {
dbConnection := impl.installedAppRepository.GetConnection()
localTx, err = dbConnection.Begin()
if err != nil {
return err
}
// Rollback tx on error.
defer localTx.Rollback()
}

gitRepoUrl, err := impl.fullModeDeploymentService.GetGitRepoUrl(installedApp.GitOpsRepoName)
if err != nil {
impl.logger.Errorw("error in GitOps repository url migration", "err", err)
return err
}
installedApp.GitOpsRepoUrl = gitRepoUrl
installedApp.UpdatedOn = time.Now()
installedApp.UpdatedBy = userId

var dbTx *pg.Tx
if localTx != nil {
dbTx = localTx
} else {
dbTx = tx
}

_, err = impl.installedAppRepository.UpdateInstalledApp(installedApp, dbTx)
if err != nil {
impl.logger.Errorw("error in updating installed app model", "err", err)
return err
}

deploymentConfig.RepoURL = gitRepoUrl
deploymentConfig, err = impl.deploymentConfigService.CreateOrUpdateConfig(dbTx, deploymentConfig, userId)
if err != nil {
impl.logger.Errorw("error in updating deployment config", "err", err)
return err
}

if localTx != nil {
err = localTx.Commit()
if err != nil {
impl.logger.Errorw("error while committing transaction to db", "error", err)
return err
}
}
return err
}

// getAppNameForInstalledApp will fetch and returns AppName from app table
func (impl *AppStoreDeploymentServiceImpl) getAppNameForInstalledApp(installedAppId int) string {
installedApp, err := impl.installedAppRepository.GetInstalledApp(installedAppId)
Expand Down
Loading
Loading