Skip to content

fix: hibernating status is not being updated in app listing page #5294

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 2 commits into from
Jun 12, 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
2 changes: 1 addition & 1 deletion cmd/external-app/wire_gen.go

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

22 changes: 22 additions & 0 deletions internal/sql/repository/pipelineConfig/CdWorfkflowRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type CdWorkflowRepository interface {
FindWorkflowRunnerById(wfrId int) (*CdWorkflowRunner, error)
FindRetriedWorkflowCountByReferenceId(wfrId int) (int, error)
FindLatestWfrByAppIdAndEnvironmentId(appId int, environmentId int) (*CdWorkflowRunner, error)
FindLastUnFailedProcessedRunner(appId int, environmentId int) (*CdWorkflowRunner, error)
IsLatestCDWfr(pipelineId, wfrId int) (bool, error)
FindLatestCdWorkflowRunnerByEnvironmentIdAndRunnerType(appId int, environmentId int, runnerType bean.WorkflowType) (CdWorkflowRunner, error)
FindAllTriggeredWorkflowCountInLast24Hour() (cdWorkflowCount int, err error)
Expand Down Expand Up @@ -490,6 +491,27 @@ func (impl *CdWorkflowRepositoryImpl) FindLatestWfrByAppIdAndEnvironmentId(appId
return wfr, nil
}

func (impl *CdWorkflowRepositoryImpl) FindLastUnFailedProcessedRunner(appId int, environmentId int) (*CdWorkflowRunner, error) {
wfr := &CdWorkflowRunner{}
err := impl.dbConnection.
Model(wfr).
Column("cd_workflow_runner.*", "CdWorkflow.Pipeline.id", "CdWorkflow.Pipeline.deployment_app_delete_request", "CdWorkflow.Pipeline.deployment_app_type").
Where("p.environment_id = ?", environmentId).
Where("p.app_id = ?", appId).
Where("cd_workflow_runner.workflow_type = ?", bean.CD_WORKFLOW_TYPE_DEPLOY).
Where("cd_workflow_runner.status NOT IN (?)", pg.In([]string{WorkflowInitiated, WorkflowInQueue, WorkflowFailed})).
Order("cd_workflow_runner.id DESC").
Join("inner join cd_workflow wf on wf.id = cd_workflow_runner.cd_workflow_id").
Join("inner join pipeline p on p.id = wf.pipeline_id").
Limit(1).
Select()
if err != nil {
return wfr, err
}
return wfr, nil

}

func (impl *CdWorkflowRepositoryImpl) IsLatestCDWfr(pipelineId, wfrId int) (bool, error) {
wfr := &CdWorkflowRunner{}
ifAnySuccessorWfrExists, err := impl.dbConnection.
Expand Down
16 changes: 12 additions & 4 deletions internal/sql/repository/pipelineConfig/PipelineRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,13 +722,21 @@ func (impl PipelineRepositoryImpl) FindDeploymentTypeByPipelineIds(cdPipelineIds
pipelineIdsMap := make(map[int]DeploymentObject)

var deploymentType []DeploymentObject
query := "with pcos as(select max(id) as id from pipeline_config_override where pipeline_id in (?) " +
"group by pipeline_id) select pco.deployment_type,pco.pipeline_id, aps.status from pipeline_config_override " +
//query := "with pcos as(select max(id) as id from pipeline_config_override where pipeline_id in (?) " +
// "group by pipeline_id) select pco.deployment_type,pco.pipeline_id, aps.status from pipeline_config_override " +
// "pco inner join pcos on pcos.id=pco.id" +
// " inner join pipeline p on p.id=pco.pipeline_id left join app_status aps on aps.app_id=p.app_id " +
// "and aps.env_id=p.environment_id;"
query := " WITH pcos AS " +
" (SELECT max(p.id) AS id FROM pipeline_config_override p " +
" INNER JOIN cd_workflow_runner cdwr ON cdwr.cd_workflow_id = p.cd_workflow_id " +
// pipeline ids deploy type initiated,queued,failed
" WHERE p.pipeline_id in (?) AND cdwr.workflow_type = ? AND cdwr.status NOT IN (?) " +
" GROUP BY p.pipeline_id) select pco.deployment_type,pco.pipeline_id, aps.status from pipeline_config_override " +
"pco inner join pcos on pcos.id=pco.id" +
" inner join pipeline p on p.id=pco.pipeline_id left join app_status aps on aps.app_id=p.app_id " +
"and aps.env_id=p.environment_id;"

_, err := impl.dbConnection.Query(&deploymentType, query, pg.In(cdPipelineIds), true)
_, err := impl.dbConnection.Query(&deploymentType, query, pg.In(cdPipelineIds), bean.CD_WORKFLOW_TYPE_DEPLOY, pg.In([]string{WorkflowInitiated, WorkflowInQueue, WorkflowFailed}))
if err != nil {
return pipelineIdsMap, err
}
Expand Down
53 changes: 51 additions & 2 deletions pkg/app/AppService.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ import (
"context"
"encoding/json"
"fmt"
health2 "github.com/argoproj/gitops-engine/pkg/health"
argoApplication "github.com/devtron-labs/devtron/client/argocdServer/bean"
"github.com/devtron-labs/devtron/internal/sql/models"
commonBean "github.com/devtron-labs/devtron/pkg/deployment/gitOps/common/bean"
"github.com/devtron-labs/devtron/pkg/deployment/gitOps/config"
"github.com/devtron-labs/devtron/pkg/deployment/gitOps/git"
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate"
bean4 "github.com/devtron-labs/devtron/pkg/deployment/trigger/devtronApps/bean"
"github.com/pkg/errors"
"io/ioutil"
"net/url"
"path"
Expand Down Expand Up @@ -117,6 +120,7 @@ type AppServiceImpl struct {
gitOpsConfigReadService config.GitOpsConfigReadService
gitOperationService git.GitOperationService
deploymentTemplateService deploymentTemplate.DeploymentTemplateService
appListingService AppListingService
}

type AppService interface {
Expand All @@ -139,6 +143,7 @@ type AppService interface {
GetDeployedManifestByPipelineIdAndCDWorkflowId(appId int, envId int, cdWorkflowId int, ctx context.Context) ([]byte, error)
//SetPipelineFieldsInOverrideRequest(overrideRequest *bean.ValuesOverrideRequest, pipeline *pipelineConfig.Pipeline)
GetActiveCiCdAppsCount() (int, error)
ComputeAppstatus(appId, envId int, status health2.HealthStatusCode) (string, error)
}

func NewAppService(
Expand All @@ -162,7 +167,8 @@ func NewAppService(
installedAppVersionHistoryRepository repository4.InstalledAppVersionHistoryRepository,
scopedVariableManager variables.ScopedVariableCMCSManager, acdConfig *argocdServer.ACDConfig,
gitOpsConfigReadService config.GitOpsConfigReadService, gitOperationService git.GitOperationService,
deploymentTemplateService deploymentTemplate.DeploymentTemplateService) *AppServiceImpl {
deploymentTemplateService deploymentTemplate.DeploymentTemplateService,
appListingService AppListingService) *AppServiceImpl {
appServiceImpl := &AppServiceImpl{
environmentConfigRepository: environmentConfigRepository,
mergeUtil: mergeUtil,
Expand Down Expand Up @@ -192,6 +198,7 @@ func NewAppService(
gitOpsConfigReadService: gitOpsConfigReadService,
gitOperationService: gitOperationService,
deploymentTemplateService: deploymentTemplateService,
appListingService: appListingService,
}
return appServiceImpl
}
Expand Down Expand Up @@ -263,6 +270,42 @@ func (impl *AppServiceImpl) UpdateDeploymentStatusAndCheckIsSucceeded(app *v1alp
return isSucceeded, pipelineOverride, nil
}

func (impl *AppServiceImpl) ComputeAppstatus(appId, envId int, status health2.HealthStatusCode) (string, error) {
appStatusInternal := string(status)

// get the last accepted deploy type workflow runner, accepted state -> not in (initiated/queued/failed)
cdWfr, err := impl.cdWorkflowRepository.FindLastUnFailedProcessedRunner(appId, envId)
if err != nil {
impl.logger.Errorw("error in getting latest wfr by appId and envId", "err", err, "appId", appId, "envId", envId)
return "", err
}

override, err := impl.pipelineOverrideRepository.FindLatestByCdWorkflowId(cdWfr.CdWorkflowId)
if err != nil {
impl.logger.Errorw("error in getting latest wfr by pipelineId", "cdWorkflowId", cdWfr.CdWorkflowId, "err", err)
return "", err
}

if errors.Is(err, pg.ErrNoRows) {
// not deployed
return appStatusInternal, nil
}

// this is not stop/start type
if override.DeploymentType != models.DEPLOYMENTTYPE_STOP && override.DeploymentType != models.DEPLOYMENTTYPE_START {
return appStatusInternal, nil
}

// for stop, then user requested for hibernate, then check for hibernation.
IslastAcceptedReleaseIsStopRequest := models.DEPLOYMENTTYPE_STOP == override.DeploymentType
// request essentially means that the previous state of the release was hibernation/partial hibernation

if IslastAcceptedReleaseIsStopRequest {
appStatusInternal = appStatus.GetHibernationStatus(status)
}
return appStatusInternal, nil
}

func (impl *AppServiceImpl) UpdateDeploymentStatusForGitOpsPipelines(app *v1alpha1.Application, statusTime time.Time, isAppStore bool) (bool, bool, *chartConfig.PipelineOverride, error) {
isSucceeded := false
isTimelineUpdated := false
Expand Down Expand Up @@ -296,7 +339,13 @@ func (impl *AppServiceImpl) UpdateDeploymentStatusForGitOpsPipelines(app *v1alph
impl.logger.Errorw("error in getting latest timeline before update", "err", err, "cdWfrId", cdWfr.Id)
return isSucceeded, isTimelineUpdated, pipelineOverride, err
}
err = impl.appStatusService.UpdateStatusWithAppIdEnvId(cdPipeline.AppId, cdPipeline.EnvironmentId, string(app.Status.Health.Status))

appStatusInternal, err := impl.ComputeAppstatus(cdPipeline.AppId, cdPipeline.EnvironmentId, app.Status.Health.Status)
if err != nil {
impl.logger.Errorw("error in checking if last release is stop type", "err", err, cdPipeline.AppId, "envId", cdPipeline.EnvironmentId)
return isSucceeded, isTimelineUpdated, pipelineOverride, err
}
err = impl.appStatusService.UpdateStatusWithAppIdEnvId(cdPipeline.AppId, cdPipeline.EnvironmentId, appStatusInternal)
if err != nil {
impl.logger.Errorw("error occurred while updating app status in app_status table", "error", err, "appId", cdPipeline.AppId, "envId", cdPipeline.EnvironmentId)
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/appStatus/AppStatusService.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package appStatus

import (
"github.com/argoproj/gitops-engine/pkg/health"
"github.com/devtron-labs/devtron/internal/sql/repository/appStatus"
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
"github.com/devtron-labs/devtron/util/rbac"
Expand Down Expand Up @@ -103,3 +104,11 @@ func (impl *AppStatusServiceImpl) DeleteWithAppIdEnvId(tx *pg.Tx, appId, envId i
}
return nil
}

func GetHibernationStatus(status health.HealthStatusCode) string {
switch status {
case health.HealthStatusHealthy:
return HealthStatusHibernatingFilter
}
return string(status)
}
5 changes: 5 additions & 0 deletions pkg/deployment/deployedApp/DeployedAppService.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package deployedApp
import (
"context"
"encoding/json"
"errors"
"fmt"
util5 "github.com/devtron-labs/common-lib/utils/k8s"
bean2 "github.com/devtron-labs/devtron/api/bean"
Expand All @@ -29,6 +30,7 @@ import (
"github.com/devtron-labs/devtron/pkg/deployment/trigger/devtronApps"
bean3 "github.com/devtron-labs/devtron/pkg/deployment/trigger/devtronApps/bean"
"github.com/devtron-labs/devtron/pkg/k8s"
"github.com/go-pg/pg"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -81,6 +83,9 @@ func (impl *DeployedAppServiceImpl) StopStartApp(ctx context.Context, stopReques
//FIXME
}
wf, err := impl.cdWorkflowRepository.FindLatestCdWorkflowByPipelineId(pipelineIds)
if errors.Is(err, pg.ErrNoRows) {
return 0, errors.New("no deployment history found,this app was never deployed")
}
if err != nil {
impl.logger.Errorw("error in fetching latest release", "err", err)
return 0, err
Expand Down
18 changes: 14 additions & 4 deletions pkg/workflow/status/WorkflowStatusService.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type WorkflowStatusServiceImpl struct {
installedAppRepository repository3.InstalledAppRepository
pipelineStatusTimelineRepository pipelineConfig.PipelineStatusTimelineRepository
pipelineRepository pipelineConfig.PipelineRepository
appListingService app.AppListingService

application application.ServiceClient
}
Expand All @@ -103,7 +104,9 @@ func NewWorkflowStatusServiceImpl(logger *zap.SugaredLogger,
installedAppRepository repository3.InstalledAppRepository,
pipelineStatusTimelineRepository pipelineConfig.PipelineStatusTimelineRepository,
pipelineRepository pipelineConfig.PipelineRepository,
application application.ServiceClient) (*WorkflowStatusServiceImpl, error) {
application application.ServiceClient,
appListingService app.AppListingService,
) (*WorkflowStatusServiceImpl, error) {
impl := &WorkflowStatusServiceImpl{
logger: logger,
workflowDagExecutor: workflowDagExecutor,
Expand All @@ -125,6 +128,7 @@ func NewWorkflowStatusServiceImpl(logger *zap.SugaredLogger,
pipelineStatusTimelineRepository: pipelineStatusTimelineRepository,
pipelineRepository: pipelineRepository,
application: application,
appListingService: appListingService,
}
config, err := types.GetCdConfig()
if err != nil {
Expand Down Expand Up @@ -263,11 +267,17 @@ func (impl *WorkflowStatusServiceImpl) UpdatePipelineTimelineAndStatusByLiveAppl
}
isSucceeded, isTimelineUpdated, pipelineOverride, err = impl.appService.UpdateDeploymentStatusForGitOpsPipelines(app, time.Now(), isAppStore)
if err != nil {
impl.logger.Errorw("error in updating deployment status for gitOps cd pipelines", "app", app)
impl.logger.Errorw("error in updating deployment status for gitOps cd pipelines", "app", app, "err", err)
return err, isTimelineUpdated
}
appStatus := app.Status.Health.Status
err = impl.appStatusService.UpdateStatusWithAppIdEnvId(pipeline.AppId, pipeline.EnvironmentId, string(appStatus))

appStatus, err := impl.appService.ComputeAppstatus(pipeline.AppId, pipeline.EnvironmentId, app.Status.Health.Status)
if err != nil {
impl.logger.Errorw("error in checking if last release is stop type", "err", err, pipeline.AppId, "envId", pipeline.EnvironmentId)
return err, isTimelineUpdated
}

err = impl.appStatusService.UpdateStatusWithAppIdEnvId(pipeline.AppId, pipeline.EnvironmentId, appStatus)
if err != nil {
impl.logger.Errorw("error occurred while updating app-status for cd pipeline", "err", err, "appId", pipeline.AppId, "envId", pipeline.EnvironmentId)
impl.logger.Debugw("ignoring the error, UpdateStatusWithAppIdEnvId", "err", err, "appId", pipeline.AppId, "envId", pipeline.EnvironmentId)
Expand Down
Loading
Loading