Skip to content

Commit 12ddd37

Browse files
authored
Merge pull request #2254 from fabriziopandini/clusterctl-make-template-generic
✨clusterctl: make template object more generic
2 parents 5ce39e9 + 2dab84b commit 12ddd37

File tree

5 files changed

+8
-139
lines changed

5 files changed

+8
-139
lines changed

cmd/clusterctl/pkg/client/config_test.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
324324
}
325325

326326
type templateValues struct {
327-
name string
328-
url string
329-
providerType clusterctlv1.ProviderType
330-
version string
331-
flavor string
332327
variables []string
333328
targetNamespace string
334329
yaml []byte
@@ -353,11 +348,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
353348
},
354349
},
355350
want: templateValues{
356-
name: "infra",
357-
url: "url",
358-
providerType: clusterctlv1.InfrastructureProviderType,
359-
version: "v3.0.0",
360-
flavor: "",
361351
variables: []string{"CLUSTER_NAME"}, // variable detected
362352
targetNamespace: "ns1",
363353
yaml: templateYAML("ns1", "test"), // original template modified with target namespace and variable replacement
@@ -376,11 +366,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
376366
},
377367
},
378368
want: templateValues{
379-
name: "infra",
380-
url: "url",
381-
providerType: clusterctlv1.InfrastructureProviderType,
382-
version: "v3.0.0",
383-
flavor: "",
384369
variables: []string{"CLUSTER_NAME"}, // variable detected
385370
targetNamespace: "ns1",
386371
yaml: templateYAML("ns1", "test"), // original template modified with target namespace and variable replacement
@@ -399,11 +384,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
399384
},
400385
},
401386
want: templateValues{
402-
name: "infra",
403-
url: "url",
404-
providerType: clusterctlv1.InfrastructureProviderType,
405-
version: "v3.0.0",
406-
flavor: "",
407387
variables: []string{"CLUSTER_NAME"}, // variable detected
408388
targetNamespace: "default",
409389
yaml: templateYAML("default", "test"), // original template modified with target namespace and variable replacement
@@ -420,21 +400,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
420400
return
421401
}
422402

423-
if got.Name() != tt.want.name {
424-
t.Errorf("Name() got = %v, want %v", got.Name(), tt.want.name)
425-
}
426-
if got.URL() != tt.want.url {
427-
t.Errorf("URL() got = %v, want %v", got.URL(), tt.want.url)
428-
}
429-
if got.Type() != tt.want.providerType {
430-
t.Errorf("Type() got = %v, want %v", got.Type(), tt.want.providerType)
431-
}
432-
if got.Version() != tt.want.version {
433-
t.Errorf("Version() got = %v, want %v", got.Version(), tt.want.version)
434-
}
435-
if got.Flavor() != tt.want.flavor {
436-
t.Errorf("Flavor() got = %v, want %v", got.Flavor(), tt.want.flavor)
437-
}
438403
if !reflect.DeepEqual(got.Variables(), tt.want.variables) {
439404
t.Errorf("Variables() got = %v, want %v", got.Variables(), tt.want.variables)
440405
}

cmd/clusterctl/pkg/client/repository/template.go

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@ import (
2929
// 1. Checks for all the variables in the cluster template YAML file and replace with corresponding config values
3030
// 2. Ensure all the cluster objects are deployed in the target namespace
3131
type Template interface {
32-
// configuration of the provider the template belongs to.
33-
config.Provider
34-
35-
// Version of the provider the template belongs to.
36-
Version() string
37-
38-
// Flavor implemented by the template (empty means default flavor).
39-
// A flavor is a variant of cluster template supported by the provider, like e.g. Prod, Test.
40-
Flavor() string
41-
4232
// Variables required by the template.
4333
// This value is derived by the template YAML.
4434
Variables() []string
@@ -55,9 +45,6 @@ type Template interface {
5545

5646
// template implements Template.
5747
type template struct {
58-
config.Provider
59-
version string
60-
flavor string
6148
variables []string
6249
targetNamespace string
6350
objs []unstructured.Unstructured
@@ -66,14 +53,6 @@ type template struct {
6653
// Ensures template implements the Template interface.
6754
var _ Template = &template{}
6855

69-
func (t *template) Version() string {
70-
return t.version
71-
}
72-
73-
func (t *template) Flavor() string {
74-
return t.flavor
75-
}
76-
7756
func (t *template) Variables() []string {
7857
return t.variables
7958
}
@@ -90,22 +69,12 @@ func (t *template) Yaml() ([]byte, error) {
9069
return util.FromUnstructured(t.objs)
9170
}
9271

93-
// newTemplateOptions carries the options supported by newTemplate
94-
type newTemplateOptions struct {
95-
provider config.Provider
96-
version string
97-
flavor string
98-
rawYaml []byte
99-
configVariablesClient config.VariablesClient
100-
targetNamespace string
101-
}
102-
103-
// newTemplate returns a new objects embedding a cluster template YAML file.
104-
func newTemplate(options newTemplateOptions) (*template, error) {
72+
// NewTemplate returns a new objects embedding a cluster template YAML file.
73+
func NewTemplate(rawYaml []byte, configVariablesClient config.VariablesClient, targetNamespace string) (*template, error) {
10574
// Inspect variables and replace with values from the configuration.
106-
variables := inspectVariables(options.rawYaml)
75+
variables := inspectVariables(rawYaml)
10776

108-
yaml, err := replaceVariables(options.rawYaml, variables, options.configVariablesClient)
77+
yaml, err := replaceVariables(rawYaml, variables, configVariablesClient)
10978
if err != nil {
11079
return nil, errors.Wrap(err, "failed to perform variable substitution")
11180
}
@@ -119,14 +88,11 @@ func newTemplate(options newTemplateOptions) (*template, error) {
11988
// Ensures all the template components are deployed in the target namespace (applies only to namespaced objects)
12089
// This is required in order to ensure a cluster and all the related objects are in a single namespace, that is a requirement for
12190
// the clusterctl move operation (and also for many controller reconciliation loops).
122-
objs = fixTargetNamespace(objs, options.targetNamespace)
91+
objs = fixTargetNamespace(objs, targetNamespace)
12392

12493
return &template{
125-
Provider: options.provider,
126-
version: options.version,
127-
flavor: options.flavor,
12894
variables: variables,
129-
targetNamespace: options.targetNamespace,
95+
targetNamespace: targetNamespace,
13096
objs: objs,
13197
}, nil
13298
}

cmd/clusterctl/pkg/client/repository/template_client.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,5 @@ func (c *templateClient) Get(flavor, targetNamespace string) (Template, error) {
9494
}
9595
}
9696

97-
return newTemplate(newTemplateOptions{
98-
provider: c.provider,
99-
version: version,
100-
flavor: flavor,
101-
rawYaml: rawYaml,
102-
configVariablesClient: c.configVariablesClient,
103-
targetNamespace: targetNamespace,
104-
})
97+
return NewTemplate(rawYaml, c.configVariablesClient, targetNamespace)
10598
}

cmd/clusterctl/pkg/client/repository/template_client_test.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ func Test_templates_Get(t *testing.T) {
4141
targetNamespace string
4242
}
4343
type want struct {
44-
provider config.Provider
45-
version string
46-
flavor string
4744
variables []string
4845
targetNamespace string
4946
}
@@ -70,9 +67,6 @@ func Test_templates_Get(t *testing.T) {
7067
targetNamespace: "ns1",
7168
},
7269
want: want{
73-
provider: p1,
74-
version: "v1.0",
75-
flavor: "",
7670
variables: []string{variableName},
7771
targetNamespace: "ns1",
7872
},
@@ -94,9 +88,6 @@ func Test_templates_Get(t *testing.T) {
9488
targetNamespace: "ns1",
9589
},
9690
want: want{
97-
provider: p1,
98-
version: "v1.0",
99-
flavor: "prod",
10091
variables: []string{variableName},
10192
targetNamespace: "ns1",
10293
},
@@ -130,18 +121,6 @@ func Test_templates_Get(t *testing.T) {
130121
return
131122
}
132123

133-
if got.Name() != tt.want.provider.Name() {
134-
t.Errorf("got.Name() = %v, want = %v ", got.Name(), tt.want.provider.Name())
135-
}
136-
137-
if got.Type() != tt.want.provider.Type() {
138-
t.Errorf("got.Type() = %v, want = %v ", got.Type(), tt.want.provider.Type())
139-
}
140-
141-
if got.Version() != tt.want.version {
142-
t.Errorf("got.Version() = %v, want = %v ", got.Version(), tt.want.version)
143-
}
144-
145124
if !reflect.DeepEqual(got.Variables(), tt.want.variables) {
146125
t.Errorf("got.Variables() = %v, want = %v ", got.Variables(), tt.want.variables)
147126
}

cmd/clusterctl/pkg/client/repository/template_test.go

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"reflect"
2323
"testing"
2424

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

3938
func Test_newTemplate(t *testing.T) {
40-
p1 := config.NewProvider("p1", "", clusterctlv1.BootstrapProviderType)
41-
4239
type args struct {
43-
provider config.Provider
44-
version string
45-
flavor string
4640
rawYaml []byte
4741
configVariablesClient config.VariablesClient
4842
targetNamespace string
4943
}
5044
type want struct {
51-
provider config.Provider
52-
version string
53-
flavor string
5445
variables []string
5546
targetNamespace string
5647
}
@@ -63,17 +54,11 @@ func Test_newTemplate(t *testing.T) {
6354
{
6455
name: "variable is replaced and namespace fixed",
6556
args: args{
66-
provider: p1,
67-
version: "v1.2.3",
68-
flavor: "flavor",
6957
rawYaml: templateMapYaml,
7058
configVariablesClient: test.NewFakeVariableClient().WithVar(variableName, variableValue),
7159
targetNamespace: "ns1",
7260
},
7361
want: want{
74-
provider: p1,
75-
version: "v1.2.3",
76-
flavor: "flavor",
7762
variables: []string{variableName},
7863
targetNamespace: "ns1",
7964
},
@@ -82,33 +67,14 @@ func Test_newTemplate(t *testing.T) {
8267
}
8368
for _, tt := range tests {
8469
t.Run(tt.name, func(t *testing.T) {
85-
got, err := newTemplate(newTemplateOptions{
86-
provider: tt.args.provider,
87-
version: tt.args.version,
88-
flavor: tt.args.flavor,
89-
rawYaml: tt.args.rawYaml,
90-
configVariablesClient: tt.args.configVariablesClient,
91-
targetNamespace: tt.args.targetNamespace,
92-
})
70+
got, err := NewTemplate(tt.args.rawYaml, tt.args.configVariablesClient, tt.args.targetNamespace)
9371
if (err != nil) != tt.wantErr {
9472
t.Fatalf("error = %v, wantErr %v", err, tt.wantErr)
9573
}
9674
if tt.wantErr {
9775
return
9876
}
9977

100-
if got.Name() != tt.want.provider.Name() {
101-
t.Errorf("got.Name() = %v, want = %v ", got.Name(), tt.want.provider.Name())
102-
}
103-
104-
if got.Type() != tt.want.provider.Type() {
105-
t.Errorf("got.Type() = %v, want = %v ", got.Type(), tt.want.provider.Type())
106-
}
107-
108-
if got.Version() != tt.want.version {
109-
t.Errorf("got.Version() = %v, want = %v ", got.Version(), tt.want.version)
110-
}
111-
11278
if !reflect.DeepEqual(got.Variables(), tt.want.variables) {
11379
t.Errorf("got.Variables() = %v, want = %v ", got.Variables(), tt.want.variables)
11480
}

0 commit comments

Comments
 (0)