Skip to content

Commit

Permalink
Add TemplateInput struct to reduce the number of args
Browse files Browse the repository at this point in the history
  • Loading branch information
Warren Fernandes committed Jun 10, 2020
1 parent f6082ea commit a93ff23
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 39 deletions.
8 changes: 7 additions & 1 deletion cmd/clusterctl/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 14 additions & 2 deletions cmd/clusterctl/client/cluster/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
28 changes: 14 additions & 14 deletions cmd/clusterctl/client/cluster/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand Down Expand Up @@ -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))
})
Expand Down
29 changes: 15 additions & 14 deletions cmd/clusterctl/client/repository/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion cmd/clusterctl/client/repository/template_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
14 changes: 7 additions & 7 deletions cmd/clusterctl/client/repository/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a93ff23

Please sign in to comment.