Skip to content

Commit d6461fb

Browse files
FIx: backward compatibility added for gitops repo for charts (#1327)
* bakcward compatibility added for gitops repo for charts * added todo * acd error check fixed * acd error check fixed
1 parent f90799d commit d6461fb

File tree

9 files changed

+62
-24
lines changed

9 files changed

+62
-24
lines changed

cmd/external-app/wire.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
delete2 "github.com/devtron-labs/devtron/pkg/delete"
2727
"github.com/devtron-labs/devtron/pkg/sql"
2828
util2 "github.com/devtron-labs/devtron/pkg/util"
29+
util3 "github.com/devtron-labs/devtron/util"
2930
"github.com/devtron-labs/devtron/util/k8s"
3031
"github.com/devtron-labs/devtron/util/rbac"
3132
"github.com/google/wire"
@@ -49,7 +50,7 @@ func InitializeApp() (*App, error) {
4950

5051
NewApp,
5152
NewMuxRouter,
52-
53+
util3.GetGlobalEnvVariables,
5354
util.NewHttpClient,
5455
util.NewSugardLogger,
5556
util.NewK8sUtil,

internal/util/ChartService.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"path/filepath"
2828
"regexp"
2929
"strconv"
30+
"strings"
3031
"time"
3132

3233
"github.com/ghodss/yaml"
@@ -45,6 +46,7 @@ type ChartTemplateService interface {
4546
CreateChartProxy(chartMetaData *chart.Metadata, refChartLocation string, templateName string, version string, envName string, appName string) (string, *ChartGitAttribute, error)
4647
GitPull(clonedDir string, repoUrl string, appStoreName string) error
4748
GetGitOpsRepoName(appName string) string
49+
GetGitOpsRepoNameFromUrl(gitRepoUrl string) string
4850
}
4951
type ChartTemplateServiceImpl struct {
5052
randSource rand.Source
@@ -456,3 +458,9 @@ func (impl ChartTemplateServiceImpl) GetGitOpsRepoName(appName string) string {
456458
}
457459
return repoName
458460
}
461+
462+
func (impl ChartTemplateServiceImpl) GetGitOpsRepoNameFromUrl(gitRepoUrl string) string {
463+
gitRepoUrl = gitRepoUrl[strings.LastIndex(gitRepoUrl, "/")+1:]
464+
gitRepoUrl = strings.ReplaceAll(gitRepoUrl, ".git", "")
465+
return gitRepoUrl
466+
}

pkg/appStore/AppStoreDeploymentService.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"github.com/devtron-labs/devtron/pkg/user"
3636
util2 "github.com/devtron-labs/devtron/pkg/util"
3737
"github.com/ktrysmt/go-bitbucket"
38-
3938
/* #nosec */
4039
"crypto/sha1"
4140
"encoding/json"
@@ -179,9 +178,21 @@ func (impl InstalledAppServiceImpl) UpdateInstalledApp(ctx context.Context, inst
179178
impl.logger.Errorw("fetching error", "err", err)
180179
return nil, err
181180
}
182-
impl.logger.Debug(team.Name)
181+
impl.logger.Info(team)
183182
argocdAppName := installedApp.App.AppName + "-" + environment.Name
184-
183+
gitOpsRepoName := installedApp.GitOpsRepoName
184+
if len(gitOpsRepoName) == 0 {
185+
application, err := impl.acdClient.Get(ctx, &application.ApplicationQuery{Name: &argocdAppName})
186+
if err != nil {
187+
impl.logger.Errorw("no argo app exists", "app", argocdAppName)
188+
return nil, err
189+
}
190+
if application != nil {
191+
gitOpsRepoUrl := application.Spec.Source.RepoURL
192+
gitOpsRepoName = impl.chartTemplateService.GetGitOpsRepoNameFromUrl(gitOpsRepoUrl)
193+
}
194+
}
195+
installAppVersionRequest.GitOpsRepoName = gitOpsRepoName
185196
if installAppVersionRequest.Id == 0 {
186197
// upgrade chart to other repo
187198
_, _, err := impl.upgradeInstalledApp(ctx, installAppVersionRequest, installedApp, tx)
@@ -297,13 +308,12 @@ func (impl InstalledAppServiceImpl) updateRequirementDependencies(environment *r
297308
if err != nil {
298309
return err
299310
}
300-
gitOpsRepoName := impl.chartTemplateService.GetGitOpsRepoName(installedAppVersion.AppStoreApplicationVersion.AppStore.Name)
301311
requirmentYamlConfig := &util.ChartConfig{
302312
FileName: appStoreBean.REQUIREMENTS_YAML_FILE,
303313
FileContent: string(requirementDependenciesByte),
304314
ChartName: installedAppVersion.AppStoreApplicationVersion.AppStore.Name,
305315
ChartLocation: argocdAppName,
306-
ChartRepoName: gitOpsRepoName,
316+
ChartRepoName: installAppVersionRequest.GitOpsRepoName,
307317
ReleaseMessage: fmt.Sprintf("release-%d-env-%d ", appStoreAppVersion.Id, environment.Id),
308318
}
309319
gitOpsConfigBitbucket, err := impl.gitOpsRepository.GetGitOpsConfigByProvider(util.BITBUCKET_PROVIDER)
@@ -345,13 +355,12 @@ func (impl InstalledAppServiceImpl) updateValuesYaml(environment *repository5.En
345355
impl.logger.Errorw("error in marshaling", "err", err)
346356
return err
347357
}
348-
gitOpsRepoName := impl.chartTemplateService.GetGitOpsRepoName(installedAppVersion.AppStoreApplicationVersion.AppStore.Name)
349358
valuesConfig := &util.ChartConfig{
350359
FileName: appStoreBean.VALUES_YAML_FILE,
351360
FileContent: string(valuesByte),
352361
ChartName: installedAppVersion.AppStoreApplicationVersion.AppStore.Name,
353362
ChartLocation: argocdAppName,
354-
ChartRepoName: gitOpsRepoName,
363+
ChartRepoName: installAppVersionRequest.GitOpsRepoName,
355364
ReleaseMessage: fmt.Sprintf("release-%d-env-%d ", installedAppVersion.AppStoreApplicationVersion.Id, environment.Id),
356365
}
357366
gitOpsConfigBitbucket, err := impl.gitOpsRepository.GetGitOpsConfigByProvider(util.BITBUCKET_PROVIDER)

pkg/appStore/bean/bean.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type InstallAppVersionDTO struct {
5050
ClusterId int `json:"clusterId"` // needed for hyperion mode
5151
Namespace string `json:"namespace"` // needed for hyperion mode
5252
AppOfferingMode string `json:"appOfferingMode"`
53+
GitOpsRepoName string `json:"gitOpsRepoName"`
5354
EnvironmentName string `json:"-"`
5455
InstallAppVersionChartDTO *InstallAppVersionChartDTO `json:"-"`
5556
}

pkg/appStore/deployment/AppStoreDeploymentService.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ type AppStoreDeploymentServiceImpl struct {
6161
appStoreDeploymentArgoCdService appStoreDeploymentGitopsTool.AppStoreDeploymentArgoCdService
6262
environmentService cluster.EnvironmentService
6363
clusterService cluster.ClusterService
64+
globalEnvVariables *util2.GlobalEnvVariables
6465
}
6566

6667
func NewAppStoreDeploymentServiceImpl(logger *zap.SugaredLogger, installedAppRepository appStoreRepository.InstalledAppRepository,
6768
appStoreApplicationVersionRepository appStoreDiscoverRepository.AppStoreApplicationVersionRepository, environmentRepository clusterRepository.EnvironmentRepository,
6869
clusterInstalledAppsRepository appStoreRepository.ClusterInstalledAppsRepository, appRepository app.AppRepository,
6970
appStoreDeploymentHelmService appStoreDeploymentTool.AppStoreDeploymentHelmService,
7071
appStoreDeploymentArgoCdService appStoreDeploymentGitopsTool.AppStoreDeploymentArgoCdService, environmentService cluster.EnvironmentService,
71-
clusterService cluster.ClusterService) *AppStoreDeploymentServiceImpl {
72+
clusterService cluster.ClusterService, globalEnvVariables *util2.GlobalEnvVariables) *AppStoreDeploymentServiceImpl {
7273
return &AppStoreDeploymentServiceImpl{
7374
logger: logger,
7475
installedAppRepository: installedAppRepository,
@@ -80,6 +81,7 @@ func NewAppStoreDeploymentServiceImpl(logger *zap.SugaredLogger, installedAppRep
8081
appStoreDeploymentArgoCdService: appStoreDeploymentArgoCdService,
8182
environmentService: environmentService,
8283
clusterService: clusterService,
84+
globalEnvVariables: globalEnvVariables,
8385
}
8486
}
8587

@@ -130,6 +132,9 @@ func (impl AppStoreDeploymentServiceImpl) AppStoreDeployOperationDB(installAppVe
130132
installedAppModel.CreatedOn = time.Now()
131133
installedAppModel.UpdatedOn = time.Now()
132134
installedAppModel.Active = true
135+
if util2.GetDevtronVersion().ServerMode == util2.SERVER_MODE_FULL {
136+
installedAppModel.GitOpsRepoName = impl.GetGitOpsRepoName(appStoreAppVersion.AppStore.Name)
137+
}
133138
installedApp, err := impl.installedAppRepository.CreateInstalledApp(installedAppModel, tx)
134139
if err != nil {
135140
impl.logger.Errorw("error while creating install app", "error", err)
@@ -175,6 +180,17 @@ func (impl AppStoreDeploymentServiceImpl) AppStoreDeployOperationDB(installAppVe
175180
return installAppVersionRequest, nil
176181
}
177182

183+
//TODO - dedupe, move it to one location
184+
func (impl AppStoreDeploymentServiceImpl) GetGitOpsRepoName(appName string) string {
185+
var repoName string
186+
if len(impl.globalEnvVariables.GitOpsRepoPrefix) == 0 {
187+
repoName = appName
188+
} else {
189+
repoName = fmt.Sprintf("%s-%s", impl.globalEnvVariables.GitOpsRepoPrefix, appName)
190+
}
191+
return repoName
192+
}
193+
178194
func (impl AppStoreDeploymentServiceImpl) AppStoreDeployOperationStatusUpdate(installAppId int, status appStoreBean.AppstoreDeploymentStatus) (bool, error) {
179195
dbConnection := impl.installedAppRepository.GetConnection()
180196
tx, err := dbConnection.Begin()
@@ -344,9 +360,9 @@ func (impl AppStoreDeploymentServiceImpl) GetAllInstalledAppsByAppStoreId(w http
344360
var installedAppsEnvResponse []appStoreBean.InstalledAppsResponse
345361
for _, a := range installedApps {
346362
var status string
347-
if util2.GetDevtronVersion().ServerMode == util2.SERVER_MODE_HYPERION || a.AppOfferingMode == util2.SERVER_MODE_HYPERION {
363+
if util2.GetDevtronVersion().ServerMode == util2.SERVER_MODE_HYPERION || a.AppOfferingMode == util2.SERVER_MODE_HYPERION {
348364
status, err = impl.appStoreDeploymentHelmService.GetAppStatus(a, w, r, token)
349-
}else{
365+
} else {
350366
status, err = impl.appStoreDeploymentArgoCdService.GetAppStatus(a, w, r, token)
351367
}
352368
if apiErr, ok := err.(*util.ApiError); ok {
@@ -444,9 +460,9 @@ func (impl AppStoreDeploymentServiceImpl) DeleteInstalledApp(ctx context.Context
444460
}
445461
}
446462

447-
if util2.GetDevtronVersion().ServerMode == util2.SERVER_MODE_HYPERION || app.AppOfferingMode == util2.SERVER_MODE_HYPERION {
463+
if util2.GetDevtronVersion().ServerMode == util2.SERVER_MODE_HYPERION || app.AppOfferingMode == util2.SERVER_MODE_HYPERION {
448464
err = impl.appStoreDeploymentHelmService.DeleteInstalledApp(ctx, app.AppName, environment.Name, installAppVersionRequest, model, tx)
449-
}else{
465+
} else {
450466
err = impl.appStoreDeploymentArgoCdService.DeleteInstalledApp(ctx, app.AppName, environment.Name, installAppVersionRequest, model, tx)
451467
}
452468

pkg/appStore/repository/AppStoreDeploymentRepository.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type InstalledAppRepository interface {
4848
GetInstalledAppVersionByInstalledAppId(id int) ([]*InstalledAppVersions, error)
4949
GetConnection() (dbConnection *pg.DB)
5050
GetInstalledAppVersionByInstalledAppIdMeta(appStoreApplicationId int) ([]*InstalledAppVersions, error)
51-
GetClusterComponentByClusterId(clusterId int) ([]*InstalledApps, error) //unused
51+
GetClusterComponentByClusterId(clusterId int) ([]*InstalledApps, error) //unused
5252
GetClusterComponentByClusterIds(clusterIds []int) ([]*InstalledApps, error) //unused
5353
GetInstalledAppVersionByAppIdAndEnvId(appId int, envId int) (*InstalledAppVersions, error)
5454
GetInstalledAppVersionByClusterIds(clusterIds []int) ([]*InstalledAppVersions, error) //unused
@@ -66,14 +66,15 @@ func NewInstalledAppRepositoryImpl(Logger *zap.SugaredLogger, dbConnection *pg.D
6666
}
6767

6868
type InstalledApps struct {
69-
TableName struct{} `sql:"installed_apps" pg:",discard_unknown_columns"`
70-
Id int `sql:"id,pk"`
71-
AppId int `sql:"app_id,notnull"`
72-
EnvironmentId int `sql:"environment_id,notnull"`
73-
Active bool `sql:"active, notnull"`
74-
Status appStoreBean.AppstoreDeploymentStatus `sql:"status"`
75-
App app.App
76-
Environment repository.Environment
69+
TableName struct{} `sql:"installed_apps" pg:",discard_unknown_columns"`
70+
Id int `sql:"id,pk"`
71+
AppId int `sql:"app_id,notnull"`
72+
EnvironmentId int `sql:"environment_id,notnull"`
73+
Active bool `sql:"active, notnull"`
74+
GitOpsRepoName string `sql:"git_ops_repo_name"`
75+
Status appStoreBean.AppstoreDeploymentStatus `sql:"status"`
76+
App app.App
77+
Environment repository.Environment
7778
sql.AuditLog
7879
}
7980

@@ -422,4 +423,4 @@ func (impl InstalledAppRepositoryImpl) GetInstalledApplicationByClusterIdAndName
422423
Where("environment.active = ?", true).
423424
Select()
424425
return model, err
425-
}
426+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE "public"."installed_apps" DROP COLUMN "git_ops_repo_name";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE "public"."installed_apps" ADD COLUMN "git_ops_repo_name" varchar(255);

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.

0 commit comments

Comments
 (0)