func()

in controllers/cdstagedeploy/chain/resolve_status.go [27:91]


func (r *ResolveStatus) ServeRequest(ctx context.Context, stageDeploy *codebaseApi.CDStageDeploy) error {
	log := ctrl.LoggerFrom(ctx)

	if stageDeploy.IsFailed() {
		log.Info("CDStageDeploy has failed status. Retry to deploy.")
		return nil
	}

	pipelineRun, err := r.getRunningPipelines(ctx, stageDeploy)
	if err != nil {
		return fmt.Errorf("failed to get running pipelines: %w", err)
	}

	if stageDeploy.IsPending() {
		if allPipelineRunsCompleted(pipelineRun.Items) {
			log.Info("CDStageDeploy has pending status. Start deploying.")

			return nil
		}

		log.Info("Put CDStageDeploy in queue. Some PipelineRuns are still running.")

		stageDeploy.Status.Status = codebaseApi.CDStageDeployStatusInQueue

		return nil
	}

	if stageDeploy.IsInQueue() {
		shouldStart, err := r.shouldStartCDStageDeploy(
			ctx,
			stageDeploy.Name,
			stageDeploy.GetStageCRName(),
			stageDeploy.Spec.Pipeline,
			stageDeploy.Namespace,
			pipelineRun.Items,
		)
		if err != nil {
			return fmt.Errorf("failed to check running CDStageDeploys: %w", err)
		}

		if shouldStart {
			log.Info("Starting processing CDStageDeploy.")

			stageDeploy.Status.Status = codebaseApi.CDStageDeployStatusPending
		}

		return nil
	}

	if stageDeploy.IsRunning() {
		if allPipelineRunsCompleted(pipelineRun.Items) {
			log.Info("All PipelineRuns have been completed.")

			stageDeploy.Status.Status = codebaseApi.CDStageDeployStatusCompleted

			return nil
		}

		log.Info("Some PipelineRuns are still running.")

		return nil
	}

	return nil
}