Skip to content

Commit

Permalink
Decouple config reading and retrieval (#1668)
Browse files Browse the repository at this point in the history
There are use cases where we need to run `GetStepConfig()` multiple times.
In such cases it is more efficient to load the files once and then resolve the
respective step configuration.
  • Loading branch information
OliverNocon authored Jun 15, 2020
1 parent 0fcf165 commit 6116fe5
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type Config struct {
Stages map[string]map[string]interface{} `json:"stages"`
Steps map[string]map[string]interface{} `json:"steps"`
Hooks map[string]*json.RawMessage `json:"hooks,omitempty"`
defaults PipelineDefaults
initialized bool
openFile func(s string) (io.ReadCloser, error)
}

Expand Down Expand Up @@ -121,19 +123,14 @@ func (c *Config) copyStepAliasConfig(stepName string, stepAliases []Alias) {
}
}

// GetStepConfig provides merged step configuration using defaults, config, if available
func (c *Config) GetStepConfig(flagValues map[string]interface{}, paramJSON string, configuration io.ReadCloser, defaults []io.ReadCloser, ignoreCustomDefaults bool, filters StepFilters, parameters []StepParameters, secrets []StepSecrets, envParameters map[string]interface{}, stageName, stepName string, stepAliases []Alias) (StepConfig, error) {
var stepConfig StepConfig
var d PipelineDefaults

// InitializeConfig prepares the config object, i.e. loading content, etc.
func (c *Config) InitializeConfig(configuration io.ReadCloser, defaults []io.ReadCloser, ignoreCustomDefaults bool) error {
if configuration != nil {
if err := c.ReadConfig(configuration); err != nil {
return StepConfig{}, errors.Wrap(err, "failed to parse custom pipeline configuration")
return errors.Wrap(err, "failed to parse custom pipeline configuration")
}
}

c.ApplyAliasConfig(parameters, secrets, filters, stageName, stepName, stepAliases)

// consider custom defaults defined in config.yml unless told otherwise
if ignoreCustomDefaults {
log.Entry().Info("Ignoring custom defaults from pipeline config")
Expand All @@ -144,21 +141,38 @@ func (c *Config) GetStepConfig(flagValues map[string]interface{}, paramJSON stri
for _, f := range c.CustomDefaults {
fc, err := c.openFile(f)
if err != nil {
return StepConfig{}, errors.Wrapf(err, "getting default '%v' failed", f)
return errors.Wrapf(err, "getting default '%v' failed", f)
}
defaults = append(defaults, fc)
}
}

if err := d.ReadPipelineDefaults(defaults); err != nil {
return StepConfig{}, errors.Wrap(err, "failed to read default configuration")
if err := c.defaults.ReadPipelineDefaults(defaults); err != nil {
return errors.Wrap(err, "failed to read default configuration")
}
c.initialized = true
return nil
}

// GetStepConfig provides merged step configuration using defaults, config, if available
func (c *Config) GetStepConfig(flagValues map[string]interface{}, paramJSON string, configuration io.ReadCloser, defaults []io.ReadCloser, ignoreCustomDefaults bool, filters StepFilters, parameters []StepParameters, secrets []StepSecrets, envParameters map[string]interface{}, stageName, stepName string, stepAliases []Alias) (StepConfig, error) {
var stepConfig StepConfig
var err error

if !c.initialized {
err = c.InitializeConfig(configuration, defaults, ignoreCustomDefaults)
if err != nil {
return StepConfig{}, err
}
}

c.ApplyAliasConfig(parameters, secrets, filters, stageName, stepName, stepAliases)

// initialize with defaults from step.yaml
stepConfig.mixInStepDefaults(parameters)

// read defaults & merge general -> steps (-> general -> steps ...)
for _, def := range d.Defaults {
for _, def := range c.defaults.Defaults {
def.ApplyAliasConfig(parameters, secrets, filters, stageName, stepName, stepAliases)
stepConfig.mixIn(def.General, filters.General)
stepConfig.mixIn(def.Steps[stepName], filters.Steps)
Expand Down

0 comments on commit 6116fe5

Please sign in to comment.