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

chore: oss ent triggerService changes #6189

Merged
merged 15 commits into from
Dec 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (handler PipelineTriggerRestHandlerImpl) OverrideConfig(w http.ResponseWrit
triggerContext := bean3.TriggerContext{
Context: ctx,
}
mergeResp, helmPackageName, err := handler.cdTriggerService.ManualCdTrigger(triggerContext, &overrideRequest)
mergeResp, helmPackageName, _, err := handler.cdTriggerService.ManualCdTrigger(triggerContext, &overrideRequest)
span.End()
if err != nil {
handler.logger.Errorw("request err, OverrideConfig", "err", err, "payload", overrideRequest)
Expand Down
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.

20 changes: 11 additions & 9 deletions pkg/app/AppService.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import (
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig/adapter/cdWorkflow"
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig/bean/timelineStatus"
cdWorkflow2 "github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig/bean/workflow/cdWorkflow"
"github.com/devtron-labs/devtron/pkg/argoApplication/helper"
bean3 "github.com/devtron-labs/devtron/pkg/app/bean"
installedAppReader "github.com/devtron-labs/devtron/pkg/appStore/installedApp/read"
"github.com/devtron-labs/devtron/pkg/argoApplication/helper"
common2 "github.com/devtron-labs/devtron/pkg/deployment/common"
bean2 "github.com/devtron-labs/devtron/pkg/deployment/common/bean"
commonBean "github.com/devtron-labs/devtron/pkg/deployment/gitOps/common/bean"
Expand Down Expand Up @@ -775,14 +776,15 @@ func (impl *AppServiceImpl) BuildCDSuccessPayload(appName string, environmentNam
}

type ValuesOverrideResponse struct {
MergedValues string
ReleaseOverrideJSON string
EnvOverride *bean6.EnvConfigOverride
PipelineStrategy *chartConfig.PipelineStrategy
PipelineOverride *chartConfig.PipelineOverride
Artifact *repository.CiArtifact
Pipeline *pipelineConfig.Pipeline
DeploymentConfig *bean2.DeploymentConfig
MergedValues string
ReleaseOverrideJSON string
EnvOverride *bean6.EnvConfigOverride
PipelineStrategy *chartConfig.PipelineStrategy
PipelineOverride *chartConfig.PipelineOverride
Artifact *repository.CiArtifact
Pipeline *pipelineConfig.Pipeline
DeploymentConfig *bean2.DeploymentConfig
ManifestPushTemplate *bean3.ManifestPushTemplate
}

func (impl *AppServiceImpl) buildACDContext() (acdContext context.Context, err error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/deployment/deployedApp/DeployedAppService.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (impl *DeployedAppServiceImpl) StopStartApp(ctx context.Context, stopReques
Context: ctx,
ReferenceId: stopRequest.ReferenceId,
}
id, _, err := impl.cdTriggerService.ManualCdTrigger(triggerContext, overrideRequest)
id, _, _, err := impl.cdTriggerService.ManualCdTrigger(triggerContext, overrideRequest)
if err != nil {
impl.logger.Errorw("error in stopping app", "err", err, "appId", stopRequest.AppId, "envId", stopRequest.EnvironmentId)
return 0, err
Expand Down
62 changes: 44 additions & 18 deletions pkg/deployment/trigger/devtronApps/PostStageTriggerService.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
bean2 "github.com/devtron-labs/devtron/api/bean"
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig/bean/workflow/cdWorkflow"
bean4 "github.com/devtron-labs/devtron/pkg/app/bean"
"github.com/devtron-labs/devtron/pkg/deployment/trigger/devtronApps/bean"
bean3 "github.com/devtron-labs/devtron/pkg/pipeline/bean"
repository3 "github.com/devtron-labs/devtron/pkg/pipeline/history/repository"
Expand All @@ -29,9 +30,9 @@ import (
"time"
)

func (impl *TriggerServiceImpl) TriggerPostStage(request bean.TriggerRequest) error {
func (impl *TriggerServiceImpl) TriggerPostStage(request bean.TriggerRequest) (*bean4.ManifestPushTemplate, error) {
request.WorkflowType = bean2.CD_WORKFLOW_TYPE_POST
//setting triggeredAt variable to have consistent data for various audit log places in db for deployment time
// setting triggeredAt variable to have consistent data for various audit log places in db for deployment time
triggeredAt := time.Now()
triggeredBy := request.TriggeredBy
pipeline := request.Pipeline
Expand All @@ -40,21 +41,23 @@ func (impl *TriggerServiceImpl) TriggerPostStage(request bean.TriggerRequest) er
env, namespace, err := impl.getEnvAndNsIfRunStageInEnv(ctx, request)
if err != nil {
impl.logger.Errorw("error, getEnvAndNsIfRunStageInEnv", "err", err, "pipeline", pipeline, "stage", request.WorkflowType)
return nil
return nil, nil
}
request.RunStageInEnvNamespace = namespace

cdWf, runner, err := impl.createStartingWfAndRunner(request, triggeredAt)
if err != nil {
impl.logger.Errorw("error in creating wf starting and runner entry", "err", err, "request", request)
return err
return nil, err
}
if cdWf.CiArtifact == nil || cdWf.CiArtifact.Id == 0 {
cdWf.CiArtifact, err = impl.ciArtifactRepository.Get(cdWf.CiArtifactId)
if err != nil {
impl.logger.Errorw("error fetching artifact data", "err", err)
return err
return nil, err
}
}

// Migration of deprecated DataSource Type
if cdWf.CiArtifact.IsMigrationRequired() {
migrationErr := impl.ciArtifactRepository.MigrateToWebHookDataSourceType(cdWf.CiArtifact.Id)
Expand All @@ -63,24 +66,43 @@ func (impl *TriggerServiceImpl) TriggerPostStage(request bean.TriggerRequest) er
}
}

filterEvaluationAudit, err := impl.checkFeasibilityForPostStage(pipeline, &request, env, cdWf, triggeredBy)
if err != nil {
impl.logger.Errorw("error, checkFeasibilityForPostStage", "err", err, "pipeline", pipeline)
return nil, nil
}

envDevploymentConfig, err := impl.deploymentConfigService.GetAndMigrateConfigIfAbsentForDevtronApps(pipeline.AppId, pipeline.EnvironmentId)
if err != nil {
impl.logger.Errorw("error in fetching deployment config by appId and envId", "appId", pipeline.AppId, "envId", pipeline.EnvironmentId, "err", err)
return err
return nil, err
}

dbErr := impl.createAuditDataForDeploymentWindowBypass(request, runner.Id)
if dbErr != nil {
impl.logger.Errorw("error in creating audit data for deployment window bypass", "runnerId", runner.Id, "err", dbErr)
// skip error for audit data creation
}

err = impl.handlerFilterEvaluationAudit(filterEvaluationAudit, runner)
if err != nil {
impl.logger.Errorw("error, handlerFilterEvaluationAudit", "err", err)
return nil, err
}

// custom GitOps repo url validation --> Start
err = impl.handleCustomGitOpsRepoValidation(runner, pipeline, envDevploymentConfig, triggeredBy)
if err != nil {
impl.logger.Errorw("custom GitOps repository validation error, TriggerPreStage", "err", err)
return err
return nil, err
}
// custom GitOps repo url validation --> Ends

// checking vulnerability for the selected image
err = impl.checkVulnerabilityStatusAndFailWfIfNeeded(ctx, cdWf.CiArtifact, pipeline, runner, triggeredBy)
if err != nil {
impl.logger.Errorw("error, checkVulnerabilityStatusAndFailWfIfNeeded", "err", err, "runner", runner)
return err
return nil, err
}
cdStageWorkflowRequest, err := impl.buildWFRequest(runner, cdWf, pipeline, envDevploymentConfig, triggeredBy)
if err != nil {
Expand All @@ -97,19 +119,23 @@ func (impl *TriggerServiceImpl) TriggerPostStage(request bean.TriggerRequest) er
runner.Status = cdWorkflow.WorkflowFailed
runner.Message = err.Error()
_ = impl.cdWorkflowRepository.UpdateWorkFlowRunner(runner)
return err
return nil, err
}

_, err = impl.cdWorkflowService.SubmitWorkflow(cdStageWorkflowRequest)
_, jobHelmPackagePath, err := impl.cdWorkflowService.SubmitWorkflow(cdStageWorkflowRequest)
if err != nil {
impl.logger.Errorw("error in submitting workflow", "err", err, "workflowId", cdStageWorkflowRequest.WorkflowId, "pipeline", pipeline, "env", env)
return err
return nil, err
}
manifestPushTempate, err := impl.getManifestPushTemplateForPostStage(request, envDevploymentConfig, jobHelmPackagePath, cdStageWorkflowRequest, cdWf, runner, pipeline, triggeredBy, triggeredAt)
if err != nil {
impl.logger.Errorw("error in getting manifest push template", "err", err)
return nil, err
}

wfr, err := impl.cdWorkflowRepository.FindByWorkflowIdAndRunnerType(context.Background(), cdWf.Id, bean2.CD_WORKFLOW_TYPE_POST)
if err != nil {
impl.logger.Errorw("error in getting wfr by workflowId and runnerType", "err", err, "wfId", cdWf.Id)
return err
return nil, err
}
wfr.ImagePathReservationIds = pluginImagePathReservationIds
err = impl.cdWorkflowRepository.UpdateWorkFlowRunner(&wfr)
Expand All @@ -124,19 +150,19 @@ func (impl *TriggerServiceImpl) TriggerPostStage(request bean.TriggerRequest) er
if evtErr != nil {
impl.logger.Errorw("CD trigger event not sent", "error", evtErr)
}
//creating cd config history entry
// creating cd config history entry
err = impl.prePostCdScriptHistoryService.CreatePrePostCdScriptHistory(pipeline, nil, repository3.POST_CD_TYPE, true, triggeredBy, triggeredAt)
if err != nil {
impl.logger.Errorw("error in creating post cd script entry", "err", err, "pipeline", pipeline)
return err
return nil, err
}
return nil
return manifestPushTempate, nil
}

func (impl *TriggerServiceImpl) buildWfRequestErrorHandler(runner *pipelineConfig.CdWorkflowRunner, err error, triggeredBy int32) error {
func (impl *TriggerServiceImpl) buildWfRequestErrorHandler(runner *pipelineConfig.CdWorkflowRunner, err error, triggeredBy int32) (*bean4.ManifestPushTemplate, error) {
dbErr := impl.cdWorkflowCommonService.MarkCurrentDeploymentFailed(runner, err, triggeredBy)
if dbErr != nil {
impl.logger.Errorw("error while updating current runner status to failed, buildWfRequestErrorHandler", "runner", runner.Id, "err", dbErr, "releaseErr", err)
}
return err
return nil, err
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024. Devtron Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package devtronApps

import (
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
bean4 "github.com/devtron-labs/devtron/pkg/app/bean"
"github.com/devtron-labs/devtron/pkg/cluster/environment/repository"
bean5 "github.com/devtron-labs/devtron/pkg/deployment/common/bean"
"github.com/devtron-labs/devtron/pkg/deployment/trigger/devtronApps/bean"
"github.com/devtron-labs/devtron/pkg/pipeline/types"
"time"
)

func (impl *TriggerServiceImpl) checkFeasibilityForPostStage(pipeline *pipelineConfig.Pipeline, request *bean.TriggerRequest,
env *repository.Environment, cdWf *pipelineConfig.CdWorkflow, triggeredBy int32) (interface{}, error) {
//here return type is interface as ResourceFilterEvaluationAudit is not present in this version
return nil, nil
}

func (impl *TriggerServiceImpl) getManifestPushTemplateForPostStage(request bean.TriggerRequest, envDevploymentConfig *bean5.DeploymentConfig,
jobHelmPackagePath string, cdStageWorkflowRequest *types.WorkflowRequest, cdWf *pipelineConfig.CdWorkflow, runner *pipelineConfig.CdWorkflowRunner,
pipeline *pipelineConfig.Pipeline, triggeredBy int32, triggeredAt time.Time) (*bean4.ManifestPushTemplate, error) {
return nil, nil
}
Loading
Loading