Skip to content

Commit

Permalink
make replace variable to consider variables if defined
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziopandini committed Apr 20, 2020
1 parent 4186743 commit cfc7abb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
16 changes: 6 additions & 10 deletions cmd/clusterctl/client/repository/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,10 @@ func NewComponents(provider config.Provider, configClient config.Client, rawyaml
// Inspect the yaml read from the repository for variables.
variables := inspectVariables(rawyaml)

yaml := rawyaml
var err error
if !options.SkipVariables {
// Replace variables with corresponding values read from the config
yaml, err = replaceVariables(rawyaml, variables, configClient.Variables())
if err != nil {
return nil, errors.Wrap(err, "failed to perform variable substitution")
}
// Replace variables with corresponding values read from the config
yaml, err := replaceVariables(rawyaml, variables, configClient.Variables(), options.SkipVariables)
if err != nil {
return nil, errors.Wrap(err, "failed to perform variable substitution")
}

// Transform the yaml in a list of objects, so following transformation can work on typed objects (instead of working on a string/slice of bytes)
Expand Down Expand Up @@ -351,7 +347,7 @@ func inspectVariables(data []byte) []string {
return ret
}

func replaceVariables(yaml []byte, variables []string, configVariablesClient config.VariablesClient) ([]byte, error) {
func replaceVariables(yaml []byte, variables []string, configVariablesClient config.VariablesClient, skipVariables bool) ([]byte, error) {
tmp := string(yaml)
var missingVariables []string
for _, key := range variables {
Expand All @@ -363,7 +359,7 @@ func replaceVariables(yaml []byte, variables []string, configVariablesClient con
exp := regexp.MustCompile(`\$\{\s*` + regexp.QuoteMeta(key) + `\s*\}`)
tmp = exp.ReplaceAllLiteralString(tmp, val)
}
if len(missingVariables) > 0 {
if !skipVariables && len(missingVariables) > 0 {
return nil, errors.Errorf("value for variables [%s] is not set. Please set the value using os environment variables or the clusterctl config file", strings.Join(missingVariables, ", "))
}

Expand Down
21 changes: 19 additions & 2 deletions cmd/clusterctl/client/repository/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func Test_replaceVariables(t *testing.T) {
yaml []byte
variables []string
configVariablesClient config.VariablesClient
skipVariables bool
}
tests := []struct {
name string
Expand All @@ -88,6 +89,7 @@ func Test_replaceVariables(t *testing.T) {
variables: []string{"BAR"},
configVariablesClient: test.NewFakeVariableClient().
WithVar("BAR", "bar"),
skipVariables: false,
},
want: []byte("foo bar"),
wantErr: false,
Expand All @@ -99,6 +101,7 @@ func Test_replaceVariables(t *testing.T) {
variables: []string{"BA$R"},
configVariablesClient: test.NewFakeVariableClient().
WithVar("BA$R", "bar"),
skipVariables: false,
},
want: []byte("foo bar"),
wantErr: false,
Expand All @@ -110,26 +113,40 @@ func Test_replaceVariables(t *testing.T) {
variables: []string{"BAR"},
configVariablesClient: test.NewFakeVariableClient().
WithVar("BAR", "ba$r"),
skipVariables: false,
},
want: []byte("foo ba$r"),
wantErr: false,
},
{
name: "fails for missing variables",
name: "fails for missing variables and not skip variables",
args: args{
yaml: []byte("foo ${ BAR } ${ BAZ }"),
variables: []string{"BAR", "BAZ"},
configVariablesClient: test.NewFakeVariableClient(),
skipVariables: false,
},
want: nil,
wantErr: true,
},
{
name: "pass when missing variables and skip variables",
args: args{
yaml: []byte("foo ${ BAR } ${ BAZ }"),
variables: []string{"BAR", "BAZ"},
configVariablesClient: test.NewFakeVariableClient().
WithVar("BAR", "bar"),
skipVariables: true,
},
want: []byte("foo bar ${ BAZ }"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

got, err := replaceVariables(tt.args.yaml, tt.args.variables, tt.args.configVariablesClient)
got, err := replaceVariables(tt.args.yaml, tt.args.variables, tt.args.configVariablesClient, tt.args.skipVariables)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
return
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterctl/client/repository/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewTemplate(rawYaml []byte, configVariablesClient config.VariablesClient, t
}, nil
}

yaml, err := replaceVariables(rawYaml, variables, configVariablesClient)
yaml, err := replaceVariables(rawYaml, variables, configVariablesClient, false)
if err != nil {
return nil, errors.Wrap(err, "failed to perform variable substitution")
}
Expand Down

0 comments on commit cfc7abb

Please sign in to comment.