@@ -20,6 +20,7 @@ import (
2020 "github.com/devtron-labs/devtron/pkg/pipeline"
2121 "github.com/devtron-labs/devtron/pkg/pipeline/adapter"
2222 "github.com/devtron-labs/devtron/pkg/pipeline/bean"
23+ "github.com/devtron-labs/devtron/pkg/pipeline/history"
2324 repository3 "github.com/devtron-labs/devtron/pkg/pipeline/history/repository"
2425 "github.com/devtron-labs/devtron/pkg/resourceQualifiers"
2526 "github.com/devtron-labs/devtron/pkg/variables"
@@ -52,6 +53,8 @@ type DeploymentConfigurationServiceImpl struct {
5253 deploymentConfigService pipeline.PipelineDeploymentConfigService
5354 chartRefService chartRef.ChartRefService
5455 pipelineRepository pipelineConfig.PipelineRepository
56+ deploymentTemplateHistoryService history.DeploymentTemplateHistoryService
57+ configMapHistoryService history.ConfigMapHistoryService
5558}
5659
5760func NewDeploymentConfigurationServiceImpl (logger * zap.SugaredLogger ,
@@ -68,6 +71,8 @@ func NewDeploymentConfigurationServiceImpl(logger *zap.SugaredLogger,
6871 deploymentConfigService pipeline.PipelineDeploymentConfigService ,
6972 chartRefService chartRef.ChartRefService ,
7073 pipelineRepository pipelineConfig.PipelineRepository ,
74+ deploymentTemplateHistoryService history.DeploymentTemplateHistoryService ,
75+ configMapHistoryService history.ConfigMapHistoryService ,
7176) (* DeploymentConfigurationServiceImpl , error ) {
7277 deploymentConfigurationService := & DeploymentConfigurationServiceImpl {
7378 logger : logger ,
@@ -84,6 +89,8 @@ func NewDeploymentConfigurationServiceImpl(logger *zap.SugaredLogger,
8489 deploymentConfigService : deploymentConfigService ,
8590 chartRefService : chartRefService ,
8691 pipelineRepository : pipelineRepository ,
92+ deploymentTemplateHistoryService : deploymentTemplateHistoryService ,
93+ configMapHistoryService : configMapHistoryService ,
8794 }
8895
8996 return deploymentConfigurationService , nil
@@ -268,7 +275,7 @@ func (impl *DeploymentConfigurationServiceImpl) getCmCsConfigHistory(ctx context
268275 return nil , err
269276 }
270277 var configData []* bean.ConfigData
271- configList := pipeline .ConfigsList {}
278+ configList := bean .ConfigsList {}
272279 secretList := bean.SecretsList {}
273280 switch configType {
274281 case repository3 .CONFIGMAP_TYPE :
@@ -360,11 +367,123 @@ func (impl *DeploymentConfigurationServiceImpl) encodeSecretDataFromNonAdminUser
360367 }
361368}
362369
370+ func (impl * DeploymentConfigurationServiceImpl ) getCmCsDataForPreviousDeployments (ctx context.Context , deploymentTemplateHistoryId , pipelineId int , userHasAdminAccess bool ) (* bean2.DeploymentAndCmCsConfigDto , error ) {
371+
372+ configDataDto := & bean2.DeploymentAndCmCsConfigDto {}
373+
374+ deplTemplateHistory , err := impl .deploymentTemplateHistoryService .GetTemplateHistoryModelForDeployedTemplateById (deploymentTemplateHistoryId , pipelineId )
375+ if err != nil {
376+ impl .logger .Errorw ("error in getting deployment template history" , "err" , err , "deploymentTemplateHistoryId" , deploymentTemplateHistoryId , "pipelineId" , pipelineId )
377+ return nil , err
378+ }
379+
380+ secretConfigData , cmConfigData , err := impl .configMapHistoryService .GetConfigmapHistoryDataByDeployedOnAndPipelineId (ctx , pipelineId , deplTemplateHistory .DeployedOn , userHasAdminAccess )
381+ if err != nil {
382+ impl .logger .Errorw ("error in getting secretData and cmData" , "err" , err , "deploymentTemplateHistoryId" , deploymentTemplateHistoryId , "pipelineId" , pipelineId )
383+ return nil , err
384+ }
385+ configDataDto .WithConfigMapData (cmConfigData ).WithSecretData (secretConfigData )
386+ return configDataDto , nil
387+
388+ }
389+ func (impl * DeploymentConfigurationServiceImpl ) getPipelineStrategyForPreviousDeployments (ctx context.Context , deploymentTemplateHistoryId , pipelineId int ) (* bean2.DeploymentAndCmCsConfig , error ) {
390+ pipelineStrategyJson := json.RawMessage {}
391+ pipelineConfig := bean2 .NewDeploymentAndCmCsConfig ()
392+ deplTemplateHistory , err := impl .deploymentTemplateHistoryService .GetTemplateHistoryModelForDeployedTemplateById (deploymentTemplateHistoryId , pipelineId )
393+ if err != nil {
394+ impl .logger .Errorw ("error in getting deployment template history" , "deploymentTemplateHistoryId" , deploymentTemplateHistoryId , "pipelineId" , pipelineId , "err" , err )
395+ return nil , err
396+ }
397+ pipelineStrategyHistory , err := impl .pipelineStrategyHistoryRepository .FindPipelineStrategyForDeployedOnAndPipelineId (pipelineId , deplTemplateHistory .DeployedOn )
398+ if err != nil && ! util .IsErrNoRows (err ) {
399+ impl .logger .Errorw ("error in FindPipelineStrategyForDeployedOnAndPipelineId" , "deploymentTemplateHistoryId" , deploymentTemplateHistoryId , "deployedOn" , deplTemplateHistory .DeployedOn , "pipelineId" , pipelineId , "err" , err )
400+ return nil , err
401+ } else if util .IsErrNoRows (err ) {
402+ return pipelineConfig , nil
403+ }
404+ err = pipelineStrategyJson .UnmarshalJSON ([]byte (pipelineStrategyHistory .Config ))
405+ if err != nil {
406+ impl .logger .Errorw ("getDeploymentTemplateForEnvLevel, error in unmarshalling string pipelineStrategyHistory data into json Raw message" , "err" , err )
407+ return nil , err
408+ }
409+ pipelineConfig .WithConfigData (pipelineStrategyJson ).
410+ WithResourceType (bean .PipelineStrategy ).
411+ WithPipelineStrategyMetadata (pipelineStrategyHistory .PipelineTriggerType , string (pipelineStrategyHistory .Strategy ))
412+ return pipelineConfig , nil
413+ }
414+
415+ func (impl * DeploymentConfigurationServiceImpl ) getDeploymentsConfigForPreviousDeployments (ctx context.Context , configDataQueryParams * bean2.ConfigDataQueryParams ,
416+ appId , envId int ) (generateManifest.DeploymentTemplateResponse , error ) {
417+ deploymentTemplateRequest := generateManifest.DeploymentTemplateRequest {
418+ PipelineId : configDataQueryParams .PipelineId ,
419+ DeploymentTemplateHistoryId : configDataQueryParams .IdentifierId ,
420+ RequestDataMode : generateManifest .Values ,
421+ Type : repository2 .DeployedOnSelfEnvironment ,
422+ }
423+ var deploymentTemplateResponse generateManifest.DeploymentTemplateResponse
424+ deploymentTemplateResponse , err := impl .deploymentTemplateService .GetDeploymentTemplate (ctx , deploymentTemplateRequest )
425+ if err != nil {
426+ impl .logger .Errorw ("getDeploymentTemplateForEnvLevel, error in getting deployment template for " , "deploymentTemplateRequest" , deploymentTemplateRequest , "err" , err )
427+ return deploymentTemplateResponse , err
428+ }
429+
430+ return deploymentTemplateResponse , nil
431+ }
432+
433+ func (impl * DeploymentConfigurationServiceImpl ) getDeploymentAndCmCsConfigDataForPreviousDeployments (ctx context.Context , configDataQueryParams * bean2.ConfigDataQueryParams ,
434+ appId , envId int , userHasAdminAccess bool ) (* bean2.DeploymentAndCmCsConfigDto , error ) {
435+
436+ // getting DeploymentAndCmCsConfigDto obj with cm and cs data populated
437+ configDataDto , err := impl .getCmCsDataForPreviousDeployments (ctx , configDataQueryParams .IdentifierId , configDataQueryParams .PipelineId , userHasAdminAccess )
438+ if err != nil {
439+ impl .logger .Errorw ("error in getting cm cs for PreviousDeployments state" , "deploymentTemplateHistoryId" , configDataQueryParams .IdentifierId , "pipelineId" , configDataQueryParams .PipelineId , "err" , err )
440+ return nil , err
441+ }
442+ pipelineStrategy , err := impl .getPipelineStrategyForPreviousDeployments (ctx , configDataQueryParams .IdentifierId , configDataQueryParams .PipelineId )
443+ if err != nil {
444+ impl .logger .Errorw (" error in getting cm cs for PreviousDeployments state" , "deploymentTemplateHistoryId" , configDataQueryParams .IdentifierId , "pipelineId" , configDataQueryParams .PipelineId , "err" , err )
445+ return nil , err
446+ }
447+ if len (pipelineStrategy .Data ) > 0 {
448+ configDataDto .WithPipelineConfigData (pipelineStrategy )
449+ }
450+
451+ deploymentTemplateData , err := impl .getDeploymentsConfigForPreviousDeployments (ctx , configDataQueryParams , appId , envId )
452+ if err != nil {
453+ impl .logger .Errorw ("error in getting deployment config" , "appName" , configDataQueryParams .AppName , "envName" , configDataQueryParams .EnvName , "err" , err )
454+ return nil , err
455+ }
456+ deploymentJson := json.RawMessage {}
457+ err = deploymentJson .UnmarshalJSON ([]byte (deploymentTemplateData .Data ))
458+ if err != nil {
459+ impl .logger .Errorw ("error in unmarshalling string deploymentTemplateResponse data into json Raw message" , "appName" , configDataQueryParams .AppName , "envName" , configDataQueryParams .EnvName , "err" , err )
460+ return nil , err
461+ }
462+ variableSnapShotMap := map [string ]map [string ]string {bean .DeploymentTemplate .ToString (): deploymentTemplateData .VariableSnapshot }
463+
464+ deploymentConfig := bean2 .NewDeploymentAndCmCsConfig ().
465+ WithDeploymentConfigMetadata (deploymentTemplateData .TemplateVersion , deploymentTemplateData .IsAppMetricsEnabled ).
466+ WithConfigData (deploymentJson ).
467+ WithResourceType (bean .DeploymentTemplate ).
468+ WithResolvedValue (json .RawMessage (deploymentTemplateData .ResolvedData )).
469+ WithVariableSnapshot (variableSnapShotMap )
470+
471+ configDataDto .WithDeploymentTemplateData (deploymentConfig )
472+
473+ return configDataDto , nil
474+ }
475+
363476func (impl * DeploymentConfigurationServiceImpl ) getConfigDataForAppConfiguration (ctx context.Context , configDataQueryParams * bean2.ConfigDataQueryParams ,
364477 appId , envId , clusterId int , userHasAdminAccess bool , systemMetadata * resourceQualifiers.SystemMetadata ) (* bean2.DeploymentAndCmCsConfigDto , error ) {
365478 configDataDto := & bean2.DeploymentAndCmCsConfigDto {}
366479 var err error
367480 switch configDataQueryParams .ConfigType {
481+ case bean2 .PreviousDeployments .ToString ():
482+ configDataDto , err = impl .getDeploymentAndCmCsConfigDataForPreviousDeployments (ctx , configDataQueryParams , appId , envId , userHasAdminAccess )
483+ if err != nil {
484+ impl .logger .Errorw ("GetAllConfigData, error in config data for Previous Deployments" , "configDataQueryParams" , configDataQueryParams , "err" , err )
485+ return nil , err
486+ }
368487 default : // keeping default as PublishedOnly
369488 configDataDto , err = impl .getPublishedConfigData (ctx , configDataQueryParams , appId , envId , clusterId , userHasAdminAccess , systemMetadata )
370489 if err != nil {
0 commit comments