@@ -20,6 +20,7 @@ import (
20
20
"github.com/devtron-labs/devtron/pkg/pipeline"
21
21
"github.com/devtron-labs/devtron/pkg/pipeline/adapter"
22
22
"github.com/devtron-labs/devtron/pkg/pipeline/bean"
23
+ "github.com/devtron-labs/devtron/pkg/pipeline/history"
23
24
repository3 "github.com/devtron-labs/devtron/pkg/pipeline/history/repository"
24
25
"github.com/devtron-labs/devtron/pkg/resourceQualifiers"
25
26
"github.com/devtron-labs/devtron/pkg/variables"
@@ -52,6 +53,8 @@ type DeploymentConfigurationServiceImpl struct {
52
53
deploymentConfigService pipeline.PipelineDeploymentConfigService
53
54
chartRefService chartRef.ChartRefService
54
55
pipelineRepository pipelineConfig.PipelineRepository
56
+ deploymentTemplateHistoryService history.DeploymentTemplateHistoryService
57
+ configMapHistoryService history.ConfigMapHistoryService
55
58
}
56
59
57
60
func NewDeploymentConfigurationServiceImpl (logger * zap.SugaredLogger ,
@@ -68,6 +71,8 @@ func NewDeploymentConfigurationServiceImpl(logger *zap.SugaredLogger,
68
71
deploymentConfigService pipeline.PipelineDeploymentConfigService ,
69
72
chartRefService chartRef.ChartRefService ,
70
73
pipelineRepository pipelineConfig.PipelineRepository ,
74
+ deploymentTemplateHistoryService history.DeploymentTemplateHistoryService ,
75
+ configMapHistoryService history.ConfigMapHistoryService ,
71
76
) (* DeploymentConfigurationServiceImpl , error ) {
72
77
deploymentConfigurationService := & DeploymentConfigurationServiceImpl {
73
78
logger : logger ,
@@ -84,6 +89,8 @@ func NewDeploymentConfigurationServiceImpl(logger *zap.SugaredLogger,
84
89
deploymentConfigService : deploymentConfigService ,
85
90
chartRefService : chartRefService ,
86
91
pipelineRepository : pipelineRepository ,
92
+ deploymentTemplateHistoryService : deploymentTemplateHistoryService ,
93
+ configMapHistoryService : configMapHistoryService ,
87
94
}
88
95
89
96
return deploymentConfigurationService , nil
@@ -268,7 +275,7 @@ func (impl *DeploymentConfigurationServiceImpl) getCmCsConfigHistory(ctx context
268
275
return nil , err
269
276
}
270
277
var configData []* bean.ConfigData
271
- configList := pipeline .ConfigsList {}
278
+ configList := bean .ConfigsList {}
272
279
secretList := bean.SecretsList {}
273
280
switch configType {
274
281
case repository3 .CONFIGMAP_TYPE :
@@ -360,11 +367,123 @@ func (impl *DeploymentConfigurationServiceImpl) encodeSecretDataFromNonAdminUser
360
367
}
361
368
}
362
369
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
+
363
476
func (impl * DeploymentConfigurationServiceImpl ) getConfigDataForAppConfiguration (ctx context.Context , configDataQueryParams * bean2.ConfigDataQueryParams ,
364
477
appId , envId , clusterId int , userHasAdminAccess bool , systemMetadata * resourceQualifiers.SystemMetadata ) (* bean2.DeploymentAndCmCsConfigDto , error ) {
365
478
configDataDto := & bean2.DeploymentAndCmCsConfigDto {}
366
479
var err error
367
480
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
+ }
368
487
default : // keeping default as PublishedOnly
369
488
configDataDto , err = impl .getPublishedConfigData (ctx , configDataQueryParams , appId , envId , clusterId , userHasAdminAccess , systemMetadata )
370
489
if err != nil {
0 commit comments