Skip to content

Commit

Permalink
make template object more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziopandini committed Feb 4, 2020
1 parent f208101 commit 9ea30e1
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 116 deletions.
35 changes: 0 additions & 35 deletions cmd/clusterctl/pkg/client/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
}

type templateValues struct {
name string
url string
providerType clusterctlv1.ProviderType
version string
flavor string
variables []string
targetNamespace string
yaml []byte
Expand All @@ -351,11 +346,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
},
},
want: templateValues{
name: "infra",
url: "url",
providerType: clusterctlv1.InfrastructureProviderType,
version: "v3.0.0",
flavor: "",
variables: []string{"CLUSTER_NAME"}, // variable detected
targetNamespace: "ns1",
yaml: templateYAML("ns1", "test"), // original template modified with target namespace and variable replacement
Expand All @@ -374,11 +364,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
},
},
want: templateValues{
name: "infra",
url: "url",
providerType: clusterctlv1.InfrastructureProviderType,
version: "v3.0.0",
flavor: "",
variables: []string{"CLUSTER_NAME"}, // variable detected
targetNamespace: "ns1",
yaml: templateYAML("ns1", "test"), // original template modified with target namespace and variable replacement
Expand All @@ -397,11 +382,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
},
},
want: templateValues{
name: "infra",
url: "url",
providerType: clusterctlv1.InfrastructureProviderType,
version: "v3.0.0",
flavor: "",
variables: []string{"CLUSTER_NAME"}, // variable detected
targetNamespace: "default",
yaml: templateYAML("default", "test"), // original template modified with target namespace and variable replacement
Expand All @@ -418,21 +398,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
return
}

if got.Name() != tt.want.name {
t.Errorf("Name() got = %v, want %v", got.Name(), tt.want.name)
}
if got.URL() != tt.want.url {
t.Errorf("URL() got = %v, want %v", got.URL(), tt.want.url)
}
if got.Type() != tt.want.providerType {
t.Errorf("Type() got = %v, want %v", got.Type(), tt.want.providerType)
}
if got.Version() != tt.want.version {
t.Errorf("Version() got = %v, want %v", got.Version(), tt.want.version)
}
if got.Flavor() != tt.want.flavor {
t.Errorf("Flavor() got = %v, want %v", got.Flavor(), tt.want.flavor)
}
if !reflect.DeepEqual(got.Variables(), tt.want.variables) {
t.Errorf("Variables() got = %v, want %v", got.Variables(), tt.want.variables)
}
Expand Down
36 changes: 9 additions & 27 deletions cmd/clusterctl/pkg/client/repository/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ import (
// 1. Checks for all the variables in the cluster template YAML file and replace with corresponding config values
// 2. Ensure all the cluster objects are deployed in the target namespace
type Template interface {
// configuration of the provider the template belongs to.
config.Provider

// Version of the provider the template belongs to.
Version() string

// Flavor implemented by the template (empty means default flavor).
// A flavor is a variant of cluster template supported by the provider, like e.g. Prod, Test.
Flavor() string

// Variables required by the template.
// This value is derived by the template YAML.
Variables() []string
Expand All @@ -55,9 +45,6 @@ type Template interface {

// template implements Template.
type template struct {
config.Provider
version string
flavor string
variables []string
targetNamespace string
objs []unstructured.Unstructured
Expand All @@ -66,14 +53,6 @@ type template struct {
// Ensures template implements the Template interface.
var _ Template = &template{}

func (t *template) Version() string {
return t.version
}

func (t *template) Flavor() string {
return t.flavor
}

func (t *template) Variables() []string {
return t.variables
}
Expand All @@ -92,9 +71,6 @@ func (t *template) Yaml() ([]byte, error) {

// newTemplateOptions carries the options supported by newTemplate
type newTemplateOptions struct {
provider config.Provider
version string
flavor string
rawYaml []byte
configVariablesClient config.VariablesClient
targetNamespace string
Expand Down Expand Up @@ -122,11 +98,17 @@ func newTemplate(options newTemplateOptions) (*template, error) {
objs = fixTargetNamespace(objs, options.targetNamespace)

return &template{
Provider: options.provider,
version: options.version,
flavor: options.flavor,
variables: variables,
targetNamespace: options.targetNamespace,
objs: objs,
}, nil
}

// NewTemplate returns a new template object
func NewTemplate(yaml []byte, configVariablesClient config.VariablesClient, targetNamespace string) (*template, error) {
return newTemplate(newTemplateOptions{
rawYaml: yaml,
configVariablesClient: configVariablesClient,
targetNamespace: targetNamespace,
})
}
3 changes: 0 additions & 3 deletions cmd/clusterctl/pkg/client/repository/template_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ func (c *templateClient) Get(flavor, targetNamespace string) (Template, error) {
}

return newTemplate(newTemplateOptions{
provider: c.provider,
version: version,
flavor: flavor,
rawYaml: rawYaml,
configVariablesClient: c.configVariablesClient,
targetNamespace: targetNamespace,
Expand Down
21 changes: 0 additions & 21 deletions cmd/clusterctl/pkg/client/repository/template_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ func Test_templates_Get(t *testing.T) {
targetNamespace string
}
type want struct {
provider config.Provider
version string
flavor string
variables []string
targetNamespace string
}
Expand All @@ -70,9 +67,6 @@ func Test_templates_Get(t *testing.T) {
targetNamespace: "ns1",
},
want: want{
provider: p1,
version: "v1.0",
flavor: "",
variables: []string{variableName},
targetNamespace: "ns1",
},
Expand All @@ -94,9 +88,6 @@ func Test_templates_Get(t *testing.T) {
targetNamespace: "ns1",
},
want: want{
provider: p1,
version: "v1.0",
flavor: "prod",
variables: []string{variableName},
targetNamespace: "ns1",
},
Expand Down Expand Up @@ -130,18 +121,6 @@ func Test_templates_Get(t *testing.T) {
return
}

if got.Name() != tt.want.provider.Name() {
t.Errorf("got.Name() = %v, want = %v ", got.Name(), tt.want.provider.Name())
}

if got.Type() != tt.want.provider.Type() {
t.Errorf("got.Type() = %v, want = %v ", got.Type(), tt.want.provider.Type())
}

if got.Version() != tt.want.version {
t.Errorf("got.Version() = %v, want = %v ", got.Version(), tt.want.version)
}

if !reflect.DeepEqual(got.Variables(), tt.want.variables) {
t.Errorf("got.Variables() = %v, want = %v ", got.Variables(), tt.want.variables)
}
Expand Down
30 changes: 0 additions & 30 deletions cmd/clusterctl/pkg/client/repository/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"reflect"
"testing"

clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/client/config"
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/internal/test"
)
Expand All @@ -37,20 +36,12 @@ var templateMapYaml = []byte("apiVersion: v1\n" +
" name: manager")

func Test_newTemplate(t *testing.T) {
p1 := config.NewProvider("p1", "", clusterctlv1.BootstrapProviderType)

type args struct {
provider config.Provider
version string
flavor string
rawYaml []byte
configVariablesClient config.VariablesClient
targetNamespace string
}
type want struct {
provider config.Provider
version string
flavor string
variables []string
targetNamespace string
}
Expand All @@ -63,17 +54,11 @@ func Test_newTemplate(t *testing.T) {
{
name: "variable is replaced and namespace fixed",
args: args{
provider: p1,
version: "v1.2.3",
flavor: "flavor",
rawYaml: templateMapYaml,
configVariablesClient: test.NewFakeVariableClient().WithVar(variableName, variableValue),
targetNamespace: "ns1",
},
want: want{
provider: p1,
version: "v1.2.3",
flavor: "flavor",
variables: []string{variableName},
targetNamespace: "ns1",
},
Expand All @@ -83,9 +68,6 @@ func Test_newTemplate(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := newTemplate(newTemplateOptions{
provider: tt.args.provider,
version: tt.args.version,
flavor: tt.args.flavor,
rawYaml: tt.args.rawYaml,
configVariablesClient: tt.args.configVariablesClient,
targetNamespace: tt.args.targetNamespace,
Expand All @@ -97,18 +79,6 @@ func Test_newTemplate(t *testing.T) {
return
}

if got.Name() != tt.want.provider.Name() {
t.Errorf("got.Name() = %v, want = %v ", got.Name(), tt.want.provider.Name())
}

if got.Type() != tt.want.provider.Type() {
t.Errorf("got.Type() = %v, want = %v ", got.Type(), tt.want.provider.Type())
}

if got.Version() != tt.want.version {
t.Errorf("got.Version() = %v, want = %v ", got.Version(), tt.want.version)
}

if !reflect.DeepEqual(got.Variables(), tt.want.variables) {
t.Errorf("got.Variables() = %v, want = %v ", got.Variables(), tt.want.variables)
}
Expand Down

0 comments on commit 9ea30e1

Please sign in to comment.