Skip to content

Commit

Permalink
configreplacements
Browse files Browse the repository at this point in the history
Signed-off-by: Gerd Oberlechner <[email protected]>
  • Loading branch information
geoberle committed Nov 14, 2024
1 parent 4fe00e4 commit 2484307
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 46 deletions.
2 changes: 1 addition & 1 deletion tooling/templatize/cmd/rolloutoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (o *ValidatedRolloutOptions) Complete() (*RolloutOptions, error) {
return nil, err
}

variables, err := completed.ConfigProvider.GetVariables(o.Cloud, o.DeployEnv, o.Region, config.NewConfigEvaluationContext(o.Region, o.RegionShort, o.Stamp))
variables, err := completed.ConfigProvider.GetVariables(o.Cloud, o.DeployEnv, o.Region, config.NewConfigReplacements(o.Region, o.RegionShort, o.Stamp))
if err != nil {
return nil, fmt.Errorf("failed to get variables: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tooling/templatize/cmd/run/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (o *RunOptions) RunPipeline(ctx context.Context) error {
o.RolloutOptions.Cloud,
o.RolloutOptions.DeployEnv,
o.RolloutOptions.Region,
config.NewConfigEvaluationContext(
config.NewConfigReplacements(
o.RolloutOptions.Region,
o.RolloutOptions.RegionShort,
o.RolloutOptions.Stamp,
Expand Down
62 changes: 29 additions & 33 deletions tooling/templatize/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,40 @@ import (
"gopkg.in/yaml.v3"
)

func DefaultConfigEvaluationContext() *ConfigEvaluationContext {
return &ConfigEvaluationContext{
Region: "",
RegionShort: "",
Stamp: "",
}
func DefaultConfigReplacements() *ConfigReplacements {
return NewConfigReplacements("", "", "")
}

func NewConfigEvaluationContext(region, regionShort, stamp string) *ConfigEvaluationContext {
return &ConfigEvaluationContext{
Region: region,
RegionShort: regionShort,
Stamp: stamp,
func NewConfigReplacements(regionReplacement, regionShortReplacement, stampReplacement string) *ConfigReplacements {
return &ConfigReplacements{
RegionReplacement: regionReplacement,
RegionShortReplacement: regionShortReplacement,
StampReplacement: stampReplacement,
}
}

type ConfigEvaluationContext struct {
Region string
RegionShort string
Stamp string
type ConfigReplacements struct {
RegionReplacement string
RegionShortReplacement string
StampReplacement string
}

func (c *ConfigEvaluationContext) AsMap() map[string]interface{} {
func (c *ConfigReplacements) AsMap() map[string]interface{} {
return map[string]interface{}{
"ctx": map[string]interface{}{
"region": c.Region,
"regionShort": c.RegionShort,
"stamp": c.Stamp,
"region": c.RegionReplacement,
"regionShort": c.RegionShortReplacement,
"stamp": c.StampReplacement,
},
}
}

type ConfigProvider interface {
Validate(cloud, deployEnv string) error
GetVariables(cloud, deployEnv, region string, configEvalContext *ConfigEvaluationContext) (Variables, error)
GetDeployEnvVariables(cloud, deployEnv, region string, configEvalContext *ConfigEvaluationContext) (Variables, error)
GetVariables(cloud, deployEnv, region string, configReplacements *ConfigReplacements) (Variables, error)
GetDeployEnvVariables(cloud, deployEnv string, configReplacements *ConfigReplacements) (Variables, error)
GetRegions(cloud, deployEnv string) ([]string, error)
GetRegionOverrides(cloud, deployEnv, region string, configEvalContext *ConfigEvaluationContext) (Variables, error)
GetRegionOverrides(cloud, deployEnv, region string, configReplacements *ConfigReplacements) (Variables, error)
}

func NewConfigProvider(config string) ConfigProvider {
Expand Down Expand Up @@ -93,14 +89,14 @@ func mergeVariables(base, override Variables) Variables {
return base
}

func (cp *configProviderImpl) GetVariables(cloud, deployEnv, region string, configEvalContext *ConfigEvaluationContext) (Variables, error) {
variables, err := cp.GetDeployEnvVariables(cloud, deployEnv, region, configEvalContext)
func (cp *configProviderImpl) GetVariables(cloud, deployEnv, region string, configReplacements *ConfigReplacements) (Variables, error) {
variables, err := cp.GetDeployEnvVariables(cloud, deployEnv, configReplacements)
if err != nil {
return nil, err
}

// region overrides
regionOverrides, err := cp.GetRegionOverrides(cloud, deployEnv, region, configEvalContext)
regionOverrides, err := cp.GetRegionOverrides(cloud, deployEnv, region, configReplacements)
if err != nil {
return nil, err
}
Expand All @@ -110,7 +106,7 @@ func (cp *configProviderImpl) GetVariables(cloud, deployEnv, region string, conf
}

func (cp *configProviderImpl) Validate(cloud, deployEnv string) error {
config, err := cp.loadConfig(DefaultConfigEvaluationContext())
config, err := cp.loadConfig(DefaultConfigReplacements())
if err != nil {
return err
}
Expand All @@ -124,8 +120,8 @@ func (cp *configProviderImpl) Validate(cloud, deployEnv string) error {
return nil
}

func (cp *configProviderImpl) GetDeployEnvVariables(cloud, deployEnv, region string, configEvalContext *ConfigEvaluationContext) (Variables, error) {
config, err := cp.loadConfig(configEvalContext)
func (cp *configProviderImpl) GetDeployEnvVariables(cloud, deployEnv string, configReplacements *ConfigReplacements) (Variables, error) {
config, err := cp.loadConfig(configReplacements)
if err != nil {
return nil, err
}
Expand All @@ -143,7 +139,7 @@ func (cp *configProviderImpl) GetDeployEnvVariables(cloud, deployEnv, region str
}

func (cp *configProviderImpl) GetRegions(cloud, deployEnv string) ([]string, error) {
config, err := cp.loadConfig(DefaultConfigEvaluationContext())
config, err := cp.loadConfig(DefaultConfigReplacements())
if err != nil {
return nil, err
}
Expand All @@ -155,18 +151,18 @@ func (cp *configProviderImpl) GetRegions(cloud, deployEnv string) ([]string, err
return regions, nil
}

func (cp *configProviderImpl) GetRegionOverrides(cloud, deployEnv, region string, configEvalContext *ConfigEvaluationContext) (Variables, error) {
config, err := cp.loadConfig(configEvalContext)
func (cp *configProviderImpl) GetRegionOverrides(cloud, deployEnv, region string, configReplacements *ConfigReplacements) (Variables, error) {
config, err := cp.loadConfig(configReplacements)
if err != nil {
return nil, err
}
return config.GetRegionOverrides(cloud, deployEnv, region), nil
}

func (cp *configProviderImpl) loadConfig(configEvalContext *ConfigEvaluationContext) (VariableOverrides, error) {
func (cp *configProviderImpl) loadConfig(configReplacements *ConfigReplacements) (VariableOverrides, error) {
// TODO validate that field names are unique regardless of casing
// parse, execute and unmarshal the config file as a template to generate the final config file
bytes, err := PreprocessFile(cp.config, configEvalContext.AsMap())
bytes, err := PreprocessFile(cp.config, configReplacements.AsMap())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion tooling/templatize/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestConfigProvider(t *testing.T) {

configProvider := NewConfigProvider("../../testdata/config.yaml")

variables, err := configProvider.GetVariables("public", "int", region, NewConfigEvaluationContext(region, regionShort, stamp))
variables, err := configProvider.GetVariables("public", "int", region, NewConfigReplacements(region, regionShort, stamp))
assert.NoError(t, err)
assert.NotNil(t, variables)

Expand Down
20 changes: 10 additions & 10 deletions tooling/templatize/pkg/ev2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import (
// This package contains helper functions to extract EV2 conformant data from a config.yaml file.
//

func newEv2EvaluationContext() *config.ConfigEvaluationContext {
return &config.ConfigEvaluationContext{
Region: "$location()",
RegionShort: "$(regionShortName)",
Stamp: "$stamp()",
}
func newEv2ConfigReplacements() *config.ConfigReplacements {
return config.NewConfigReplacements(
"$location()",
"$(regionShortName)",
"$stamp()",
)
}

// GetNonRegionalServiceConfigVariables returns all non-regional configuration variables of a config.yaml file.
// Non regional means: global variables + cloud overrides + deployment environment overrides - but not regional overrides.
// The variable values are formatted to contain EV2 $location(), $stamp() and $(serviceConfigVar) variables.
// This function is useful to get the variables to fill the `Settings` section of an EV2 `ServiceConfig.json“
func GetNonRegionalServiceConfigVariables(configProvider config.ConfigProvider, cloud, deployEnv string) (config.Variables, error) {
return configProvider.GetVariables(cloud, deployEnv, "", newEv2EvaluationContext())
return configProvider.GetVariables(cloud, deployEnv, "", newEv2ConfigReplacements())
}

// GetRegionalServiceConfigVariableOverrides returns the regional overrides of a config.yaml file.
Expand All @@ -36,7 +36,7 @@ func GetRegionalServiceConfigVariableOverrides(configProvider config.ConfigProvi
}
overrides := make(map[string]config.Variables)
for _, region := range regions {
regionOverrides, err := configProvider.GetRegionOverrides(cloud, deployEnv, region, newEv2EvaluationContext())
regionOverrides, err := configProvider.GetRegionOverrides(cloud, deployEnv, region, newEv2ConfigReplacements())
if err != nil {
return nil, err
}
Expand All @@ -49,7 +49,7 @@ func GetRegionalServiceConfigVariableOverrides(configProvider config.ConfigProvi
// It uses the provided configProvider to fetch the variables, flattens them into a __VAR__ = $config(var) formatted map.
// This function is useful to get the find/replace pairs for an EV2 `ScopeBinding.json`
func ScopeBindingVariables(configProvider config.ConfigProvider, cloud, deployEnv string) (map[string]string, error) {
vars, err := configProvider.GetVariables(cloud, deployEnv, "", newEv2EvaluationContext())
vars, err := configProvider.GetVariables(cloud, deployEnv, "", newEv2ConfigReplacements())
if err != nil {
return nil, err
}
Expand All @@ -65,7 +65,7 @@ func ScopeBindingVariables(configProvider config.ConfigProvider, cloud, deployEn
// while maintaining EV2 conformant system variables.
// This function is useful to process a pipeline.yaml file so that it contains EV2 system variables.
func PreprocessFileForEV2(configProvider config.ConfigProvider, cloud, deployEnv string, templateFile string) ([]byte, error) {
vars, err := configProvider.GetVariables(cloud, deployEnv, "", newEv2EvaluationContext())
vars, err := configProvider.GetVariables(cloud, deployEnv, "", newEv2ConfigReplacements())
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2484307

Please sign in to comment.