diff --git a/internal/sql/repository/appWorkflow/AppWorkflowRepository.go b/internal/sql/repository/appWorkflow/AppWorkflowRepository.go index 1f076acc71..724eacdd39 100644 --- a/internal/sql/repository/appWorkflow/AppWorkflowRepository.go +++ b/internal/sql/repository/appWorkflow/AppWorkflowRepository.go @@ -54,6 +54,7 @@ type AppWorkflowRepository interface { FindByTypeAndComponentId(wfId int, componentId int, componentType string) (*AppWorkflowMapping, error) FindAllWfsHavingCdPipelinesFromSpecificEnvsOnly(envIds []int, appIds []int) ([]*AppWorkflowMapping, error) FindCiPipelineIdsFromAppWfIds(appWfIds []int) ([]int, error) + FindChildCDIdsByParentCDPipelineId(cdPipelineId int) ([]int, error) } type AppWorkflowRepositoryImpl struct { @@ -383,3 +384,10 @@ func (impl AppWorkflowRepositoryImpl) FindWFCDMappingByExternalCiId(externalCiId Select() return models, err } + +func (impl AppWorkflowRepositoryImpl) FindChildCDIdsByParentCDPipelineId(cdPipelineId int) ([]int, error) { + var ids []int + query := `select component_id from app_workflow_mapping where parent_id=? and parent_type=? and type=? and active=?;` + _, err := impl.dbConnection.Query(&ids, query, cdPipelineId, CDPIPELINE, CDPIPELINE, true) + return ids, err +} diff --git a/internal/sql/repository/pipelineConfig/PipelineRepository.go b/internal/sql/repository/pipelineConfig/PipelineRepository.go index d4136476aa..49645e3306 100644 --- a/internal/sql/repository/pipelineConfig/PipelineRepository.go +++ b/internal/sql/repository/pipelineConfig/PipelineRepository.go @@ -126,9 +126,10 @@ func (impl PipelineRepositoryImpl) GetConnection() *pg.DB { func (impl PipelineRepositoryImpl) FindByIdsIn(ids []int) ([]*Pipeline, error) { var pipelines []*Pipeline err := impl.dbConnection.Model(&pipelines). - Column("pipeline.*", "App.app_name", "Environment.environment_name"). + Column("pipeline.*", "App.app_name", "Environment.environment_name", "Environment.Cluster"). Join("inner join app a on pipeline.app_id = a.id"). Join("inner join environment e on pipeline.environment_id = e.id"). + Join("inner join cluster c on c.id = e.cluster_id"). Where("pipeline.id in (?)", pg.In(ids)). Where("pipeline.deleted = false"). Select() diff --git a/pkg/pipeline/WorkflowDagExecutor.go b/pkg/pipeline/WorkflowDagExecutor.go index bf2efd8745..9c90e5de28 100644 --- a/pkg/pipeline/WorkflowDagExecutor.go +++ b/pkg/pipeline/WorkflowDagExecutor.go @@ -99,8 +99,25 @@ type WorkflowDagExecutorImpl struct { argoUserService argo.ArgoUserService cdPipelineStatusTimelineRepo pipelineConfig.PipelineStatusTimelineRepository CiTemplateRepository pipelineConfig.CiTemplateRepository + ciWorkflowRepository pipelineConfig.CiWorkflowRepository + appLabelRepository pipelineConfig.AppLabelRepository } +const ( + CD_PIPELINE_ENV_NAME_KEY = "CD_PIPELINE_ENV_NAME" + CD_PIPELINE_CLUSTER_NAME_KEY = "CD_PIPELINE_CLUSTER_NAME" + GIT_COMMIT_HASH_PREFIX = "GIT_COMMIT_HASH" + GIT_SOURCE_TYPE_PREFIX = "GIT_SOURCE_TYPE" + GIT_SOURCE_VALUE_PREFIX = "GIT_SOURCE_VALUE" + GIT_SOURCE_COUNT = "GIT_SOURCE_COUNT" + APP_LABEL_KEY_PREFIX = "APP_LABEL_KEY" + APP_LABEL_VALUE_PREFIX = "APP_LABEL_VALUE" + APP_LABEL_COUNT = "APP_LABEL_COUNT" + CHILD_CD_ENV_NAME_PREFIX = "CHILD_CD_ENV_NAME" + CHILD_CD_CLUSTER_NAME_PREFIX = "CHILD_CD_CLUSTER_NAME" + CHILD_CD_COUNT = "CHILD_CD_COUNT" +) + type CiArtifactDTO struct { Id int `json:"id"` PipelineId int `json:"pipelineId"` //id of the ci pipeline from which this webhook was triggered @@ -145,7 +162,9 @@ func NewWorkflowDagExecutorImpl(Logger *zap.SugaredLogger, pipelineRepository pi prePostCdScriptHistoryService history2.PrePostCdScriptHistoryService, argoUserService argo.ArgoUserService, cdPipelineStatusTimelineRepo pipelineConfig.PipelineStatusTimelineRepository, - CiTemplateRepository pipelineConfig.CiTemplateRepository) *WorkflowDagExecutorImpl { + CiTemplateRepository pipelineConfig.CiTemplateRepository, + ciWorkflowRepository pipelineConfig.CiWorkflowRepository, + appLabelRepository pipelineConfig.AppLabelRepository) *WorkflowDagExecutorImpl { wde := &WorkflowDagExecutorImpl{logger: Logger, pipelineRepository: pipelineRepository, cdWorkflowRepository: cdWorkflowRepository, @@ -173,6 +192,8 @@ func NewWorkflowDagExecutorImpl(Logger *zap.SugaredLogger, pipelineRepository pi argoUserService: argoUserService, cdPipelineStatusTimelineRepo: cdPipelineStatusTimelineRepo, CiTemplateRepository: CiTemplateRepository, + ciWorkflowRepository: ciWorkflowRepository, + appLabelRepository: appLabelRepository, } err := util4.AddStream(wde.pubsubClient.JetStrCtxt, util4.ORCHESTRATOR_STREAM, util4.CI_RUNNER_STREAM) if err != nil { @@ -670,11 +691,54 @@ func (impl *WorkflowDagExecutorImpl) buildWFRequest(runner *pipelineConfig.CdWor OrchestratorToken: impl.cdConfig.OrchestratorToken, CloudProvider: impl.cdConfig.CloudProvider, } + extraEnvVariables := make(map[string]string) + env, err := impl.envRepository.FindById(cdPipeline.EnvironmentId) + if err != nil { + impl.logger.Errorw("error in getting environment by id", "err", err) + return nil, err + } + if env != nil { + extraEnvVariables[CD_PIPELINE_ENV_NAME_KEY] = env.Name + if env.Cluster != nil { + extraEnvVariables[CD_PIPELINE_CLUSTER_NAME_KEY] = env.Cluster.ClusterName + } + } + ciWf, err := impl.ciWorkflowRepository.FindLastTriggeredWorkflowByArtifactId(artifact.Id) + if err != nil && err != pg.ErrNoRows { + impl.logger.Errorw("error in getting ciWf by artifactId", "err", err, "artifactId", artifact.Id) + return nil, err + } + + if ciWf != nil && ciWf.GitTriggers != nil { + i := 1 + for _, gitTrigger := range ciWf.GitTriggers { + extraEnvVariables[fmt.Sprintf("%s_%d", GIT_COMMIT_HASH_PREFIX, i)] = gitTrigger.Commit + extraEnvVariables[fmt.Sprintf("%s_%d", GIT_SOURCE_TYPE_PREFIX, i)] = string(gitTrigger.CiConfigureSourceType) + extraEnvVariables[fmt.Sprintf("%s_%d", GIT_SOURCE_VALUE_PREFIX, i)] = gitTrigger.CiConfigureSourceValue + i++ + } + extraEnvVariables[GIT_SOURCE_COUNT] = strconv.Itoa(len(ciWf.GitTriggers)) + } + childCdIds, err := impl.appWorkflowRepository.FindChildCDIdsByParentCDPipelineId(cdPipeline.Id) + if err != nil && err != pg.ErrNoRows { + impl.logger.Errorw("error in getting child cdPipelineIds by parent cdPipelineId", "err", err, "parent cdPipelineId", cdPipeline.Id) + return nil, err + } + if len(childCdIds) > 0 { + childPipelines, err := impl.pipelineRepository.FindByIdsIn(childCdIds) + if err != nil { + impl.logger.Errorw("error in getting pipelines by ids", "err", err, "ids", childCdIds) + return nil, err + } + for i, childPipeline := range childPipelines { + extraEnvVariables[fmt.Sprintf("%s_%d", CHILD_CD_ENV_NAME_PREFIX, i+1)] = childPipeline.Environment.Name + extraEnvVariables[fmt.Sprintf("%s_%d", CHILD_CD_CLUSTER_NAME_PREFIX, i+1)] = childPipeline.Environment.Cluster.ClusterName + } + extraEnvVariables[CHILD_CD_COUNT] = strconv.Itoa(len(childPipelines)) + } if ciPipeline != nil && ciPipeline.Id > 0 { - extraEnvVariables := make(map[string]string) extraEnvVariables["APP_NAME"] = ciPipeline.App.AppName - cdStageWorkflowRequest.ExtraEnvironmentVariables = extraEnvVariables cdStageWorkflowRequest.DockerUsername = ciPipeline.CiTemplate.DockerRegistry.Username cdStageWorkflowRequest.DockerPassword = ciPipeline.CiTemplate.DockerRegistry.Password cdStageWorkflowRequest.AwsRegion = ciPipeline.CiTemplate.DockerRegistry.AWSRegion @@ -689,9 +753,7 @@ func (impl *WorkflowDagExecutorImpl) buildWFRequest(runner *pipelineConfig.CdWor if err != nil { return nil, err } - extraEnvVariables := make(map[string]string) extraEnvVariables["APP_NAME"] = ciTemplate.App.AppName - cdStageWorkflowRequest.ExtraEnvironmentVariables = extraEnvVariables cdStageWorkflowRequest.DockerUsername = ciTemplate.DockerRegistry.Username cdStageWorkflowRequest.DockerPassword = ciTemplate.DockerRegistry.Password cdStageWorkflowRequest.AwsRegion = ciTemplate.DockerRegistry.AWSRegion @@ -701,8 +763,20 @@ func (impl *WorkflowDagExecutorImpl) buildWFRequest(runner *pipelineConfig.CdWor cdStageWorkflowRequest.SecretKey = ciTemplate.DockerRegistry.AWSSecretAccessKey cdStageWorkflowRequest.DockerRegistryType = string(ciTemplate.DockerRegistry.RegistryType) cdStageWorkflowRequest.DockerRegistryURL = ciTemplate.DockerRegistry.RegistryURL + appLabels, err := impl.appLabelRepository.FindAllByAppId(cdPipeline.AppId) + if err != nil && err != pg.ErrNoRows { + impl.logger.Errorw("error in getting labels by appId", "err", err, "appId", cdPipeline.AppId) + return nil, err + } + for i, appLabel := range appLabels { + extraEnvVariables[fmt.Sprintf("%s_%d", APP_LABEL_KEY_PREFIX, i+1)] = appLabel.Key + extraEnvVariables[fmt.Sprintf("%s_%d", APP_LABEL_VALUE_PREFIX, i+1)] = appLabel.Value + } + if len(appLabels) > 0 { + extraEnvVariables[APP_LABEL_COUNT] = strconv.Itoa(len(appLabels)) + } } - + cdStageWorkflowRequest.ExtraEnvironmentVariables = extraEnvVariables if deployStageTriggeredByUser != nil { cdStageWorkflowRequest.DeploymentTriggerTime = deployStageWfr.StartedOn cdStageWorkflowRequest.DeploymentTriggeredBy = deployStageTriggeredByUser.EmailId diff --git a/wire_gen.go b/wire_gen.go index 46bef51b1a..a789cf3076 100644 --- a/wire_gen.go +++ b/wire_gen.go @@ -1,7 +1,8 @@ // Code generated by Wire. DO NOT EDIT. //go:generate go run github.com/google/wire/cmd/wire -//+build !wireinject +//go:build !wireinject +// +build !wireinject package main @@ -330,7 +331,7 @@ func InitializeApp() (*App, error) { prePostCdScriptHistoryRepositoryImpl := repository6.NewPrePostCdScriptHistoryRepositoryImpl(sugaredLogger, db) prePostCdScriptHistoryServiceImpl := history.NewPrePostCdScriptHistoryServiceImpl(sugaredLogger, prePostCdScriptHistoryRepositoryImpl, configMapRepositoryImpl, configMapHistoryServiceImpl) ciTemplateRepositoryImpl := pipelineConfig.NewCiTemplateRepositoryImpl(db, sugaredLogger) - workflowDagExecutorImpl := pipeline.NewWorkflowDagExecutorImpl(sugaredLogger, pipelineRepositoryImpl, cdWorkflowRepositoryImpl, pubSubClient, appServiceImpl, cdWorkflowServiceImpl, cdConfig, ciArtifactRepositoryImpl, ciPipelineRepositoryImpl, materialRepositoryImpl, pipelineOverrideRepositoryImpl, userServiceImpl, deploymentGroupRepositoryImpl, environmentRepositoryImpl, enforcerImpl, enforcerUtilImpl, tokenCache, acdAuthConfig, eventSimpleFactoryImpl, eventRESTClientImpl, cvePolicyRepositoryImpl, imageScanResultRepositoryImpl, appWorkflowRepositoryImpl, prePostCdScriptHistoryServiceImpl, argoUserServiceImpl, pipelineStatusTimelineRepositoryImpl, ciTemplateRepositoryImpl) + workflowDagExecutorImpl := pipeline.NewWorkflowDagExecutorImpl(sugaredLogger, pipelineRepositoryImpl, cdWorkflowRepositoryImpl, pubSubClient, appServiceImpl, cdWorkflowServiceImpl, cdConfig, ciArtifactRepositoryImpl, ciPipelineRepositoryImpl, materialRepositoryImpl, pipelineOverrideRepositoryImpl, userServiceImpl, deploymentGroupRepositoryImpl, environmentRepositoryImpl, enforcerImpl, enforcerUtilImpl, tokenCache, acdAuthConfig, eventSimpleFactoryImpl, eventRESTClientImpl, cvePolicyRepositoryImpl, imageScanResultRepositoryImpl, appWorkflowRepositoryImpl, prePostCdScriptHistoryServiceImpl, argoUserServiceImpl, pipelineStatusTimelineRepositoryImpl, ciTemplateRepositoryImpl, ciWorkflowRepositoryImpl, appLabelRepositoryImpl) deploymentGroupAppRepositoryImpl := repository.NewDeploymentGroupAppRepositoryImpl(sugaredLogger, db) deploymentGroupServiceImpl := deploymentGroup.NewDeploymentGroupServiceImpl(appRepositoryImpl, sugaredLogger, pipelineRepositoryImpl, ciPipelineRepositoryImpl, deploymentGroupRepositoryImpl, environmentRepositoryImpl, deploymentGroupAppRepositoryImpl, ciArtifactRepositoryImpl, appWorkflowRepositoryImpl, workflowDagExecutorImpl) deploymentConfigServiceImpl := pipeline.NewDeploymentConfigServiceImpl(sugaredLogger, envConfigOverrideRepositoryImpl, chartRepositoryImpl, pipelineRepositoryImpl, envLevelAppMetricsRepositoryImpl, appLevelMetricsRepositoryImpl, pipelineConfigRepositoryImpl, configMapRepositoryImpl, configMapHistoryServiceImpl, chartRefRepositoryImpl)