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

fix: Packet testing fixes #5916

Merged
merged 8 commits into from
Sep 27, 2024
Merged
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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ build: clean wire
-X 'github.com/devtron-labs/devtron/util.BuildTime=${BUILD_TIME}' \
-X 'github.com/devtron-labs/devtron/util.ServerMode=${SERVER_MODE_FULL}'"

mod:
go mod vendor

wire:
wire

Expand Down Expand Up @@ -54,7 +57,7 @@ docker-build-push: docker-build-image

#############################################################################

build-all: build
build-all: mod build
make --directory ./cmd/external-app build

build-ea:
Expand Down
4 changes: 4 additions & 0 deletions Wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import (
security2 "github.com/devtron-labs/devtron/internal/sql/repository/security"
"github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/pkg/app"
"github.com/devtron-labs/devtron/pkg/app/dbMigration"
"github.com/devtron-labs/devtron/pkg/app/status"
"github.com/devtron-labs/devtron/pkg/appClone"
"github.com/devtron-labs/devtron/pkg/appClone/batch"
Expand Down Expand Up @@ -1005,6 +1006,9 @@ func InitializeApp() (*App, error) {

repocreds.NewServiceClientImpl,
wire.Bind(new(repocreds.ServiceClient), new(*repocreds.ServiceClientImpl)),

dbMigration.NewDbMigrationServiceImpl,
wire.Bind(new(dbMigration.DbMigration), new(*dbMigration.DbMigrationServiceImpl)),
)
return &App{}, nil
}
3 changes: 1 addition & 2 deletions api/appStore/deployment/CommonDeploymentRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ func (handler *CommonDeploymentRestHandlerImpl) getAppOfferingMode(installedAppI
err = &util.ApiError{HttpStatusCode: http.StatusBadRequest, UserMessage: "invalid app id"}
return appOfferingMode, installedAppDto, err
}
uniqueAppName := appIdentifier.GetUniqueAppNameIdentifier()
installedAppDto, err = handler.installedAppService.GetInstalledAppByClusterNamespaceAndName(appIdentifier.ClusterId, appIdentifier.Namespace, uniqueAppName)
installedAppDto, err = handler.installedAppService.GetInstalledAppByClusterNamespaceAndName(appIdentifier)
if err != nil {
err = &util.ApiError{HttpStatusCode: http.StatusBadRequest, UserMessage: "unable to find app in database"}
return appOfferingMode, installedAppDto, err
Expand Down
2 changes: 1 addition & 1 deletion api/helm-app/HelmAppRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (handler *HelmAppRestHandlerImpl) GetApplicationDetail(w http.ResponseWrite
return
}

installedApp, err := handler.installedAppService.GetInstalledAppByClusterNamespaceAndName(appIdentifier.ClusterId, appIdentifier.Namespace, appIdentifier.ReleaseName)
installedApp, err := handler.installedAppService.GetInstalledAppByClusterNamespaceAndName(appIdentifier)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,17 +714,30 @@ func (handler *PipelineConfigRestHandlerImpl) TriggerCiPipeline(w http.ResponseW
return
}
cdPipelineRbacObjects := make([]string, len(cdPipelines))
rbacObjectToCdPipeline := make(map[string]*pipelineConfig.Pipeline)
for i, cdPipeline := range cdPipelines {
envObject := handler.enforcerUtil.GetAppRBACByAppIdAndPipelineId(cdPipeline.AppId, cdPipeline.Id)
cdPipelineRbacObjects[i] = envObject
rbacObjectToCdPipeline[envObject] = cdPipeline
}

hasAnyEnvTriggerAccess := false
envRbacResultMap := handler.enforcer.EnforceInBatch(token, casbin.ResourceEnvironment, casbin.ActionTrigger, cdPipelineRbacObjects)
for _, rbacResultOk := range envRbacResultMap {
if !rbacResultOk {
for rbacObject, rbacResultOk := range envRbacResultMap {
cdPipeline := rbacObjectToCdPipeline[rbacObject]
if cdPipeline.TriggerType == pipelineConfig.TRIGGER_TYPE_AUTOMATIC && !rbacResultOk {
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusForbidden)
return
}
if rbacResultOk {
hasAnyEnvTriggerAccess = true
}
}
if !hasAnyEnvTriggerAccess {
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusForbidden)
return
}

//RBAC ENDS
response := make(map[string]string)
resp, err := handler.ciHandler.HandleCIManual(ciTriggerRequest)
Expand Down
4 changes: 4 additions & 0 deletions cmd/external-app/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import (
security2 "github.com/devtron-labs/devtron/internal/sql/repository/security"
"github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/pkg/app"
"github.com/devtron-labs/devtron/pkg/app/dbMigration"
repository4 "github.com/devtron-labs/devtron/pkg/appStore/chartGroup/repository"
"github.com/devtron-labs/devtron/pkg/appStore/installedApp/service/EAMode"
"github.com/devtron-labs/devtron/pkg/appStore/installedApp/service/FullMode/deployment"
Expand Down Expand Up @@ -253,6 +254,9 @@ func InitializeApp() (*App, error) {

argoRepositoryCreds.NewRepositorySecret,
wire.Bind(new(argoRepositoryCreds.RepositorySecret), new(*argoRepositoryCreds.RepositorySecretImpl)),

dbMigration.NewDbMigrationServiceImpl,
wire.Bind(new(dbMigration.DbMigration), new(*dbMigration.DbMigrationServiceImpl)),
)
return &App{}, nil
}
8 changes: 5 additions & 3 deletions cmd/external-app/wire_gen.go

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

79 changes: 13 additions & 66 deletions internal/sql/repository/app/AppRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type AppRepository interface {
UpdateWithTxn(app *App, tx *pg.Tx) error
SetDescription(id int, description string, userId int32) error
FindActiveByName(appName string) (pipelineGroup *App, err error)
FindAllActiveByName(appName string) ([]*App, error)
FindAppIdByName(appName string) (int, error)

FindJobByDisplayName(appName string) (pipelineGroup *App, err error)
Expand Down Expand Up @@ -133,35 +134,24 @@ func (repo AppRepositoryImpl) SetDescription(id int, description string, userId
}

func (repo AppRepositoryImpl) FindActiveByName(appName string) (*App, error) {
pipelineGroup := &App{}
err := repo.dbConnection.
Model(pipelineGroup).
Where("app_name = ?", appName).
Where("active = ?", true).
Order("id DESC").Limit(1).
Select()
return pipelineGroup, err
}

func (repo AppRepositoryImpl) FindAllActiveByName(appName string) ([]*App, error) {
var apps []*App
err := repo.dbConnection.
Model(&apps).
Where("app_name = ?", appName).
Where("active = ?", true).
Order("id DESC").
Select()
if len(apps) == 1 {
return apps[0], nil
} else if len(apps) > 1 {
isHelmApp := true
for _, app := range apps {
if app.AppType != helper.ChartStoreApp && app.AppType != helper.ExternalChartStoreApp {
isHelmApp = false
break
}
}
if isHelmApp {
err := repo.fixMultipleHelmAppsWithSameName(appName)
if err != nil {
repo.logger.Errorw("error in fixing duplicate helm apps with same name")
return nil, err
}
}
return apps[0], nil
} else {
err = pg.ErrNoRows
}
return nil, err
return apps, err
}

func (repo AppRepositoryImpl) FindAppIdByName(appName string) (int, error) {
Expand Down Expand Up @@ -349,52 +339,9 @@ func (repo AppRepositoryImpl) FindAppAndProjectByAppName(appName string) (*App,
Where("app.app_name = ?", appName).
Where("app.active=?", true).
Select()

if err == pg.ErrMultiRows && (app.AppType == helper.ChartStoreApp || app.AppType == helper.ExternalChartStoreApp) {
// this case can arise in helms apps only

err := repo.fixMultipleHelmAppsWithSameName(appName)
if err != nil {
repo.logger.Errorw("error in fixing duplicate helm apps with same name")
return nil, err
}

err = repo.dbConnection.Model(app).Column("Team").
Where("app.app_name = ?", appName).
Where("app.active=?", true).
Select()
if err != nil {
repo.logger.Errorw("error in fetching apps by name", "appName", appName, "err", err)
return nil, err
}
}
return app, err
}

func (repo AppRepositoryImpl) fixMultipleHelmAppsWithSameName(appName string) error {
// updating installed apps setting app_id = max app_id
installAppUpdateQuery := `update installed_apps set
app_id=(select max(id) as id from app where app_name = ?)
where app_id in (select id from app where app_name= ? )`

_, err := repo.dbConnection.Exec(installAppUpdateQuery, appName, appName)
if err != nil {
repo.logger.Errorw("error in updating maxAppId in installedApps", "appName", appName, "err", err)
return err
}

maxAppIdQuery := repo.dbConnection.Model((*App)(nil)).ColumnExpr("max(id)").
Where("app_name = ? ", appName).
Where("active = ? ", true)

// deleting all apps other than app with max id
_, err = repo.dbConnection.Model((*App)(nil)).
Set("active = ?", false).Set("updated_by = ?", SYSTEM_USER_ID).Set("updated_on = ?", time.Now()).
Where("id not in (?) ", maxAppIdQuery).Update()

return nil
}

func (repo AppRepositoryImpl) FindAllMatchesByAppName(appName string, appType helper.AppType) ([]*App, error) {
var apps []*App
var err error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ type DockerArtifactStore struct {
sql.AuditLog
}

type DockerArtifactStoreExt struct {
*DockerArtifactStore
DeploymentCount int `sql:"deployment_count" json:"deploymentCount"`
}

type ChartDeploymentCount struct {
OCIChartName string `sql:"oci_chart_name" json:"ociChartName"`
DeploymentCount int `sql:"deployment_count" json:"deploymentCount"`
Expand All @@ -94,7 +89,7 @@ type DockerArtifactStoreRepository interface {
FindAllDockerArtifactCount() (int, error)
FindAllChartProviders() ([]DockerArtifactStore, error)
FindOne(storeId string) (*DockerArtifactStore, error)
FindOneWithDeploymentCount(storeId string) (*DockerArtifactStoreExt, error)
FindDeploymentCount(storeId string) (int, error)
FindOneWithChartDeploymentCount(storeId, chartName string) (*ChartDeploymentCount, error)
FindOneInactive(storeId string) (*DockerArtifactStore, error)
Update(artifactStore *DockerArtifactStore, tx *pg.Tx) error
Expand Down Expand Up @@ -205,14 +200,14 @@ func (impl DockerArtifactStoreRepositoryImpl) FindOne(storeId string) (*DockerAr
return &provider, err
}

func (impl DockerArtifactStoreRepositoryImpl) FindOneWithDeploymentCount(storeId string) (*DockerArtifactStoreExt, error) {
var provider DockerArtifactStoreExt
query := "SELECT docker_artifact_store.*, count(jq.ia_id) as deployment_count FROM docker_artifact_store" +
func (impl DockerArtifactStoreRepositoryImpl) FindDeploymentCount(storeId string) (int, error) {
var DeploymentCount int
query := "SELECT count(jq.ia_id) as deployment_count FROM docker_artifact_store" +
fmt.Sprintf(" LEFT JOIN oci_registry_config orc on (docker_artifact_store.id = orc.docker_artifact_store_id and orc.is_chart_pull_active = true and orc.deleted = false and orc.repository_type = '%s' and (orc.repository_action = '%s' or orc.repository_action = '%s'))", OCI_REGISRTY_REPO_TYPE_CHART, STORAGE_ACTION_TYPE_PULL, STORAGE_ACTION_TYPE_PULL_AND_PUSH) +
" LEFT JOIN (SELECT aps.docker_artifact_store_id as das_id ,ia.id as ia_id FROM installed_app_versions iav INNER JOIN installed_apps ia on iav.installed_app_id = ia.id INNER JOIN app_store_application_version asav on iav.app_store_application_version_id = asav.id INNER JOIN app_store aps on asav.app_store_id = aps.id WHERE ia.active=true and iav.active=true) jq on jq.das_id = docker_artifact_store.id" +
" WHERE docker_artifact_store.id = ? and docker_artifact_store.active = true Group by docker_artifact_store.id;"
_, err := impl.dbConnection.Query(&provider, query, storeId)
return &provider, err
_, err := impl.dbConnection.Query(&DeploymentCount, query, storeId)
return DeploymentCount, err
}

func (impl DockerArtifactStoreRepositoryImpl) FindOneWithChartDeploymentCount(storeId, chartName string) (*ChartDeploymentCount, error) {
Expand Down
24 changes: 22 additions & 2 deletions pkg/app/AppCrudOperationService.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
client "github.com/devtron-labs/devtron/api/helm-app/service"
helmBean "github.com/devtron-labs/devtron/api/helm-app/service/bean"
"github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/pkg/app/dbMigration"
"github.com/devtron-labs/devtron/pkg/appStore/installedApp/service/EAMode"
util2 "github.com/devtron-labs/devtron/pkg/appStore/util"
bean2 "github.com/devtron-labs/devtron/pkg/auth/user/bean"
Expand Down Expand Up @@ -88,6 +89,7 @@ type AppCrudOperationServiceImpl struct {
gitMaterialRepository pipelineConfig.MaterialRepository
installedAppDbService EAMode.InstalledAppDBService
crudOperationServiceConfig *CrudOperationServiceConfig
dbMigration dbMigration.DbMigration
}

func NewAppCrudOperationServiceImpl(appLabelRepository pipelineConfig.AppLabelRepository,
Expand All @@ -96,7 +98,8 @@ func NewAppCrudOperationServiceImpl(appLabelRepository pipelineConfig.AppLabelRe
genericNoteService genericNotes.GenericNoteService,
gitMaterialRepository pipelineConfig.MaterialRepository,
installedAppDbService EAMode.InstalledAppDBService,
crudOperationServiceConfig *CrudOperationServiceConfig) *AppCrudOperationServiceImpl {
crudOperationServiceConfig *CrudOperationServiceConfig,
dbMigration dbMigration.DbMigration) *AppCrudOperationServiceImpl {
impl := &AppCrudOperationServiceImpl{
appLabelRepository: appLabelRepository,
logger: logger,
Expand All @@ -106,6 +109,7 @@ func NewAppCrudOperationServiceImpl(appLabelRepository pipelineConfig.AppLabelRe
genericNoteService: genericNoteService,
gitMaterialRepository: gitMaterialRepository,
installedAppDbService: installedAppDbService,
dbMigration: dbMigration,
}
crudOperationServiceConfig, err := GetCrudOperationServiceConfig()
if err != nil {
Expand Down Expand Up @@ -463,13 +467,29 @@ func (impl AppCrudOperationServiceImpl) getAppAndProjectForAppIdentifier(appIden
var err error
appNameUniqueIdentifier := appIdentifier.GetUniqueAppNameIdentifier()
app, err = impl.appRepository.FindAppAndProjectByAppName(appNameUniqueIdentifier)
if err != nil && err != pg.ErrNoRows {
if err != nil && err != pg.ErrNoRows && err != pg.ErrMultiRows {
impl.logger.Errorw("error in fetching app meta data by unique app identifier", "appNameUniqueIdentifier", appNameUniqueIdentifier, "err", err)
return app, err
}
if err == pg.ErrMultiRows {
validApp, err := impl.dbMigration.FixMultipleAppsForInstalledApp(appNameUniqueIdentifier)
if err != nil {
impl.logger.Errorw("error in fixing multiple installed app entries", "appName", appNameUniqueIdentifier, "err", err)
return app, err
}
return validApp, err
}
if util.IsErrNoRows(err) {
//find app by display name if not found by unique identifier
app, err = impl.appRepository.FindAppAndProjectByAppName(appIdentifier.ReleaseName)
if err == pg.ErrMultiRows {
validApp, err := impl.dbMigration.FixMultipleAppsForInstalledApp(appIdentifier.ReleaseName)
if err != nil {
impl.logger.Errorw("error in fixing multiple installed app entries", "appName", appIdentifier.ReleaseName, "err", err)
return app, err
}
return validApp, err
}
if err != nil {
impl.logger.Errorw("error in fetching app meta data by display name", "displayName", appIdentifier.ReleaseName, "err", err)
return app, err
Expand Down
Loading
Loading