diff --git a/cmd/clusterctl/client/config.go b/cmd/clusterctl/client/config.go index ef3ba4d9b9c7..4873866ee2d3 100644 --- a/cmd/clusterctl/client/config.go +++ b/cmd/clusterctl/client/config.go @@ -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 { diff --git a/cmd/clusterctl/client/config_test.go b/cmd/clusterctl/client/config_test.go index 228bc92129da..212fe0be1a86 100644 --- a/cmd/clusterctl/client/config_test.go +++ b/cmd/clusterctl/client/config_test.go @@ -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()) diff --git a/cmd/clusterctl/client/repository/components.go b/cmd/clusterctl/client/repository/components.go index 189036db92dc..b7d5a565f97f 100644 --- a/cmd/clusterctl/client/repository/components.go +++ b/cmd/clusterctl/client/repository/components.go @@ -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) @@ -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 { @@ -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, ", ")) } diff --git a/cmd/clusterctl/client/repository/components_test.go b/cmd/clusterctl/client/repository/components_test.go index c0345cb47203..44fb5268fc3a 100644 --- a/cmd/clusterctl/client/repository/components_test.go +++ b/cmd/clusterctl/client/repository/components_test.go @@ -74,6 +74,7 @@ func Test_replaceVariables(t *testing.T) { yaml []byte variables []string configVariablesClient config.VariablesClient + skipVariables bool } tests := []struct { name string @@ -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, @@ -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, @@ -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 diff --git a/cmd/clusterctl/client/repository/template.go b/cmd/clusterctl/client/repository/template.go index f3c7f27c8b1f..08d08f49d265 100644 --- a/cmd/clusterctl/client/repository/template.go +++ b/cmd/clusterctl/client/repository/template.go @@ -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") } diff --git a/cmd/clusterctl/cmd/config_provider.go b/cmd/clusterctl/cmd/config_provider.go index 8f84cf260ffd..ec12312c5eac 100644 --- a/cmd/clusterctl/cmd/config_provider.go +++ b/cmd/clusterctl/cmd/config_provider.go @@ -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 {