Skip to content

Commit

Permalink
Merge pull request #2935 from fabriziopandini/clusterctl-config-env-var
Browse files Browse the repository at this point in the history
🏃Clusterctl config small improvements
  • Loading branch information
k8s-ci-robot authored Apr 20, 2020
2 parents 70921f2 + cfc7abb commit 1fbef48
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/clusterctl/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *clusterctlClient) GetProviderComponents(provider string, providerType c
Version: options.Version,
TargetNamespace: options.TargetNamespace,
WatchingNamespace: options.WatchingNamespace,
SkipVariables: true,
SkipVariables: options.SkipVariables,
}
components, err := c.getComponentsByName(provider, providerType, inputOptions)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/clusterctl/client/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func Test_getComponentsByName_withEmptyVariables(t *testing.T) {
options := ComponentsOptions{
TargetNamespace: "ns1",
WatchingNamespace: "",
SkipVariables: true,
}
components, err := client.GetProviderComponents(repository1Config.Name(), repository1Config.Type(), options)
g.Expect(err).NotTo(HaveOccurred())
Expand Down
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
1 change: 1 addition & 0 deletions cmd/clusterctl/cmd/config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func runGetComponents() error {
options := client.ComponentsOptions{
TargetNamespace: cpo.targetNamespace,
WatchingNamespace: cpo.watchingNamespace,
SkipVariables: true,
}
components, err := c.GetProviderComponents(providerName, providerType, options)
if err != nil {
Expand Down

0 comments on commit 1fbef48

Please sign in to comment.