Skip to content

Commit 2dab84b

Browse files
clusterctl-make-template-generic
1 parent f208101 commit 2dab84b

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
@@ -322,11 +322,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
322322
}
323323

324324
type templateValues struct {
325-
name string
326-
url string
327-
providerType clusterctlv1.ProviderType
328-
version string
329-
flavor string
330325
variables []string
331326
targetNamespace string
332327
yaml []byte
@@ -351,11 +346,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
351346
},
352347
},
353348
want: templateValues{
354-
name: "infra",
355-
url: "url",
356-
providerType: clusterctlv1.InfrastructureProviderType,
357-
version: "v3.0.0",
358-
flavor: "",
359349
variables: []string{"CLUSTER_NAME"}, // variable detected
360350
targetNamespace: "ns1",
361351
yaml: templateYAML("ns1", "test"), // original template modified with target namespace and variable replacement
@@ -374,11 +364,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
374364
},
375365
},
376366
want: templateValues{
377-
name: "infra",
378-
url: "url",
379-
providerType: clusterctlv1.InfrastructureProviderType,
380-
version: "v3.0.0",
381-
flavor: "",
382367
variables: []string{"CLUSTER_NAME"}, // variable detected
383368
targetNamespace: "ns1",
384369
yaml: templateYAML("ns1", "test"), // original template modified with target namespace and variable replacement
@@ -397,11 +382,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
397382
},
398383
},
399384
want: templateValues{
400-
name: "infra",
401-
url: "url",
402-
providerType: clusterctlv1.InfrastructureProviderType,
403-
version: "v3.0.0",
404-
flavor: "",
405385
variables: []string{"CLUSTER_NAME"}, // variable detected
406386
targetNamespace: "default",
407387
yaml: templateYAML("default", "test"), // original template modified with target namespace and variable replacement
@@ -418,21 +398,6 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
418398
return
419399
}
420400

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

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)