@@ -39,6 +39,7 @@ import (
3939 repository3 "github.com/devtron-labs/devtron/pkg/pipeline/history/repository"
4040 "github.com/devtron-labs/devtron/pkg/pipeline/types"
4141 "github.com/devtron-labs/devtron/pkg/plugin"
42+ bean3 "github.com/devtron-labs/devtron/pkg/plugin/bean"
4243 "github.com/devtron-labs/devtron/pkg/resourceQualifiers"
4344 "github.com/devtron-labs/devtron/pkg/sql"
4445 util3 "github.com/devtron-labs/devtron/pkg/util"
@@ -110,7 +111,7 @@ func (impl *TriggerServiceImpl) TriggerPreStage(request bean.TriggerRequest) err
110111 }
111112 cdStageWorkflowRequest .StageType = types .PRE
112113 // handling copyContainerImage plugin specific logic
113- imagePathReservationIds , err := impl .SetCopyContainerImagePluginDataInWorkflowRequest (cdStageWorkflowRequest , pipeline .Id , types .PRE , artifact )
114+ imagePathReservationIds , err := impl .setCopyContainerImagePluginDataAndReserveImages (cdStageWorkflowRequest , pipeline .Id , types .PRE , artifact )
114115 if err != nil {
115116 runner .Status = pipelineConfig .WorkflowFailed
116117 runner .Message = err .Error ()
@@ -236,95 +237,121 @@ func (impl *TriggerServiceImpl) checkVulnerabilityStatusAndFailWfIfNeeded(ctx co
236237 return nil
237238}
238239
239- func (impl * TriggerServiceImpl ) SetCopyContainerImagePluginDataInWorkflowRequest (cdStageWorkflowRequest * types.WorkflowRequest , pipelineId int , pipelineStage string , artifact * repository.CiArtifact ) ([]int , error ) {
240- copyContainerImagePluginId , err := impl .globalPluginService .GetRefPluginIdByRefPluginName (pipeline .COPY_CONTAINER_IMAGE )
241- var imagePathReservationIds []int
240+ // setCopyContainerImagePluginDataAndReserveImages sets required fields in cdStageWorkflowRequest and reserve images generated by plugin
241+ func (impl * TriggerServiceImpl ) setCopyContainerImagePluginDataAndReserveImages (cdStageWorkflowRequest * types.WorkflowRequest , pipelineId int , pipelineStage string , artifact * repository.CiArtifact ) ([]int , error ) {
242+
243+ copyContainerImagePluginDetail , err := impl .globalPluginService .GetRefPluginIdByRefPluginName (pipeline .COPY_CONTAINER_IMAGE )
242244 if err != nil && err != pg .ErrNoRows {
243245 impl .logger .Errorw ("error in getting copyContainerImage plugin id" , "err" , err )
244- return imagePathReservationIds , err
246+ return nil , err
245247 }
246- for _ , step := range cdStageWorkflowRequest .PrePostDeploySteps {
247- if copyContainerImagePluginId != 0 && step .RefPluginId == copyContainerImagePluginId {
248- var pipelineStageEntityType int
249- if pipelineStage == types .PRE {
250- pipelineStageEntityType = pipelineConfigBean .EntityTypePreCD
251- } else {
252- pipelineStageEntityType = pipelineConfigBean .EntityTypePostCD
253- }
254- customTagId := - 1
255- var DockerImageTag string
256248
257- customTag , err := impl .customTagService .GetActiveCustomTagByEntityKeyAndValue (pipelineStageEntityType , strconv .Itoa (pipelineId ))
258- if err != nil && err != pg .ErrNoRows {
259- impl .logger .Errorw ("error in fetching custom tag data" , "err" , err )
260- return imagePathReservationIds , err
261- }
249+ pluginIdToVersionMap := make (map [int ]string )
250+ for _ , p := range copyContainerImagePluginDetail {
251+ pluginIdToVersionMap [p .Id ] = p .Version
252+ }
262253
263- if ! customTag .Enabled {
264- // case when custom tag is not configured - source image tag will be taken as docker image tag
265- pluginTriggerImageSplit := strings .Split (artifact .Image , ":" )
266- DockerImageTag = pluginTriggerImageSplit [len (pluginTriggerImageSplit )- 1 ]
267- } else {
268- // for copyContainerImage plugin parse destination images and save its data in image path reservation table
269- customTagDbObject , customDockerImageTag , err := impl .customTagService .GetCustomTag (pipelineStageEntityType , strconv .Itoa (pipelineId ))
270- if err != nil && err != pg .ErrNoRows {
271- impl .logger .Errorw ("error in fetching custom tag by entity key and value for CD" , "err" , err )
272- return imagePathReservationIds , err
273- }
274- if customTagDbObject != nil && customTagDbObject .Id > 0 {
275- customTagId = customTagDbObject .Id
276- }
277- DockerImageTag = customDockerImageTag
278- }
254+ dockerImageTag , customTagId , err := impl .getDockerTagAndCustomTagIdForPlugin (pipelineStage , pipelineId , artifact )
255+ if err != nil {
256+ impl .logger .Errorw ("error in getting docker tag" , "err" , err )
257+ return nil , err
258+ }
279259
280- var sourceDockerRegistryId string
281- if artifact .DataSource == repository .PRE_CD || artifact .DataSource == repository .POST_CD || artifact .DataSource == repository .POST_CI {
282- if artifact .CredentialsSourceType == repository .GLOBAL_CONTAINER_REGISTRY {
283- sourceDockerRegistryId = artifact .CredentialSourceValue
284- }
285- } else {
286- sourceDockerRegistryId = cdStageWorkflowRequest .DockerRegistryId
287- }
288- registryDestinationImageMap , registryCredentialMap , err := impl .pluginInputVariableParser .HandleCopyContainerImagePluginInputVariables (step .InputVars , DockerImageTag , cdStageWorkflowRequest .CiArtifactDTO .Image , sourceDockerRegistryId )
260+ var sourceDockerRegistryId string
261+ if artifact .DataSource == repository .PRE_CD || artifact .DataSource == repository .POST_CD || artifact .DataSource == repository .POST_CI {
262+ if artifact .CredentialsSourceType == repository .GLOBAL_CONTAINER_REGISTRY {
263+ sourceDockerRegistryId = artifact .CredentialSourceValue
264+ }
265+ } else {
266+ sourceDockerRegistryId = cdStageWorkflowRequest .DockerRegistryId
267+ }
268+
269+ registryCredentialMap := make (map [string ]bean3.RegistryCredentials )
270+ var allDestinationImages []string //saving all images to be reserved in this array
271+
272+ for _ , step := range cdStageWorkflowRequest .PrePostDeploySteps {
273+ if version , ok := pluginIdToVersionMap [step .RefPluginId ]; ok {
274+ registryDestinationImageMap , credentialMap , err := impl .pluginInputVariableParser .HandleCopyContainerImagePluginInputVariables (step .InputVars , dockerImageTag , cdStageWorkflowRequest .CiArtifactDTO .Image , sourceDockerRegistryId )
289275 if err != nil {
290276 impl .logger .Errorw ("error in parsing copyContainerImage input variable" , "err" , err )
291- return imagePathReservationIds , err
292- }
293- var destinationImages []string
294- for _ , images := range registryDestinationImageMap {
295- for _ , image := range images {
296- destinationImages = append (destinationImages , image )
297- }
298- }
299- // fetch already saved artifacts to check if they are already present
300- savedCIArtifacts , err := impl .ciArtifactRepository .FindCiArtifactByImagePaths (destinationImages )
301- if err != nil {
302- impl .logger .Errorw ("error in fetching artifacts by image path" , "err" , err )
303- return imagePathReservationIds , err
277+ return nil , err
304278 }
305- if len ( savedCIArtifacts ) > 0 {
306- // if already present in ci artifact, return "image path already in use error"
307- return imagePathReservationIds , pipelineConfigBean . ErrImagePathInUse
279+ if version == pipeline . COPY_CONTAINER_IMAGE_VERSION_V1 {
280+ // this is needed in ci runner only for v1
281+ cdStageWorkflowRequest . RegistryDestinationImageMap = registryDestinationImageMap
308282 }
309- imagePathReservationIds , err = impl .ReserveImagesGeneratedAtPlugin (customTagId , registryDestinationImageMap )
310- if err != nil {
311- impl .logger .Errorw ("error in reserving image" , "err" , err )
312- return imagePathReservationIds , err
283+ for _ , images := range registryDestinationImageMap {
284+ allDestinationImages = append (allDestinationImages , images ... )
313285 }
314- cdStageWorkflowRequest .RegistryDestinationImageMap = registryDestinationImageMap
315- cdStageWorkflowRequest .RegistryCredentialMap = registryCredentialMap
316- var pluginArtifactStage string
317- if pipelineStage == types .PRE {
318- pluginArtifactStage = repository .PRE_CD
319- } else {
320- pluginArtifactStage = repository .POST_CD
286+ for k , v := range credentialMap {
287+ registryCredentialMap [k ] = v
321288 }
322- cdStageWorkflowRequest .PluginArtifactStage = pluginArtifactStage
323289 }
324290 }
291+
292+ // set data in cdStageWorkflowRequest needed for copy container image plugin
293+
294+ cdStageWorkflowRequest .RegistryCredentialMap = registryCredentialMap
295+ cdStageWorkflowRequest .DockerImageTag = dockerImageTag
296+ if pipelineStage == types .PRE {
297+ cdStageWorkflowRequest .PluginArtifactStage = repository .PRE_CD
298+ } else {
299+ cdStageWorkflowRequest .PluginArtifactStage = repository .POST_CD
300+ }
301+
302+ // fetch already saved artifacts to check if they are already present
303+
304+ savedCIArtifacts , err := impl .ciArtifactRepository .FindCiArtifactByImagePaths (allDestinationImages )
305+ if err != nil {
306+ impl .logger .Errorw ("error in fetching artifacts by image path" , "err" , err )
307+ return nil , err
308+ }
309+ if len (savedCIArtifacts ) > 0 {
310+ // if already present in ci artifact, return "image path already in use error"
311+ return nil , pipelineConfigBean .ErrImagePathInUse
312+ }
313+ // reserve all images where data will be
314+ imagePathReservationIds , err := impl .ReserveImagesGeneratedAtPlugin (customTagId , allDestinationImages )
315+ if err != nil {
316+ impl .logger .Errorw ("error in reserving image" , "err" , err )
317+ return imagePathReservationIds , err
318+ }
325319 return imagePathReservationIds , nil
326320}
327321
322+ func (impl * TriggerServiceImpl ) getDockerTagAndCustomTagIdForPlugin (pipelineStage string , pipelineId int , artifact * repository.CiArtifact ) (string , int , error ) {
323+ var pipelineStageEntityType int
324+ if pipelineStage == types .PRE {
325+ pipelineStageEntityType = pipelineConfigBean .EntityTypePreCD
326+ } else {
327+ pipelineStageEntityType = pipelineConfigBean .EntityTypePostCD
328+ }
329+ customTag , err := impl .customTagService .GetActiveCustomTagByEntityKeyAndValue (pipelineStageEntityType , strconv .Itoa (pipelineId ))
330+ if err != nil && err != pg .ErrNoRows {
331+ impl .logger .Errorw ("error in fetching custom tag data" , "err" , err )
332+ return "" , 0 , err
333+ }
334+ var DockerImageTag string
335+ customTagId := - 1 // if customTag is not configured id=-1 will be saved in image_path_reservation table for image reservation
336+ if ! customTag .Enabled {
337+ // case when custom tag is not configured - source image tag will be taken as docker image tag
338+ pluginTriggerImageSplit := strings .Split (artifact .Image , ":" )
339+ DockerImageTag = pluginTriggerImageSplit [len (pluginTriggerImageSplit )- 1 ]
340+ } else {
341+ // for copyContainerImage plugin parse destination images and save its data in image path reservation table
342+ customTagDbObject , customDockerImageTag , err := impl .customTagService .GetCustomTag (pipelineStageEntityType , strconv .Itoa (pipelineId ))
343+ if err != nil && err != pg .ErrNoRows {
344+ impl .logger .Errorw ("error in fetching custom tag by entity key and value for CD" , "err" , err )
345+ return "" , 0 , err
346+ }
347+ if customTagDbObject != nil && customTagDbObject .Id > 0 {
348+ customTagId = customTagDbObject .Id
349+ }
350+ DockerImageTag = customDockerImageTag
351+ }
352+ return DockerImageTag , customTagId , nil
353+ }
354+
328355func (impl * TriggerServiceImpl ) buildWFRequest (runner * pipelineConfig.CdWorkflowRunner , cdWf * pipelineConfig.CdWorkflow , cdPipeline * pipelineConfig.Pipeline , envDeploymentConfig * bean5.DeploymentConfig , triggeredBy int32 ) (* types.WorkflowRequest , error ) {
329356 if cdPipeline .App .Id == 0 {
330357 appModel , err := impl .appRepository .FindById (cdPipeline .AppId )
@@ -843,20 +870,20 @@ func (impl *TriggerServiceImpl) getSourceCiPipelineForArtifact(ciPipeline pipeli
843870 return sourceCiPipeline , nil
844871}
845872
846- func (impl * TriggerServiceImpl ) ReserveImagesGeneratedAtPlugin (customTagId int , registryImageMap map [ string ] []string ) ([]int , error ) {
873+ func (impl * TriggerServiceImpl ) ReserveImagesGeneratedAtPlugin (customTagId int , destinationImages []string ) ([]int , error ) {
847874 var imagePathReservationIds []int
848- for _ , images := range registryImageMap {
849- for _ , image := range images {
850- imagePathReservationData , err := impl .customTagService .ReserveImagePath (image , customTagId )
851- if err != nil {
852- impl .logger .Errorw ("Error in marking custom tag reserved" , "err" , err )
853- return imagePathReservationIds , err
854- }
855- if imagePathReservationData != nil {
856- imagePathReservationIds = append (imagePathReservationIds , imagePathReservationData .Id )
857- }
875+
876+ for _ , image := range destinationImages {
877+ imagePathReservationData , err := impl .customTagService .ReserveImagePath (image , customTagId )
878+ if err != nil {
879+ impl .logger .Errorw ("Error in marking custom tag reserved" , "err" , err )
880+ return imagePathReservationIds , err
881+ }
882+ if imagePathReservationData != nil {
883+ imagePathReservationIds = append (imagePathReservationIds , imagePathReservationData .Id )
858884 }
859885 }
886+
860887 return imagePathReservationIds , nil
861888}
862889
0 commit comments