diff --git a/cmd/clusterctl/client/client_test.go b/cmd/clusterctl/client/client_test.go index f762aa4a31b2..59511367ced2 100644 --- a/cmd/clusterctl/client/client_test.go +++ b/cmd/clusterctl/client/client_test.go @@ -418,7 +418,13 @@ func (f *fakeTemplateClient) Get(flavor, targetNamespace string, listVariablesOn if err != nil { return nil, err } - return repository.NewTemplate(content, f.configVariablesClient, f.processor, targetNamespace, listVariablesOnly) + return repository.NewTemplate(repository.TemplateInput{ + RawArtifact: content, + ConfigVariablesClient: f.configVariablesClient, + Processor: f.processor, + TargetNamespace: targetNamespace, + ListVariablesOnly: listVariablesOnly, + }) } // fakeMetadataClient provides a super simple MetadataClient (e.g. without support for local overrides/embedded metadata) diff --git a/cmd/clusterctl/client/cluster/template.go b/cmd/clusterctl/client/cluster/template.go index fd72d2a42317..6d155b0fc2e6 100644 --- a/cmd/clusterctl/client/cluster/template.go +++ b/cmd/clusterctl/client/cluster/template.go @@ -99,7 +99,13 @@ func (t *templateClient) GetFromConfigMap(configMapNamespace, configMapName, con return nil, errors.Errorf("the ConfigMap %s/%s does not have the %q data key", configMapNamespace, configMapName, configMapDataKey) } - return repository.NewTemplate([]byte(data), t.configClient.Variables(), t.processor, targetNamespace, listVariablesOnly) + return repository.NewTemplate(repository.TemplateInput{ + RawArtifact: []byte(data), + ConfigVariablesClient: t.configClient.Variables(), + Processor: t.processor, + TargetNamespace: targetNamespace, + ListVariablesOnly: listVariablesOnly, + }) } func (t *templateClient) GetFromURL(templateURL, targetNamespace string, listVariablesOnly bool) (repository.Template, error) { @@ -112,7 +118,13 @@ func (t *templateClient) GetFromURL(templateURL, targetNamespace string, listVar return nil, errors.Wrapf(err, "invalid GetFromURL operation") } - return repository.NewTemplate(content, t.configClient.Variables(), t.processor, targetNamespace, listVariablesOnly) + return repository.NewTemplate(repository.TemplateInput{ + RawArtifact: content, + ConfigVariablesClient: t.configClient.Variables(), + Processor: t.processor, + TargetNamespace: targetNamespace, + ListVariablesOnly: listVariablesOnly, + }) } func (t *templateClient) getURLContent(templateURL string) ([]byte, error) { diff --git a/cmd/clusterctl/client/cluster/template_test.go b/cmd/clusterctl/client/cluster/template_test.go index 628961596235..c71d2ea0e0f7 100644 --- a/cmd/clusterctl/client/cluster/template_test.go +++ b/cmd/clusterctl/client/cluster/template_test.go @@ -143,13 +143,13 @@ func Test_templateClient_GetFromConfigMap(t *testing.T) { } g.Expect(err).NotTo(HaveOccurred()) - wantTemplate, err := repository.NewTemplate( - []byte(tt.want), - configClient.Variables(), - processor, - tt.args.targetNamespace, - tt.args.listVariablesOnly, - ) + wantTemplate, err := repository.NewTemplate(repository.TemplateInput{ + RawArtifact: []byte(tt.want), + ConfigVariablesClient: configClient.Variables(), + Processor: processor, + TargetNamespace: tt.args.targetNamespace, + ListVariablesOnly: tt.args.listVariablesOnly, + }) g.Expect(err).NotTo(HaveOccurred()) g.Expect(got).To(Equal(wantTemplate)) }) @@ -360,13 +360,13 @@ func Test_templateClient_GetFromURL(t *testing.T) { g.Expect(err).NotTo(HaveOccurred()) - wantTemplate, err := repository.NewTemplate( - []byte(tt.want), - configClient.Variables(), - processor, - tt.args.targetNamespace, - tt.args.listVariablesOnly, - ) + wantTemplate, err := repository.NewTemplate(repository.TemplateInput{ + RawArtifact: []byte(tt.want), + ConfigVariablesClient: configClient.Variables(), + Processor: processor, + TargetNamespace: tt.args.targetNamespace, + ListVariablesOnly: tt.args.listVariablesOnly, + }) g.Expect(err).NotTo(HaveOccurred()) g.Expect(got).To(Equal(wantTemplate)) }) diff --git a/cmd/clusterctl/client/repository/template.go b/cmd/clusterctl/client/repository/template.go index 5b61e52802ac..4b1801cca1c2 100644 --- a/cmd/clusterctl/client/repository/template.go +++ b/cmd/clusterctl/client/repository/template.go @@ -70,28 +70,29 @@ func (t *template) Yaml() ([]byte, error) { return utilyaml.FromUnstructured(t.objs) } +type TemplateInput struct { + RawArtifact []byte + ConfigVariablesClient config.VariablesClient + Processor yaml.Processor + TargetNamespace string + ListVariablesOnly bool +} + // NewTemplate returns a new objects embedding a cluster template YAML file. -// TODO: Refactor to reduce the number of args passed in. -func NewTemplate( - rawArtifact []byte, - configVariablesClient config.VariablesClient, - processor yaml.Processor, - targetNamespace string, - listVariablesOnly bool, -) (*template, error) { - variables, err := processor.GetVariables(rawArtifact) +func NewTemplate(input TemplateInput) (*template, error) { + variables, err := input.Processor.GetVariables(input.RawArtifact) if err != nil { return nil, err } - if listVariablesOnly { + if input.ListVariablesOnly { return &template{ variables: variables, - targetNamespace: targetNamespace, + targetNamespace: input.TargetNamespace, }, nil } - processedYaml, err := processor.Process(rawArtifact, configVariablesClient) + processedYaml, err := input.Processor.Process(input.RawArtifact, input.ConfigVariablesClient) if err != nil { return nil, err } @@ -105,11 +106,11 @@ func NewTemplate( // Ensures all the template components are deployed in the target namespace (applies only to namespaced objects) // This is required in order to ensure a cluster and all the related objects are in a single namespace, that is a requirement for // the clusterctl move operation (and also for many controller reconciliation loops). - objs = fixTargetNamespace(objs, targetNamespace) + objs = fixTargetNamespace(objs, input.TargetNamespace) return &template{ variables: variables, - targetNamespace: targetNamespace, + targetNamespace: input.TargetNamespace, objs: objs, }, nil } diff --git a/cmd/clusterctl/client/repository/template_client.go b/cmd/clusterctl/client/repository/template_client.go index a90dbe0d435d..a8d14ff3b913 100644 --- a/cmd/clusterctl/client/repository/template_client.go +++ b/cmd/clusterctl/client/repository/template_client.go @@ -95,5 +95,5 @@ func (c *templateClient) Get(flavor, targetNamespace string, listVariablesOnly b log.V(1).Info("Using", "Override", name, "Provider", c.provider.ManifestLabel(), "Version", version) } - return NewTemplate(rawArtifact, c.configVariablesClient, c.processor, targetNamespace, listVariablesOnly) + return NewTemplate(TemplateInput{rawArtifact, c.configVariablesClient, c.processor, targetNamespace, listVariablesOnly}) } diff --git a/cmd/clusterctl/client/repository/template_test.go b/cmd/clusterctl/client/repository/template_test.go index 1af4263f0a2d..9ce045c6bb81 100644 --- a/cmd/clusterctl/client/repository/template_test.go +++ b/cmd/clusterctl/client/repository/template_test.go @@ -89,13 +89,13 @@ func Test_newTemplate(t *testing.T) { t.Run(tt.name, func(t *testing.T) { g := NewWithT(t) - got, err := NewTemplate( - tt.args.rawYaml, - tt.args.configVariablesClient, - tt.args.processor, - tt.args.targetNamespace, - tt.args.listVariablesOnly, - ) + got, err := NewTemplate(TemplateInput{ + RawArtifact: tt.args.rawYaml, + ConfigVariablesClient: tt.args.configVariablesClient, + Processor: tt.args.processor, + TargetNamespace: tt.args.targetNamespace, + ListVariablesOnly: tt.args.listVariablesOnly, + }) if tt.wantErr { g.Expect(err).To(HaveOccurred()) return