Skip to content

Commit 6f038c5

Browse files
yiannistriAlinaGoagafoot
authored
Add list of known CAPI providers (#174)
* Add known providers * Simplify filter by provider * Return an error if provider is not recognised * Adjust acceptance tests to new template providers * Adjust acceptance tests to new template providers - 2 * Code review changes * Remove empty strings from provider dropdown Co-authored-by: AlinaGoaga <[email protected]> Co-authored-by: Simon Howe <[email protected]>
1 parent e35a2b7 commit 6f038c5

File tree

6 files changed

+152
-51
lines changed

6 files changed

+152
-51
lines changed

cmd/capi-server/pkg/server/server.go

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ import (
2929
"sigs.k8s.io/controller-runtime/pkg/client"
3030
)
3131

32+
var providers = map[string]string{
33+
"AWSCluster": "aws",
34+
"AWSManagedCluster": "aws",
35+
"AzureCluster": "azure",
36+
"AzureManagedCluster": "azure",
37+
"DOCluster": "digitalocean",
38+
"DockerCluster": "docker",
39+
"GCPCluster": "gcp",
40+
"OpenStackCluster": "openstack",
41+
"PacketCluster": "packet",
42+
"VSphereCluster": "vsphere",
43+
}
44+
3245
type server struct {
3346
log logr.Logger
3447
library templates.Library
@@ -56,13 +69,26 @@ func (s *server) ListTemplates(ctx context.Context, msg *capiv1_proto.ListTempla
5669
}
5770

5871
if msg.Provider != "" {
72+
if !isProviderRecognised(msg.Provider) {
73+
return nil, fmt.Errorf("provider %q is not recognised", msg.Provider)
74+
}
75+
5976
templates = filterTemplatesByProvider(templates, msg.Provider)
6077
}
6178

6279
sort.Slice(templates, func(i, j int) bool { return templates[i].Name < templates[j].Name })
6380
return &capiv1_proto.ListTemplatesResponse{Templates: templates, Total: int32(len(tl))}, err
6481
}
6582

83+
func isProviderRecognised(provider string) bool {
84+
for _, p := range providers {
85+
if strings.EqualFold(provider, p) {
86+
return true
87+
}
88+
}
89+
return false
90+
}
91+
6692
func getProvider(t *capiv1.CAPITemplate) string {
6793
meta, err := capi.ParseTemplateMeta(t)
6894

@@ -71,43 +97,26 @@ func getProvider(t *capiv1.CAPITemplate) string {
7197
}
7298

7399
for _, obj := range meta.Objects {
74-
switch obj.Kind {
75-
case "AWSCluster", "AWSManagedCluster":
76-
return "AWSCluster"
77-
case "AzureCluster", "VSphereCluster":
78-
return obj.Kind
100+
if p, ok := providers[obj.Kind]; ok {
101+
return p
79102
}
80103
}
81104

82-
return "Generic"
105+
return ""
83106
}
84107

85108
func filterTemplatesByProvider(tl []*capiv1_proto.Template, provider string) []*capiv1_proto.Template {
86109
templates := []*capiv1_proto.Template{}
87110

88111
for _, t := range tl {
89-
providerKind := formatProviderName(provider)
90-
91-
if t.Provider == providerKind {
112+
if strings.EqualFold(t.Provider, provider) {
92113
templates = append(templates, t)
93114
}
94115
}
95116

96117
return templates
97118
}
98119

99-
func formatProviderName(provider string) string {
100-
switch name := provider; strings.ToLower(name) {
101-
case "aws":
102-
return "AWSCluster"
103-
case "azure":
104-
return "AzureCluster"
105-
case "vsphere":
106-
return "VSphereCluster"
107-
}
108-
return ""
109-
}
110-
111120
func (s *server) GetTemplate(ctx context.Context, msg *capiv1_proto.GetTemplateRequest) (*capiv1_proto.GetTemplateResponse, error) {
112121
tm, err := s.library.Get(ctx, msg.TemplateName)
113122
if err != nil {

cmd/capi-server/pkg/server/server_test.go

Lines changed: 107 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestListTemplates(t *testing.T) {
5959
{
6060
Name: "cluster-template-1",
6161
Description: "this is test template 1",
62-
Provider: "AWSCluster",
62+
Provider: "aws",
6363
Objects: []*capiv1_protos.TemplateObject{
6464
{
6565
Name: string("${CLUSTER_NAME}"),
@@ -89,7 +89,7 @@ func TestListTemplates(t *testing.T) {
8989
{
9090
Name: "cluster-template-1",
9191
Description: "this is test template 1",
92-
Provider: "Generic",
92+
Provider: "",
9393
Objects: []*capiv1_protos.TemplateObject{
9494
{
9595
Name: string("${CLUSTER_NAME}"),
@@ -109,7 +109,7 @@ func TestListTemplates(t *testing.T) {
109109
{
110110
Name: "cluster-template-2",
111111
Description: "this is test template 2",
112-
Provider: "Generic",
112+
Provider: "",
113113
Objects: []*capiv1_protos.TemplateObject{
114114
{
115115
Name: string("${CLUSTER_NAME}"),
@@ -155,12 +155,11 @@ func TestListTemplates(t *testing.T) {
155155

156156
func TestListTemplates_FilterByProvider(t *testing.T) {
157157
testCases := []struct {
158-
name string
159-
provider string
160-
clusterState []runtime.Object
161-
expected []*capiv1_protos.Template
162-
err error
163-
expectedErrorStr string
158+
name string
159+
provider string
160+
clusterState []runtime.Object
161+
expected []*capiv1_protos.Template
162+
err error
164163
}{
165164
{
166165
name: "Provider name with upper case letters",
@@ -175,7 +174,7 @@ func TestListTemplates_FilterByProvider(t *testing.T) {
175174
{
176175
Name: "cluster-template-2",
177176
Description: "this is test template 2",
178-
Provider: "AWSCluster",
177+
Provider: "aws",
179178
Objects: []*capiv1_protos.TemplateObject{
180179
{
181180
Name: string("${CLUSTER_NAME}"),
@@ -206,7 +205,7 @@ func TestListTemplates_FilterByProvider(t *testing.T) {
206205
{
207206
Name: "cluster-template-2",
208207
Description: "this is test template 2",
209-
Provider: "AWSCluster",
208+
Provider: "aws",
210209
Objects: []*capiv1_protos.TemplateObject{
211210
{
212211
Name: string("${CLUSTER_NAME}"),
@@ -235,6 +234,17 @@ func TestListTemplates_FilterByProvider(t *testing.T) {
235234
},
236235
expected: []*capiv1_protos.Template{},
237236
},
237+
{
238+
name: "Provider name not recognised",
239+
provider: "foo",
240+
clusterState: []runtime.Object{
241+
makeTemplateConfigMap("template2", makeTemplateWithProvider(t, "AWSCluster", func(ct *capiv1.CAPITemplate) {
242+
ct.ObjectMeta.Name = "cluster-template-2"
243+
ct.Spec.Description = "this is test template 2"
244+
}), "template1", makeTemplate(t)),
245+
},
246+
err: fmt.Errorf("provider %q is not recognised", "foo"),
247+
},
238248
}
239249

240250
for _, tt := range testCases {
@@ -281,7 +291,7 @@ func TestGetTemplate(t *testing.T) {
281291
expected: &capiv1_protos.Template{
282292
Name: "cluster-template-1",
283293
Description: "this is test template 1",
284-
Provider: "Generic",
294+
Provider: "",
285295
Objects: []*capiv1_protos.TemplateObject{
286296
{
287297
Name: string("${CLUSTER_NAME}"),
@@ -873,7 +883,7 @@ func TestGetProvider(t *testing.T) {
873883
},
874884
},
875885
},
876-
provider: "AWSCluster",
886+
provider: "aws",
877887
},
878888
{
879889
name: "AWSManagedCluster",
@@ -889,7 +899,7 @@ func TestGetProvider(t *testing.T) {
889899
},
890900
},
891901
},
892-
provider: "AWSCluster",
902+
provider: "aws",
893903
},
894904
{
895905
name: "AzureCluster",
@@ -905,7 +915,87 @@ func TestGetProvider(t *testing.T) {
905915
},
906916
},
907917
},
908-
provider: "AzureCluster",
918+
provider: "azure",
919+
},
920+
{
921+
name: "AzureManagedCluster",
922+
template: &capiv1.CAPITemplate{
923+
Spec: capiv1.CAPITemplateSpec{
924+
ResourceTemplates: []capiv1.CAPIResourceTemplate{
925+
{
926+
RawExtension: rawExtension(`{
927+
"apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha4",
928+
"kind": "AzureManagedCluster"
929+
}`),
930+
},
931+
},
932+
},
933+
},
934+
provider: "azure",
935+
},
936+
{
937+
name: "DOCluster",
938+
template: &capiv1.CAPITemplate{
939+
Spec: capiv1.CAPITemplateSpec{
940+
ResourceTemplates: []capiv1.CAPIResourceTemplate{
941+
{
942+
RawExtension: rawExtension(`{
943+
"apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha4",
944+
"kind": "DOCluster"
945+
}`),
946+
},
947+
},
948+
},
949+
},
950+
provider: "digitalocean",
951+
},
952+
{
953+
name: "GCPCluster",
954+
template: &capiv1.CAPITemplate{
955+
Spec: capiv1.CAPITemplateSpec{
956+
ResourceTemplates: []capiv1.CAPIResourceTemplate{
957+
{
958+
RawExtension: rawExtension(`{
959+
"apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha4",
960+
"kind": "GCPCluster"
961+
}`),
962+
},
963+
},
964+
},
965+
},
966+
provider: "gcp",
967+
},
968+
{
969+
name: "OpenStackCluster",
970+
template: &capiv1.CAPITemplate{
971+
Spec: capiv1.CAPITemplateSpec{
972+
ResourceTemplates: []capiv1.CAPIResourceTemplate{
973+
{
974+
RawExtension: rawExtension(`{
975+
"apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha4",
976+
"kind": "OpenStackCluster"
977+
}`),
978+
},
979+
},
980+
},
981+
},
982+
provider: "openstack",
983+
},
984+
{
985+
name: "PacketCluster",
986+
template: &capiv1.CAPITemplate{
987+
Spec: capiv1.CAPITemplateSpec{
988+
ResourceTemplates: []capiv1.CAPIResourceTemplate{
989+
{
990+
RawExtension: rawExtension(`{
991+
"apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha4",
992+
"kind": "PacketCluster"
993+
}`),
994+
},
995+
},
996+
},
997+
},
998+
provider: "packet",
909999
},
9101000
{
9111001
name: "VSphereCluster",
@@ -921,7 +1011,7 @@ func TestGetProvider(t *testing.T) {
9211011
},
9221012
},
9231013
},
924-
provider: "VSphereCluster",
1014+
provider: "vsphere",
9251015
},
9261016
{
9271017
name: "FooCluster",
@@ -937,7 +1027,7 @@ func TestGetProvider(t *testing.T) {
9371027
},
9381028
},
9391029
},
940-
provider: "Generic",
1030+
provider: "",
9411031
},
9421032
}
9431033

cmd/capi-server/pkg/server/template_response_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestToTemplate(t *testing.T) {
2121
name: "empty",
2222
value: "",
2323
expected: &capiv1_protos.Template{
24-
Provider: "Generic",
24+
Provider: "",
2525
},
2626
},
2727
{
@@ -34,7 +34,7 @@ metadata:
3434
`,
3535
expected: &capiv1_protos.Template{
3636
Name: "foo",
37-
Provider: "Generic",
37+
Provider: "",
3838
},
3939
},
4040
{
@@ -60,7 +60,7 @@ metadata:
6060
expected: &capiv1_protos.Template{
6161
Name: "cluster-template-1",
6262
Description: "this is test template 1",
63-
Provider: "Generic",
63+
Provider: "",
6464
Objects: []*capiv1_protos.TemplateObject{
6565
{
6666
ApiVersion: "fooversion",

test/acceptance/test/mccp_templates.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ func DescribeMCCPTemplates(mccpTestRunner MCCPTestRunner) {
155155
By("And templates can be filtered by provider - table view", func() {
156156
// Select cluster provider by selecting from the popup list
157157
Expect(templatesPage.TemplateProvider.Click()).To(Succeed())
158-
Expect(templatesPage.SelectProvider("Generic").Click()).To(Succeed())
158+
Expect(templatesPage.SelectProvider("aws").Click()).To(Succeed())
159159

160160
rowCount, _ := templatesPage.TemplatesTable.Count()
161-
Eventually(rowCount).Should(Equal(capdTemplateCount), "The number of DockerCluster provider template tiles rendered should be equal to number of CAPD templates created")
161+
Eventually(rowCount).Should(Equal(4), "The number of selected template tiles rendered should be equal to number of aws templates created")
162162

163163
Expect(templatesPage.TemplateProvider.Click()).To(Succeed())
164164
Expect(templatesPage.TemplateProvider.SendKeys("\uE003")).To(Succeed()) // sending back space key
@@ -184,19 +184,19 @@ func DescribeMCCPTemplates(mccpTestRunner MCCPTestRunner) {
184184
By("And templates can be filtered by provider - grid view", func() {
185185
// Select cluster provider by selecting from the popup list
186186
Expect(templatesPage.TemplateProvider.Click()).To(Succeed())
187-
Expect(templatesPage.SelectProvider("AWSCluster").Click()).To(Succeed())
187+
Expect(templatesPage.SelectProvider("aws").Click()).To(Succeed())
188188

189189
tileCount, _ := templatesPage.TemplateTiles.Count()
190-
Eventually(tileCount).Should(Equal(awsTemplateCount+eksFargateTemplateCount), "The number of AWSCluster provider template tiles rendered should be equal to number of AWS templates created")
190+
Eventually(tileCount).Should(Equal(awsTemplateCount+eksFargateTemplateCount), "The number of aws provider template tiles rendered should be equal to number of aws templates created")
191191

192192
// Select cluster provider by typing the provider name
193193
Expect(templatesPage.TemplateProvider.Click()).To(Succeed())
194194
Expect(templatesPage.TemplateProvider.SendKeys("\uE003")).To(Succeed()) // sending back space key
195-
Expect(templatesPage.TemplateProvider.SendKeys("AzureCluster")).To(Succeed())
195+
Expect(templatesPage.TemplateProvider.SendKeys("azure")).To(Succeed())
196196
Expect(templatesPage.TemplateProviderPopup.At(0).Click()).To(Succeed())
197197

198198
tileCount, _ = templatesPage.TemplateTiles.Count()
199-
Eventually(tileCount).Should(Equal(azureTemplateCount), "The number of AzureCluster provider template tiles rendered should be equal to number of Azure templates created")
199+
Eventually(tileCount).Should(Equal(azureTemplateCount), "The number of azure provider template tiles rendered should be equal to number of azure templates created")
200200
})
201201
})
202202
})
@@ -237,7 +237,7 @@ func DescribeMCCPTemplates(mccpTestRunner MCCPTestRunner) {
237237
By("And I should choose a template - table view", func() {
238238

239239
templateRow := pages.GetTemplateRow(webDriver, "cluster-template-10")
240-
Eventually(templateRow.Provider).Should(MatchText("Generic"))
240+
Eventually(templateRow.Provider).Should(MatchText(""))
241241
Eventually(templateRow.Description).Should(MatchText("This is test template 10"))
242242
Expect(templateRow.CreateTemplate).Should(BeFound())
243243
Expect(templateRow.CreateTemplate.Click()).To(Succeed())

ui-cra/src/components/Templates/Card.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ const TemplateCard: FC<{ template: Template }> = ({ template }) => {
3636
};
3737
const getTile = () => {
3838
switch (template.provider) {
39-
case 'AWSCluster':
39+
case 'aws':
4040
return <EKS />;
41-
case 'GKECluster':
41+
case 'gcp':
4242
return <GKE />;
4343
default:
4444
return <Generic />;

0 commit comments

Comments
 (0)