diff --git a/api/restHandler/app/appList/AppListingRestHandler.go b/api/restHandler/app/appList/AppListingRestHandler.go index 7a08a3db07..a995a4f12b 100644 --- a/api/restHandler/app/appList/AppListingRestHandler.go +++ b/api/restHandler/app/appList/AppListingRestHandler.go @@ -547,7 +547,7 @@ func (handler AppListingRestHandlerImpl) FetchAppDetails(w http.ResponseWriter, apiError, ok := err.(*util.ApiError) if ok && apiError != nil { if apiError.Code == constants.AppDetailResourceTreeNotFound && appDetail.DeploymentAppDeleteRequest == true { - acdAppFound, _ := handler.pipeline.MarkGitOpsDevtronAppsDeletedWhereArgoAppIsDeleted(appId, envId, acdToken, cdPipeline) + acdAppFound, _ := handler.pipeline.MarkGitOpsDevtronAppsDeletedWhereArgoAppIsDeleted(acdToken, cdPipeline) if acdAppFound { common.WriteJsonResp(w, fmt.Errorf("unable to fetch resource tree"), nil, http.StatusInternalServerError) return @@ -638,20 +638,20 @@ func (handler AppListingRestHandlerImpl) FetchResourceTree(w http.ResponseWriter resourceTree, err := handler.fetchResourceTree(w, r, appId, envId, acdToken, cdPipeline) if err != nil { handler.logger.Errorw("error in fetching resource tree", "err", err, "appId", appId, "envId", envId) - handler.handleResourceTreeErrAndDeletePipelineIfNeeded(w, err, appId, envId, acdToken, cdPipeline) + handler.handleResourceTreeErrAndDeletePipelineIfNeeded(w, err, acdToken, cdPipeline) return } common.WriteJsonResp(w, err, resourceTree, http.StatusOK) } func (handler AppListingRestHandlerImpl) handleResourceTreeErrAndDeletePipelineIfNeeded(w http.ResponseWriter, err error, - appId int, envId int, acdToken string, cdPipeline *pipelineConfig.Pipeline) { + acdToken string, cdPipeline *pipelineConfig.Pipeline) { var apiError *util.ApiError ok := errors.As(err, &apiError) if cdPipeline.DeploymentAppType == util.PIPELINE_DEPLOYMENT_TYPE_ACD { if ok && apiError != nil { if apiError.Code == constants.AppDetailResourceTreeNotFound && cdPipeline.DeploymentAppDeleteRequest == true && cdPipeline.DeploymentAppCreated == true { - acdAppFound, appDeleteErr := handler.pipeline.MarkGitOpsDevtronAppsDeletedWhereArgoAppIsDeleted(appId, envId, acdToken, cdPipeline) + acdAppFound, appDeleteErr := handler.pipeline.MarkGitOpsDevtronAppsDeletedWhereArgoAppIsDeleted(acdToken, cdPipeline) if appDeleteErr != nil { apiError.UserMessage = constants.ErrorDeletingPipelineForDeletedArgoAppMsg common.WriteJsonResp(w, apiError, nil, http.StatusInternalServerError) diff --git a/internal/sql/repository/pipelineConfig/PipelineRepository.go b/internal/sql/repository/pipelineConfig/PipelineRepository.go index e5cc702937..35645bb35d 100644 --- a/internal/sql/repository/pipelineConfig/PipelineRepository.go +++ b/internal/sql/repository/pipelineConfig/PipelineRepository.go @@ -75,6 +75,7 @@ type PipelineRepository interface { Update(pipeline *Pipeline, tx *pg.Tx) error FindActiveByAppId(appId int) (pipelines []*Pipeline, err error) Delete(id int, userId int32, tx *pg.Tx) error + MarkPartiallyDeleted(id int, userId int32, tx *pg.Tx) error FindByName(pipelineName string) (pipeline *Pipeline, err error) PipelineExists(pipelineName string) (bool, error) FindById(id int) (pipeline *Pipeline, err error) @@ -88,7 +89,6 @@ type PipelineRepository interface { GetByEnvOverrideId(envOverrideId int) ([]Pipeline, error) GetByEnvOverrideIdAndEnvId(envOverrideId, envId int) (Pipeline, error) FindActiveByAppIdAndEnvironmentId(appId int, environmentId int) (pipelines []*Pipeline, err error) - UndoDelete(id int) error UniqueAppEnvironmentPipelines() ([]*Pipeline, error) FindByCiPipelineId(ciPipelineId int) (pipelines []*Pipeline, err error) FindByParentCiPipelineId(ciPipelineId int) (pipelines []*Pipeline, err error) @@ -278,11 +278,18 @@ func (impl PipelineRepositoryImpl) Delete(id int, userId int32, tx *pg.Tx) error return err } -func (impl PipelineRepositoryImpl) UndoDelete(id int) error { +func (impl PipelineRepositoryImpl) MarkPartiallyDeleted(id int, userId int32, tx *pg.Tx) error { pipeline := &Pipeline{} - _, err := impl.dbConnection.Model(pipeline).Set("deleted =?", false).Where("id =?", id).Update() + _, err := tx.Model(pipeline). + Set("deployment_app_delete_request = ?", true). + Set("updated_on = ?", time.Now()). + Set("updated_by = ?", userId). + Where("deleted = ?", false). + Where("id = ?", id). + Update() return err } + func (impl PipelineRepositoryImpl) FindByName(pipelineName string) (pipeline *Pipeline, err error) { pipeline = &Pipeline{} err = impl.dbConnection.Model(pipeline). @@ -664,6 +671,7 @@ func (impl PipelineRepositoryImpl) FindIdsByProjectIdsAndEnvironmentIds(projectI func (impl PipelineRepositoryImpl) GetArgoPipelineByArgoAppName(argoAppName string) (Pipeline, error) { var pipeline Pipeline err := impl.dbConnection.Model(&pipeline). + Column("pipeline.*", "Environment"). Where("deployment_app_name = ?", argoAppName). Where("deployment_app_type = ?", util.PIPELINE_DEPLOYMENT_TYPE_ACD). Where("deleted = ?", false). diff --git a/pkg/pipeline/DeploymentPipelineConfigService.go b/pkg/pipeline/DeploymentPipelineConfigService.go index ed42e7a979..40efabf736 100644 --- a/pkg/pipeline/DeploymentPipelineConfigService.go +++ b/pkg/pipeline/DeploymentPipelineConfigService.go @@ -113,7 +113,7 @@ type CdPipelineConfigService interface { GetBulkActionImpactedPipelines(dto *bean.CdBulkActionRequestDto) ([]*pipelineConfig.Pipeline, error) //no usage // IsGitOpsRequiredForCD : Determine if GitOps is required for CD based on the provided pipeline creation request IsGitOpsRequiredForCD(pipelineCreateRequest *bean.CdPipelines) bool - MarkGitOpsDevtronAppsDeletedWhereArgoAppIsDeleted(appId int, envId int, acdToken string, pipeline *pipelineConfig.Pipeline) (bool, error) + MarkGitOpsDevtronAppsDeletedWhereArgoAppIsDeleted(acdToken string, pipeline *pipelineConfig.Pipeline) (bool, error) // GetEnvironmentListForAutocompleteFilter : lists environment for given configuration GetEnvironmentListForAutocompleteFilter(envName string, clusterIds []int, offset int, size int, token string, checkAuthBatch func(token string, appObject []string, envObject []string) (map[string]bool, map[string]bool), ctx context.Context) (*clutserBean.ResourceGroupingResponse, error) RegisterInACD(ctx context.Context, chartGitAttr *commonBean.ChartGitAttribute, userId int32) error @@ -824,7 +824,7 @@ func (impl *CdPipelineConfigServiceImpl) DeleteCdPipeline(pipeline *pipelineConf if pipeline.DeploymentAppCreated == true { deploymentAppName := fmt.Sprintf("%s-%s", pipeline.App.AppName, pipeline.Environment.Name) if util.IsAcdApp(pipeline.DeploymentAppType) { - if !deleteResponse.ClusterReachable { + if !forceDelete && !deleteResponse.ClusterReachable { impl.logger.Errorw("cluster connection error", "err", clusterBean.ErrorInConnecting) if cascadeDelete { return deleteResponse, nil @@ -1427,7 +1427,7 @@ func (impl *CdPipelineConfigServiceImpl) IsGitOpsRequiredForCD(pipelineCreateReq return haveAtLeastOneGitOps } -func (impl *CdPipelineConfigServiceImpl) MarkGitOpsDevtronAppsDeletedWhereArgoAppIsDeleted(appId int, envId int, acdToken string, pipeline *pipelineConfig.Pipeline) (bool, error) { +func (impl *CdPipelineConfigServiceImpl) MarkGitOpsDevtronAppsDeletedWhereArgoAppIsDeleted(acdToken string, pipeline *pipelineConfig.Pipeline) (bool, error) { acdAppFound := false ctx := context.Background() @@ -1985,7 +1985,7 @@ func (impl *CdPipelineConfigServiceImpl) DeleteCdPipelinePartial(pipeline *pipel if pipeline.DeploymentAppCreated && !pipeline.DeploymentAppDeleteRequest { deploymentAppName := fmt.Sprintf("%s-%s", pipeline.App.AppName, pipeline.Environment.Name) if util.IsAcdApp(pipeline.DeploymentAppType) { - if !deleteResponse.ClusterReachable { + if !forceDelete && !deleteResponse.ClusterReachable { impl.logger.Errorw("cluster connection error", "err", clusterBean.ErrorInConnecting) if cascadeDelete { return deleteResponse, nil @@ -2018,10 +2018,7 @@ func (impl *CdPipelineConfigServiceImpl) DeleteCdPipelinePartial(pipeline *pipel } } impl.logger.Infow("app deleted from argocd", "id", pipeline.Id, "pipelineName", pipeline.Name, "app", deploymentAppName) - pipeline.DeploymentAppDeleteRequest = true - pipeline.UpdatedOn = time.Now() - pipeline.UpdatedBy = userId - err = impl.pipelineRepository.Update(pipeline, tx) + err = impl.pipelineRepository.MarkPartiallyDeleted(pipeline.Id, userId, tx) if err != nil { impl.logger.Errorw("error in partially delete cd pipeline", "err", err) return deleteResponse, err