Skip to content

Commit 5ffb3dd

Browse files
committed
previous deployments stage added
1 parent 494d30b commit 5ffb3dd

10 files changed

+315
-24
lines changed

pkg/configDiff/DeploymentConfigurationService.go

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

5760
func 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+
363476
func (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 {

pkg/configDiff/adaptor/adaptor.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package adaptor
33
import (
44
bean3 "github.com/devtron-labs/devtron/pkg/bean"
55
bean2 "github.com/devtron-labs/devtron/pkg/configDiff/bean"
6-
"github.com/devtron-labs/devtron/pkg/pipeline"
76
"github.com/devtron-labs/devtron/pkg/pipeline/adapter"
87
"github.com/devtron-labs/devtron/pkg/pipeline/bean"
98
)
@@ -31,12 +30,12 @@ func GetCmCsAppAndEnvLevelMap(cMCSNamesAppLevel, cMCSNamesEnvLevel []bean.Config
3130
return cMCSNamesAppLevelMap, cMCSNamesEnvLevelMap
3231
}
3332

34-
func ConfigListConvertor(r bean3.ConfigList) pipeline.ConfigsList {
33+
func ConfigListConvertor(r bean3.ConfigList) bean.ConfigsList {
3534
pipelineConfigData := make([]*bean.ConfigData, 0, len(r.ConfigData))
3635
for _, item := range r.ConfigData {
3736
pipelineConfigData = append(pipelineConfigData, adapter.ConvertConfigDataToPipelineConfigData(item))
3837
}
39-
return pipeline.ConfigsList{ConfigData: pipelineConfigData}
38+
return bean.ConfigsList{ConfigData: pipelineConfigData}
4039
}
4140

4241
func SecretListConvertor(r bean3.SecretList) bean.SecretsList {
@@ -47,7 +46,7 @@ func SecretListConvertor(r bean3.SecretList) bean.SecretsList {
4746
return bean.SecretsList{ConfigData: pipelineConfigData}
4847
}
4948

50-
func ReverseConfigListConvertor(r pipeline.ConfigsList) bean3.ConfigList {
49+
func ReverseConfigListConvertor(r bean.ConfigsList) bean3.ConfigList {
5150
configData := make([]*bean3.ConfigData, 0, len(r.ConfigData))
5251
for _, item := range r.ConfigData {
5352
configData = append(configData, adapter.ConvertPipelineConfigDataToConfigData(item))

pkg/configDiff/bean/bean.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type ConfigState string
1313

1414
const (
1515
PublishedConfigState ConfigState = "PublishedOnly"
16+
PreviousDeployments ConfigState = "PreviousDeployments"
1617
)
1718

1819
func (r ConfigState) ToString() string {

pkg/pipeline/ConfigMapService.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ const (
4747
HashiCorpVault string = "HashiCorpVault"
4848
)
4949

50-
type ConfigsList struct {
51-
ConfigData []*bean.ConfigData `json:"maps"`
52-
}
53-
5450
type ConfigMapService interface {
5551
CMGlobalAddUpdate(configMapRequest *bean.ConfigDataRequest) (*bean.ConfigDataRequest, error)
5652
CMGlobalFetch(appId int) (*bean.ConfigDataRequest, error)
@@ -165,7 +161,7 @@ func (impl ConfigMapServiceImpl) CMGlobalAddUpdate(configMapRequest *bean.Config
165161
impl.logger.Errorw("error while fetching from db", "error", err)
166162
return nil, err
167163
}
168-
configsList := &ConfigsList{}
164+
configsList := &bean.ConfigsList{}
169165
found := false
170166
var configs []*bean.ConfigData
171167
if len(model.ConfigMapData) > 0 {
@@ -208,7 +204,7 @@ func (impl ConfigMapServiceImpl) CMGlobalAddUpdate(configMapRequest *bean.Config
208204

209205
} else {
210206
//creating config map record for first time
211-
configsList := &ConfigsList{
207+
configsList := &bean.ConfigsList{
212208
ConfigData: configMapRequest.ConfigData,
213209
}
214210
configDataByte, err := json.Marshal(configsList)
@@ -254,7 +250,7 @@ func (impl ConfigMapServiceImpl) CMGlobalFetch(appId int) (*bean.ConfigDataReque
254250
impl.logger.Debugw("no config map data found for this request", "appId", appId)
255251
}
256252

257-
configMapGlobalList := &ConfigsList{}
253+
configMapGlobalList := &bean.ConfigsList{}
258254
if len(configMapGlobal.ConfigMapData) > 0 {
259255
err = json.Unmarshal([]byte(configMapGlobal.ConfigMapData), configMapGlobalList)
260256
if err != nil {
@@ -301,7 +297,7 @@ func (impl ConfigMapServiceImpl) CMEnvironmentAddUpdate(configMapRequest *bean.C
301297
return nil, err
302298
}
303299
if err == nil && model.Id > 0 {
304-
configsList := &ConfigsList{}
300+
configsList := &bean.ConfigsList{}
305301
found := false
306302
var configs []*bean.ConfigData
307303
if len(model.ConfigMapData) > 0 {
@@ -345,7 +341,7 @@ func (impl ConfigMapServiceImpl) CMEnvironmentAddUpdate(configMapRequest *bean.C
345341

346342
} else if err == pg.ErrNoRows {
347343
//creating config map record for first time
348-
configsList := &ConfigsList{
344+
configsList := &bean.ConfigsList{
349345
ConfigData: configMapRequest.ConfigData,
350346
}
351347
configDataByte, err := json.Marshal(configsList)
@@ -391,7 +387,7 @@ func (impl ConfigMapServiceImpl) CMGlobalFetchForEdit(name string, id int) (*bea
391387
impl.logger.Debugw("no config map data found for this request", "id", id)
392388
}
393389

394-
configMapGlobalList := &ConfigsList{}
390+
configMapGlobalList := &bean.ConfigsList{}
395391
if len(configMapGlobal.ConfigMapData) > 0 {
396392
err = json.Unmarshal([]byte(configMapGlobal.ConfigMapData), configMapGlobalList)
397393
if err != nil {
@@ -439,7 +435,7 @@ func (impl ConfigMapServiceImpl) CMEnvironmentFetch(appId int, envId int) (*bean
439435
if pg.ErrNoRows == err {
440436
impl.logger.Debugw("no config map data found for this request", "appId", appId)
441437
}
442-
configMapGlobalList := &ConfigsList{}
438+
configMapGlobalList := &bean.ConfigsList{}
443439
if len(configMapGlobal.ConfigMapData) > 0 {
444440
err = json.Unmarshal([]byte(configMapGlobal.ConfigMapData), configMapGlobalList)
445441
if err != nil {
@@ -454,7 +450,7 @@ func (impl ConfigMapServiceImpl) CMEnvironmentFetch(appId int, envId int) (*bean
454450
if pg.ErrNoRows == err {
455451
impl.logger.Debugw("no config map data found for this request", "appId", appId)
456452
}
457-
configsListEnvLevel := &ConfigsList{}
453+
configsListEnvLevel := &bean.ConfigsList{}
458454
if len(configMapEnvLevel.ConfigMapData) > 0 {
459455
err = json.Unmarshal([]byte(configMapEnvLevel.ConfigMapData), configsListEnvLevel)
460456
if err != nil {
@@ -918,7 +914,7 @@ func (impl ConfigMapServiceImpl) CMGlobalDelete(name string, id int, userId int3
918914
impl.logger.Errorw("error while fetching from db", "error", err)
919915
return false, err
920916
}
921-
configsList := &ConfigsList{}
917+
configsList := &bean.ConfigsList{}
922918
found := false
923919
var configs []*bean.ConfigData
924920
if len(model.ConfigMapData) > 0 {
@@ -974,7 +970,7 @@ func (impl ConfigMapServiceImpl) CMEnvironmentDelete(name string, id int, userId
974970
impl.logger.Errorw("error while fetching from db", "error", err)
975971
return false, err
976972
}
977-
configsList := &ConfigsList{}
973+
configsList := &bean.ConfigsList{}
978974
found := false
979975
var configs []*bean.ConfigData
980976
if len(model.ConfigMapData) > 0 {
@@ -1140,7 +1136,7 @@ func (impl ConfigMapServiceImpl) CMGlobalDeleteByAppId(name string, appId int, u
11401136
impl.logger.Errorw("error while fetching from db", "error", err)
11411137
return false, err
11421138
}
1143-
configsList := &ConfigsList{}
1139+
configsList := &bean.ConfigsList{}
11441140
found := false
11451141
var configs []*bean.ConfigData
11461142
if len(model.ConfigMapData) > 0 {
@@ -1190,7 +1186,7 @@ func (impl ConfigMapServiceImpl) CMEnvironmentDeleteByAppIdAndEnvId(name string,
11901186
impl.logger.Errorw("error while fetching from db", "error", err)
11911187
return false, err
11921188
}
1193-
configsList := &ConfigsList{}
1189+
configsList := &bean.ConfigsList{}
11941190
found := false
11951191
var configs []*bean.ConfigData
11961192
if len(model.ConfigMapData) > 0 {
@@ -1540,7 +1536,7 @@ func (impl ConfigMapServiceImpl) ConfigSecretGlobalBulkPatch(bulkPatchRequest *b
15401536
continue
15411537
}
15421538
if bulkPatchRequest.Type == "CM" {
1543-
configsList := &ConfigsList{}
1539+
configsList := &bean.ConfigsList{}
15441540
var configs []*bean.ConfigData
15451541
if len(model.ConfigMapData) > 0 {
15461542
err = json.Unmarshal([]byte(model.ConfigMapData), configsList)
@@ -1645,7 +1641,7 @@ func (impl ConfigMapServiceImpl) ConfigSecretEnvironmentBulkPatch(bulkPatchReque
16451641
continue
16461642
}
16471643
if bulkPatchRequest.Type == "CM" {
1648-
configsList := &ConfigsList{}
1644+
configsList := &bean.ConfigsList{}
16491645
var configs []*bean.ConfigData
16501646
if len(model.ConfigMapData) > 0 {
16511647
err = json.Unmarshal([]byte(model.ConfigMapData), configsList)

pkg/pipeline/bean/ConfigMapBean.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ type SecretsList struct {
119119
ConfigData []*ConfigData `json:"secrets"`
120120
}
121121

122+
type ConfigsList struct {
123+
ConfigData []*ConfigData `json:"maps"`
124+
}
125+
122126
type ConfigNameAndType struct {
123127
Id int
124128
Name string

0 commit comments

Comments
 (0)