Skip to content

Commit d82cdcb

Browse files
feat: scoped variable CMCS support and manager layer refactorings (#4174)
* app name * image tag * refactoring - move to oss * WIP * added resolved template in API response * reverting regex changes * wip * cleaned comments * test commit * fix * fix env for specific deployment * snapshot fixes for deployment * fix * sensitive variable masking in all flows * removed dead comments * validation for non-string sensitive * TODO COMMIT * scoped variable support added for add/update for cm/cs * support added for trigger * extra function call removed * addressed comments * env variable code commented and issue fixing * commit minor fix * used request context * changes done in get apis * prod issue fixed * wire gen * scopedVariable added in GetLatestCMCSConfig * fix ctx for manifest flows * resolved values in GetDeploymentTemplate * secret issue fixed * bug fixes * encoder and decoder added * fixes for helm generate * secret data resolution issue fixed * decoder resolution issue fixed * issue fixed around GetLatestCMCSConfig * encoder decoder added in GetLatestCMCSConfig * commented code removed * encoder decoder added in GetDeployedHistoryDetailForCMCSByPipelineIdAndWfrId * encoder decoder added in GetHistoryForDeployedCMCSById * minor fixes * code review comments resolved * some refactoring done around getConfigMapAndSecretJsonV2 * refactoring and testing done around last saved trigger * comments removed * manager layer refactoring * code move * refactoring cmcs flows * removing redundant methods * cleaning up * todo added * bug fixed around no variable case and get flows * bug fixed around specific trigger type * bug fixed around specific trigger type for encoding decoding * encode/decode refactor * ctx passed for isSuperAdmin check * cleaned dead comments * VariableSnapshotForCS,ResolvedTemplateDataForCS,VariableSnapshotForCM,ResolvedTemplateDataForCM removed * code review comment resolved * deployment-Component/detail issue fixed * deployment-Component/detail issue fixed for CM * resolved template issue fixed * code refactored around component * cmcs manager * cleaning comments * reverting cmcs merge function * hook for stage trigger * minor * DT fix * cmcs fixes * granular snapshot for cmcs * sensitive fix * cmcs to use string type resolution * secret parsing fix * soft parsing for cmcs trigger --------- Co-authored-by: adi6859 <[email protected]>
1 parent a306434 commit d82cdcb

34 files changed

+1574
-795
lines changed

Wire.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ func InitializeApp() (*App, error) {
281281
variables.NewVariableSnapshotHistoryServiceImpl,
282282
wire.Bind(new(variables.VariableSnapshotHistoryService), new(*variables.VariableSnapshotHistoryServiceImpl)),
283283

284+
variables.NewScopedVariableManagerImpl,
285+
wire.Bind(new(variables.ScopedVariableManager), new(*variables.ScopedVariableManagerImpl)),
286+
287+
variables.NewScopedVariableCMCSManagerImpl,
288+
wire.Bind(new(variables.ScopedVariableCMCSManager), new(*variables.ScopedVariableCMCSManagerImpl)),
289+
284290
//end
285291

286292
chart.NewChartServiceImpl,

api/bean/ConfigMapAndSecret.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,24 @@ func (configSecretJson ConfigSecretJson) GetDereferencedSecrets() []ConfigSecret
6969
func (configSecretJson *ConfigSecretJson) SetReferencedSecrets(secrets []ConfigSecretMap) {
7070
configSecretJson.Secrets = util.GetReferencedArray(secrets)
7171
}
72+
73+
func (ConfigSecretRootJson) GetTransformedDataForSecretData(data string, mode util.SecretTransformMode) (string, error) {
74+
secretsJson := ConfigSecretRootJson{}
75+
err := json.Unmarshal([]byte(data), &secretsJson)
76+
if err != nil {
77+
return "", err
78+
}
79+
80+
for _, configData := range secretsJson.ConfigSecretJson.Secrets {
81+
configData.Data, err = util.GetDecodedAndEncodedData(configData.Data, mode)
82+
if err != nil {
83+
return "", err
84+
}
85+
}
86+
87+
marshal, err := json.Marshal(secretsJson)
88+
if err != nil {
89+
return "", err
90+
}
91+
return string(marshal), nil
92+
}

internal/sql/repository/chartConfig/EnvConfigOverrideRepository.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ type EnvConfigOverride struct {
4646
sql.AuditLog
4747
ResolvedEnvOverrideValues string `sql:"-"`
4848
VariableSnapshot map[string]string `sql:"-"`
49+
//ResolvedEnvOverrideValuesForCM string `sql:"-"`
50+
VariableSnapshotForCM map[string]string `sql:"-"`
51+
//ResolvedEnvOverrideValuesForCS string `sql:"-"`
52+
VariableSnapshotForCS map[string]string `sql:"-"`
4953
}
5054

5155
type EnvConfigOverrideRepository interface {

pkg/app/AppService.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import (
3434
"github.com/devtron-labs/devtron/pkg/k8s"
3535
repository3 "github.com/devtron-labs/devtron/pkg/pipeline/history/repository"
3636
repository5 "github.com/devtron-labs/devtron/pkg/pipeline/repository"
37+
"github.com/devtron-labs/devtron/pkg/resourceQualifiers"
3738
"github.com/devtron-labs/devtron/pkg/variables"
38-
"github.com/devtron-labs/devtron/pkg/variables/parsers"
3939
_ "github.com/devtron-labs/devtron/pkg/variables/repository"
4040
"github.com/devtron-labs/devtron/util/argo"
4141
"go.opentelemetry.io/otel"
@@ -160,10 +160,7 @@ type AppServiceImpl struct {
160160
globalEnvVariables *util2.GlobalEnvVariables
161161
manifestPushConfigRepository repository5.ManifestPushConfigRepository
162162
GitOpsManifestPushService GitOpsPushService
163-
variableSnapshotHistoryService variables.VariableSnapshotHistoryService
164-
scopedVariableService variables.ScopedVariableService
165-
variableEntityMappingService variables.VariableEntityMappingService
166-
variableTemplateParser parsers.VariableTemplateParser
163+
scopedVariableManager variables.ScopedVariableCMCSManager
167164
argoClientWrapperService argocdServer.ArgoClientWrapperService
168165
}
169166

@@ -174,7 +171,7 @@ type AppService interface {
174171
//TriggerCD(artifact *repository.CiArtifact, cdWorkflowId, wfrId int, pipeline *pipelineConfig.Pipeline, triggeredAt time.Time) error
175172
GetConfigMapAndSecretJson(appId int, envId int, pipelineId int) ([]byte, error)
176173
UpdateCdWorkflowRunnerByACDObject(app *v1alpha1.Application, cdWfrId int, updateTimedOutStatus bool) error
177-
GetCmSecretNew(appId int, envId int, isJob bool) (*bean.ConfigMapJson, *bean.ConfigSecretJson, error)
174+
GetCmSecretNew(appId int, envId int, isJob bool, scope resourceQualifiers.Scope) (*bean.ConfigMapJson, *bean.ConfigSecretJson, error)
178175
//MarkImageScanDeployed(appId int, envId int, imageDigest string, clusterId int, isScanEnabled bool) error
179176
UpdateDeploymentStatusForGitOpsPipelines(app *v1alpha1.Application, statusTime time.Time, isAppStore bool) (bool, bool, *chartConfig.PipelineOverride, error)
180177
WriteCDSuccessEvent(appId int, envId int, override *chartConfig.PipelineOverride)
@@ -245,11 +242,8 @@ func NewAppService(
245242
globalEnvVariables *util2.GlobalEnvVariables, helmAppService client2.HelmAppService,
246243
manifestPushConfigRepository repository5.ManifestPushConfigRepository,
247244
GitOpsManifestPushService GitOpsPushService,
248-
variableSnapshotHistoryService variables.VariableSnapshotHistoryService,
249-
scopedVariableService variables.ScopedVariableService,
250-
variableEntityMappingService variables.VariableEntityMappingService,
251-
variableTemplateParser parsers.VariableTemplateParser,
252245
argoClientWrapperService argocdServer.ArgoClientWrapperService,
246+
scopedVariableManager variables.ScopedVariableCMCSManager,
253247
) *AppServiceImpl {
254248
appServiceImpl := &AppServiceImpl{
255249
environmentConfigRepository: environmentConfigRepository,
@@ -311,11 +305,8 @@ func NewAppService(
311305
helmAppService: helmAppService,
312306
manifestPushConfigRepository: manifestPushConfigRepository,
313307
GitOpsManifestPushService: GitOpsManifestPushService,
314-
variableSnapshotHistoryService: variableSnapshotHistoryService,
315-
scopedVariableService: scopedVariableService,
316-
variableEntityMappingService: variableEntityMappingService,
317-
variableTemplateParser: variableTemplateParser,
318308
argoClientWrapperService: argoClientWrapperService,
309+
scopedVariableManager: scopedVariableManager,
319310
}
320311
return appServiceImpl
321312
}
@@ -1122,7 +1113,7 @@ func (impl *AppServiceImpl) autoHealChartLocationInChart(ctx context.Context, en
11221113
}
11231114

11241115
// FIXME tmp workaround
1125-
func (impl *AppServiceImpl) GetCmSecretNew(appId int, envId int, isJob bool) (*bean.ConfigMapJson, *bean.ConfigSecretJson, error) {
1116+
func (impl *AppServiceImpl) GetCmSecretNew(appId int, envId int, isJob bool, scope resourceQualifiers.Scope) (*bean.ConfigMapJson, *bean.ConfigSecretJson, error) {
11261117
var configMapJson string
11271118
var secretDataJson string
11281119
var configMapJsonApp string
@@ -1187,7 +1178,13 @@ func (impl *AppServiceImpl) GetCmSecretNew(appId int, envId int, isJob bool) (*b
11871178
return nil, nil, err
11881179
}
11891180
}
1190-
return &configResponse, &secretResponse, nil
1181+
1182+
resolvedConfigResponse, resolvedSecretResponse, err := impl.scopedVariableManager.ResolveForPrePostStageTrigger(scope, configResponse, secretResponse, configMapA.Id, configMapE.Id)
1183+
if err != nil {
1184+
return nil, nil, err
1185+
}
1186+
1187+
return resolvedConfigResponse, resolvedSecretResponse, nil
11911188
}
11921189

11931190
// depricated

pkg/bean/configSecretData.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package bean
2+
3+
import (
4+
"encoding/json"
5+
"github.com/devtron-labs/devtron/util"
6+
)
7+
8+
type ConfigList struct {
9+
ConfigData []*ConfigData `json:"maps"`
10+
}
11+
12+
type SecretList struct {
13+
ConfigData []*ConfigData `json:"secrets"`
14+
}
15+
16+
type ConfigData struct {
17+
Name string `json:"name"`
18+
Type string `json:"type"`
19+
External bool `json:"external"`
20+
MountPath string `json:"mountPath,omitempty"`
21+
Data json.RawMessage `json:"data"`
22+
DefaultData json.RawMessage `json:"defaultData,omitempty"`
23+
DefaultMountPath string `json:"defaultMountPath,omitempty"`
24+
Global bool `json:"global"`
25+
ExternalSecretType string `json:"externalType"`
26+
ExternalSecret []ExternalSecret `json:"secretData"`
27+
DefaultExternalSecret []ExternalSecret `json:"defaultSecretData,omitempty"`
28+
ESOSecretData ESOSecretData `json:"esoSecretData"`
29+
DefaultESOSecretData ESOSecretData `json:"defaultESOSecretData,omitempty"`
30+
RoleARN string `json:"roleARN"`
31+
SubPath bool `json:"subPath"`
32+
FilePermission string `json:"filePermission"`
33+
}
34+
35+
type ExternalSecret struct {
36+
Key string `json:"key"`
37+
Name string `json:"name"`
38+
Property string `json:"property,omitempty"`
39+
IsBinary bool `json:"isBinary"`
40+
}
41+
42+
type ESOSecretData struct {
43+
SecretStore json.RawMessage `json:"secretStore,omitempty"`
44+
SecretStoreRef json.RawMessage `json:"secretStoreRef,omitempty"`
45+
EsoData []ESOData `json:"esoData"`
46+
RefreshInterval string `json:"refreshInterval,omitempty"`
47+
}
48+
49+
type ESOData struct {
50+
SecretKey string `json:"secretKey"`
51+
Key string `json:"key"`
52+
Property string `json:"property,omitempty"`
53+
}
54+
55+
func (ConfigData) GetTransformedDataForSecretData(data string, mode util.SecretTransformMode) (string, error) {
56+
secretDataMap := make(map[string]*ConfigData)
57+
err := json.Unmarshal([]byte(data), &secretDataMap)
58+
if err != nil {
59+
return "", err
60+
}
61+
62+
for _, configData := range secretDataMap {
63+
data, err := util.GetDecodedAndEncodedData(configData.Data, mode)
64+
if err != nil {
65+
return "", err
66+
}
67+
configData.Data = data
68+
69+
}
70+
resolvedTemplate, err := json.Marshal(secretDataMap)
71+
if err != nil {
72+
return "", err
73+
}
74+
return string(resolvedTemplate), nil
75+
}
76+
77+
func (SecretList) GetTransformedDataForSecret(data string, mode util.SecretTransformMode) (string, error) {
78+
secretsList := SecretList{}
79+
err := json.Unmarshal([]byte(data), &secretsList)
80+
if err != nil {
81+
return "", err
82+
}
83+
84+
for _, configData := range secretsList.ConfigData {
85+
configData.Data, err = util.GetDecodedAndEncodedData(configData.Data, mode)
86+
if err != nil {
87+
return "", err
88+
}
89+
}
90+
91+
marshal, err := json.Marshal(secretsList)
92+
if err != nil {
93+
return "", err
94+
}
95+
return string(marshal), nil
96+
}

pkg/bulkAction/BulkUpdateService.go

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/devtron-labs/devtron/pkg/pipeline/history"
2929
repository4 "github.com/devtron-labs/devtron/pkg/pipeline/history/repository"
3030
"github.com/devtron-labs/devtron/pkg/variables"
31-
"github.com/devtron-labs/devtron/pkg/variables/parsers"
3231
repository5 "github.com/devtron-labs/devtron/pkg/variables/repository"
3332
"github.com/devtron-labs/devtron/util/argo"
3433
"github.com/devtron-labs/devtron/util/rbac"
@@ -92,8 +91,7 @@ type BulkUpdateServiceImpl struct {
9291
appWorkflowService appWorkflow2.AppWorkflowService
9392
pubsubClient *pubsub.PubSubClientServiceImpl
9493
argoUserService argo.ArgoUserService
95-
variableEntityMappingService variables.VariableEntityMappingService
96-
variableTemplateParser parsers.VariableTemplateParser
94+
scopedVariableManager variables.ScopedVariableManager
9795
}
9896

9997
func NewBulkUpdateServiceImpl(bulkUpdateRepository bulkUpdate.BulkUpdateRepository,
@@ -124,8 +122,8 @@ func NewBulkUpdateServiceImpl(bulkUpdateRepository bulkUpdate.BulkUpdateReposito
124122
appWorkflowService appWorkflow2.AppWorkflowService,
125123
pubsubClient *pubsub.PubSubClientServiceImpl,
126124
argoUserService argo.ArgoUserService,
127-
variableEntityMappingService variables.VariableEntityMappingService,
128-
variableTemplateParser parsers.VariableTemplateParser) (*BulkUpdateServiceImpl, error) {
125+
scopedVariableManager variables.ScopedVariableManager,
126+
) (*BulkUpdateServiceImpl, error) {
129127
impl := &BulkUpdateServiceImpl{
130128
bulkUpdateRepository: bulkUpdateRepository,
131129
chartRepository: chartRepository,
@@ -159,8 +157,7 @@ func NewBulkUpdateServiceImpl(bulkUpdateRepository bulkUpdate.BulkUpdateReposito
159157
appWorkflowService: appWorkflowService,
160158
pubsubClient: pubsubClient,
161159
argoUserService: argoUserService,
162-
variableTemplateParser: variableTemplateParser,
163-
variableEntityMappingService: variableEntityMappingService,
160+
scopedVariableManager: scopedVariableManager,
164161
}
165162

166163
err := impl.SubscribeToCdBulkTriggerTopic()
@@ -465,7 +462,7 @@ func (impl BulkUpdateServiceImpl) BulkUpdateDeploymentTemplate(bulkUpdatePayload
465462
}
466463
//VARIABLE_MAPPING_UPDATE
467464
//NOTE: this flow is doesn't have the user info, therefore updated by is being set to the last updated by
468-
err = impl.extractAndMapVariables(chart.GlobalOverride, chart.Id, repository5.EntityTypeDeploymentTemplateAppLevel, chart.UpdatedBy)
465+
err = impl.scopedVariableManager.ExtractAndMapVariables(chart.GlobalOverride, chart.Id, repository5.EntityTypeDeploymentTemplateAppLevel, chart.UpdatedBy, nil)
469466
if err != nil {
470467
return nil
471468
}
@@ -538,7 +535,7 @@ func (impl BulkUpdateServiceImpl) BulkUpdateDeploymentTemplate(bulkUpdatePayload
538535
impl.logger.Errorw("error in creating entry for env deployment template history", "err", err, "envOverride", chartEnv)
539536
}
540537
//VARIABLE_MAPPING_UPDATE
541-
err = impl.extractAndMapVariables(chartEnv.EnvOverrideValues, chartEnv.Id, repository5.EntityTypeDeploymentTemplateEnvLevel, chartEnv.UpdatedBy)
538+
err = impl.scopedVariableManager.ExtractAndMapVariables(chartEnv.EnvOverrideValues, chartEnv.Id, repository5.EntityTypeDeploymentTemplateEnvLevel, chartEnv.UpdatedBy, nil)
542539
if err != nil {
543540
return nil
544541
}
@@ -554,21 +551,6 @@ func (impl BulkUpdateServiceImpl) BulkUpdateDeploymentTemplate(bulkUpdatePayload
554551
return deploymentTemplateBulkUpdateResponse
555552
}
556553

557-
func (impl BulkUpdateServiceImpl) extractAndMapVariables(template string, entityId int, entityType repository5.EntityType, userId int32) error {
558-
usedVariables, err := impl.variableTemplateParser.ExtractVariables(template, parsers.JsonVariableTemplate)
559-
if err != nil {
560-
return err
561-
}
562-
err = impl.variableEntityMappingService.UpdateVariablesForEntity(usedVariables, repository5.Entity{
563-
EntityType: entityType,
564-
EntityId: entityId,
565-
}, userId, nil)
566-
if err != nil {
567-
return err
568-
}
569-
return nil
570-
}
571-
572554
func (impl BulkUpdateServiceImpl) BulkUpdateConfigMap(bulkUpdatePayload *BulkUpdatePayload) *CmAndSecretBulkUpdateResponse {
573555
configMapBulkUpdateResponse := &CmAndSecretBulkUpdateResponse{}
574556
var appNameIncludes []string

0 commit comments

Comments
 (0)