Skip to content

Commit 1b75b10

Browse files
migrate more test to test CRD
1 parent 607d267 commit 1b75b10

File tree

6 files changed

+272
-78
lines changed

6 files changed

+272
-78
lines changed

internal/controllers/topology/cluster/cluster_controller_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -547,19 +547,19 @@ func setupTestEnvForIntegrationTests(ns *corev1.Namespace) (func() error, error)
547547
// Cluster given a skeletal Cluster object and a ClusterClass. The objects include:
548548

549549
// 1) Templates for Machine, Cluster, ControlPlane and Bootstrap.
550-
infrastructureMachineTemplate1 := builder.InfrastructureMachineTemplate(ns.Name, infrastructureMachineTemplateName1).Build()
551-
infrastructureMachineTemplate2 := builder.InfrastructureMachineTemplate(ns.Name, infrastructureMachineTemplateName2).
550+
infrastructureMachineTemplate1 := builder.TestInfrastructureMachineTemplate(ns.Name, infrastructureMachineTemplateName1).Build()
551+
infrastructureMachineTemplate2 := builder.TestInfrastructureMachineTemplate(ns.Name, infrastructureMachineTemplateName2).
552552
WithSpecFields(map[string]interface{}{"spec.template.spec.fakeSetting": true}).
553553
Build()
554-
infrastructureClusterTemplate1 := builder.InfrastructureClusterTemplate(ns.Name, "infraclustertemplate1").
554+
infrastructureClusterTemplate1 := builder.TestInfrastructureClusterTemplate(ns.Name, "infraclustertemplate1").
555555
Build()
556-
infrastructureClusterTemplate2 := builder.InfrastructureClusterTemplate(ns.Name, "infraclustertemplate2").
556+
infrastructureClusterTemplate2 := builder.TestInfrastructureClusterTemplate(ns.Name, "infraclustertemplate2").
557557
WithSpecFields(map[string]interface{}{"spec.template.spec.alteredSetting": true}).
558558
Build()
559-
controlPlaneTemplate := builder.ControlPlaneTemplate(ns.Name, "cp1").
559+
controlPlaneTemplate := builder.TestControlPlaneTemplate(ns.Name, "cp1").
560560
WithInfrastructureMachineTemplate(infrastructureMachineTemplate1).
561561
Build()
562-
bootstrapTemplate := builder.BootstrapTemplate(ns.Name, "bootstraptemplate").Build()
562+
bootstrapTemplate := builder.TestBootstrapTemplate(ns.Name, "bootstraptemplate").Build()
563563

564564
// 2) ClusterClass definitions including definitions of MachineDeploymentClasses used inside the ClusterClass.
565565
machineDeploymentClass1 := builder.MachineDeploymentClass(workerClassName1).
@@ -676,14 +676,14 @@ func assertClusterReconcile(cluster *clusterv1.Cluster) error {
676676

677677
// Check if InfrastructureRef exists and is of the expected Kind and APIVersion.
678678
if err := referenceExistsWithCorrectKindAndAPIVersion(cluster.Spec.InfrastructureRef,
679-
builder.GenericInfrastructureClusterKind,
679+
builder.TestInfrastructureClusterKind,
680680
builder.InfrastructureGroupVersion); err != nil {
681681
return err
682682
}
683683

684684
// Check if ControlPlaneRef exists is of the expected Kind and APIVersion.
685685
if err := referenceExistsWithCorrectKindAndAPIVersion(cluster.Spec.ControlPlaneRef,
686-
builder.GenericControlPlaneKind,
686+
builder.TestControlPlaneKind,
687687
builder.ControlPlaneGroupVersion); err != nil {
688688
return err
689689
}
@@ -742,7 +742,7 @@ func assertControlPlaneReconcile(cluster *clusterv1.Cluster) error {
742742
return err
743743
}
744744
if err := referenceExistsWithCorrectKindAndAPIVersion(cpInfra,
745-
builder.GenericInfrastructureMachineTemplateKind,
745+
builder.TestInfrastructureMachineTemplateKind,
746746
builder.InfrastructureGroupVersion); err != nil {
747747
return err
748748
}
@@ -824,7 +824,7 @@ func assertMachineDeploymentsReconcile(cluster *clusterv1.Cluster) error {
824824

825825
// Check if the InfrastructureReference exists.
826826
if err := referenceExistsWithCorrectKindAndAPIVersion(&md.Spec.Template.Spec.InfrastructureRef,
827-
builder.GenericInfrastructureMachineTemplateKind,
827+
builder.TestInfrastructureMachineTemplateKind,
828828
builder.InfrastructureGroupVersion); err != nil {
829829
return err
830830
}
@@ -836,7 +836,7 @@ func assertMachineDeploymentsReconcile(cluster *clusterv1.Cluster) error {
836836

837837
// Check if the Bootstrap reference has the expected Kind and APIVersion.
838838
if err := referenceExistsWithCorrectKindAndAPIVersion(md.Spec.Template.Spec.Bootstrap.ConfigRef,
839-
builder.GenericBootstrapConfigTemplateKind,
839+
builder.TestBootstrapConfigTemplateKind,
840840
builder.BootstrapGroupVersion); err != nil {
841841
return err
842842
}

internal/test/builder/bootstrap.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ var (
5050

5151
func testBootstrapConfigTemplateCRD(gvk schema.GroupVersionKind) *apiextensionsv1.CustomResourceDefinition {
5252
return generateCRD(gvk, map[string]apiextensionsv1.JSONSchemaProps{
53+
"metadata": {
54+
// NOTE: in CRD there is only a partial definition of metadata schema.
55+
// Ref https://github.com/kubernetes-sigs/controller-tools/blob/59485af1c1f6a664655dad49543c474bb4a0d2a2/pkg/crd/gen.go#L185
56+
Type: "object",
57+
},
5358
"spec": {
5459
Type: "object",
5560
Properties: map[string]apiextensionsv1.JSONSchemaProps{
@@ -67,6 +72,11 @@ func testBootstrapConfigTemplateCRD(gvk schema.GroupVersionKind) *apiextensionsv
6772

6873
func testBootstrapConfigCRD(gvk schema.GroupVersionKind) *apiextensionsv1.CustomResourceDefinition {
6974
return generateCRD(gvk, map[string]apiextensionsv1.JSONSchemaProps{
75+
"metadata": {
76+
// NOTE: in CRD there is only a partial definition of metadata schema.
77+
// Ref https://github.com/kubernetes-sigs/controller-tools/blob/59485af1c1f6a664655dad49543c474bb4a0d2a2/pkg/crd/gen.go#L185
78+
Type: "object",
79+
},
7080
"spec": bootstrapConfigSpecSchema,
7181
"status": {
7282
Type: "object",

internal/test/builder/builders.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,51 @@ func (i *InfrastructureClusterTemplateBuilder) Build() *unstructured.Unstructure
613613
return obj
614614
}
615615

616+
// TestInfrastructureClusterTemplateBuilder holds the variables needed to build a generic TestInfrastructureClusterTemplate.
617+
// +kubebuilder:object:generate=false
618+
type TestInfrastructureClusterTemplateBuilder struct {
619+
namespace string
620+
name string
621+
specFields map[string]interface{}
622+
}
623+
624+
// TestInfrastructureClusterTemplate returns an TestInfrastructureClusterTemplateBuilder with the given name and namespace.
625+
func TestInfrastructureClusterTemplate(namespace, name string) *TestInfrastructureClusterTemplateBuilder {
626+
return &TestInfrastructureClusterTemplateBuilder{
627+
namespace: namespace,
628+
name: name,
629+
}
630+
}
631+
632+
// WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds
633+
// to the value of the spec field.
634+
//
635+
// Note: all the paths should start with "spec."; the path should correspond to a field defined in the CRD.
636+
//
637+
// Example map: map[string]interface{}{
638+
// "spec.version": "v1.2.3",
639+
// }.
640+
func (i *TestInfrastructureClusterTemplateBuilder) WithSpecFields(fields map[string]interface{}) *TestInfrastructureClusterTemplateBuilder {
641+
i.specFields = fields
642+
return i
643+
}
644+
645+
// Build creates a new Unstructured object with the variables passed to the InfrastructureClusterTemplateBuilder.
646+
func (i *TestInfrastructureClusterTemplateBuilder) Build() *unstructured.Unstructured {
647+
obj := &unstructured.Unstructured{}
648+
obj.SetAPIVersion(InfrastructureGroupVersion.String())
649+
obj.SetKind(TestInfrastructureClusterTemplateKind)
650+
obj.SetNamespace(i.namespace)
651+
obj.SetName(i.name)
652+
653+
// Initialize the spec.template.spec to make the object valid in reconciliation.
654+
setSpecFields(obj, map[string]interface{}{"spec.template.spec": map[string]interface{}{}})
655+
656+
setSpecFields(obj, i.specFields)
657+
658+
return obj
659+
}
660+
616661
// ControlPlaneTemplateBuilder holds the variables and objects needed to build a generic ControlPlane template.
617662
// +kubebuilder:object:generate=false
618663
type ControlPlaneTemplateBuilder struct {
@@ -670,6 +715,63 @@ func (c *ControlPlaneTemplateBuilder) Build() *unstructured.Unstructured {
670715
return obj
671716
}
672717

718+
// TestControlPlaneTemplateBuilder holds the variables and objects needed to build a generic ControlPlane template.
719+
// +kubebuilder:object:generate=false
720+
type TestControlPlaneTemplateBuilder struct {
721+
namespace string
722+
name string
723+
infrastructureMachineTemplate *unstructured.Unstructured
724+
specFields map[string]interface{}
725+
}
726+
727+
// TestControlPlaneTemplate creates a NewControlPlaneTemplate builder with the given name and namespace.
728+
func TestControlPlaneTemplate(namespace, name string) *TestControlPlaneTemplateBuilder {
729+
return &TestControlPlaneTemplateBuilder{
730+
namespace: namespace,
731+
name: name,
732+
}
733+
}
734+
735+
// WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds
736+
// to the value of the spec field.
737+
//
738+
// Note: all the paths should start with "spec."; the path should correspond to a field defined in the CRD.
739+
//
740+
// Example map: map[string]interface{}{
741+
// "spec.version": "v1.2.3",
742+
// }.
743+
func (c *TestControlPlaneTemplateBuilder) WithSpecFields(fields map[string]interface{}) *TestControlPlaneTemplateBuilder {
744+
c.specFields = fields
745+
return c
746+
}
747+
748+
// WithInfrastructureMachineTemplate adds the given Unstructured object to the ControlPlaneTemplateBuilder as its InfrastructureMachineTemplate.
749+
func (c *TestControlPlaneTemplateBuilder) WithInfrastructureMachineTemplate(t *unstructured.Unstructured) *TestControlPlaneTemplateBuilder {
750+
c.infrastructureMachineTemplate = t
751+
return c
752+
}
753+
754+
// Build creates an Unstructured object from the variables passed to the ControlPlaneTemplateBuilder.
755+
func (c *TestControlPlaneTemplateBuilder) Build() *unstructured.Unstructured {
756+
obj := &unstructured.Unstructured{}
757+
obj.SetAPIVersion(ControlPlaneGroupVersion.String())
758+
obj.SetKind(TestControlPlaneTemplateKind)
759+
obj.SetNamespace(c.namespace)
760+
obj.SetName(c.name)
761+
762+
// Initialize the spec.template.spec to make the object valid in reconciliation.
763+
setSpecFields(obj, map[string]interface{}{"spec.template.spec": map[string]interface{}{}})
764+
765+
setSpecFields(obj, c.specFields)
766+
767+
if c.infrastructureMachineTemplate != nil {
768+
if err := setNestedRef(obj, c.infrastructureMachineTemplate, "spec", "template", "spec", "machineTemplate", "infrastructureRef"); err != nil {
769+
panic(err)
770+
}
771+
}
772+
return obj
773+
}
774+
673775
// InfrastructureClusterBuilder holds the variables and objects needed to build a generic InfrastructureCluster.
674776
// +kubebuilder:object:generate=false
675777
type InfrastructureClusterBuilder struct {

internal/test/builder/controlplane.go

Lines changed: 77 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,63 +37,47 @@ var (
3737

3838
// TODO: drop generic CRDs in favour of typed test CRDs.
3939

40+
// TestControlPlaneTemplateKind is the Kind for the TestControlPlaneTemplate.
41+
TestControlPlaneTemplateKind = "TestControlPlaneTemplate"
42+
// TestControlPlaneTemplateCRD is a test control plane template CRD.
43+
TestControlPlaneTemplateCRD = testControlPlaneTemplateCRD(ControlPlaneGroupVersion.WithKind(TestControlPlaneTemplateKind))
44+
4045
// TestControlPlaneKind is the Kind for the TestControlPlane.
4146
TestControlPlaneKind = "TestControlPlane"
4247
// TestControlPlaneCRD is a test control plane CRD.
4348
TestControlPlaneCRD = testControlPlaneCRD(ControlPlaneGroupVersion.WithKind(TestControlPlaneKind))
4449
)
4550

46-
func testControlPlaneCRD(gvk schema.GroupVersionKind) *apiextensionsv1.CustomResourceDefinition {
51+
func testControlPlaneTemplateCRD(gvk schema.GroupVersionKind) *apiextensionsv1.CustomResourceDefinition {
4752
return generateCRD(gvk, map[string]apiextensionsv1.JSONSchemaProps{
53+
"metadata": {
54+
// NOTE: in CRD there is only a partial definition of metadata schema.
55+
// Ref https://github.com/kubernetes-sigs/controller-tools/blob/59485af1c1f6a664655dad49543c474bb4a0d2a2/pkg/crd/gen.go#L185
56+
Type: "object",
57+
},
4858
"spec": {
4959
Type: "object",
5060
Properties: map[string]apiextensionsv1.JSONSchemaProps{
51-
// Mandatory field from the Cluster API contract - version support
52-
"version": {
53-
Type: "string",
54-
},
55-
// mandatory field from the Cluster API contract - replicas support
56-
"replicas": {
57-
Type: "integer",
58-
Format: "int32",
59-
},
60-
// mandatory field from the Cluster API contract - using Machines support
61-
"machineTemplate": {
62-
Type: "object",
63-
Properties: map[string]apiextensionsv1.JSONSchemaProps{
64-
"metadata": metadataSchema,
65-
"infrastructureRef": refSchema,
66-
"nodeDeletionTimeout": {Type: "string"},
67-
"nodeDrainTimeout": {Type: "string"},
68-
},
69-
},
70-
// General purpose fields to be used in different test scenario.
71-
"foo": {Type: "string"},
72-
"bar": {Type: "string"},
73-
// Copy of a subset of KCP spec fields to test server side apply on deep nested structs
74-
"kubeadmConfigSpec": {
61+
// Mandatory field from the Cluster API contract
62+
"template": {
7563
Type: "object",
7664
Properties: map[string]apiextensionsv1.JSONSchemaProps{
77-
"clusterConfiguration": {
78-
Type: "object",
79-
Properties: map[string]apiextensionsv1.JSONSchemaProps{
80-
"controllerManager": {
81-
Type: "object",
82-
Properties: map[string]apiextensionsv1.JSONSchemaProps{
83-
"extraArgs": {
84-
Type: "object",
85-
AdditionalProperties: &apiextensionsv1.JSONSchemaPropsOrBool{
86-
Schema: &apiextensionsv1.JSONSchemaProps{Type: "string"},
87-
},
88-
},
89-
},
90-
},
91-
},
92-
},
65+
"spec": controPlaneSpecSchema,
9366
},
9467
},
9568
},
9669
},
70+
})
71+
}
72+
73+
func testControlPlaneCRD(gvk schema.GroupVersionKind) *apiextensionsv1.CustomResourceDefinition {
74+
return generateCRD(gvk, map[string]apiextensionsv1.JSONSchemaProps{
75+
"metadata": {
76+
// NOTE: in CRD there is only a partial definition of metadata schema.
77+
// Ref https://github.com/kubernetes-sigs/controller-tools/blob/59485af1c1f6a664655dad49543c474bb4a0d2a2/pkg/crd/gen.go#L185
78+
Type: "object",
79+
},
80+
"spec": controPlaneSpecSchema,
9781
"status": {
9882
Type: "object",
9983
Properties: map[string]apiextensionsv1.JSONSchemaProps{
@@ -114,3 +98,55 @@ func testControlPlaneCRD(gvk schema.GroupVersionKind) *apiextensionsv1.CustomRes
11498
},
11599
})
116100
}
101+
102+
var (
103+
controPlaneSpecSchema = apiextensionsv1.JSONSchemaProps{
104+
Type: "object",
105+
Properties: map[string]apiextensionsv1.JSONSchemaProps{
106+
// Mandatory field from the Cluster API contract - version support
107+
"version": {
108+
Type: "string",
109+
},
110+
// mandatory field from the Cluster API contract - replicas support
111+
"replicas": {
112+
Type: "integer",
113+
Format: "int32",
114+
},
115+
// mandatory field from the Cluster API contract - using Machines support
116+
"machineTemplate": {
117+
Type: "object",
118+
Properties: map[string]apiextensionsv1.JSONSchemaProps{
119+
"metadata": metadataSchema,
120+
"infrastructureRef": refSchema,
121+
"nodeDeletionTimeout": {Type: "string"},
122+
"nodeDrainTimeout": {Type: "string"},
123+
},
124+
},
125+
// General purpose fields to be used in different test scenario.
126+
"foo": {Type: "string"},
127+
"bar": {Type: "string"},
128+
// Copy of a subset of KCP spec fields to test server side apply on deep nested structs
129+
"kubeadmConfigSpec": {
130+
Type: "object",
131+
Properties: map[string]apiextensionsv1.JSONSchemaProps{
132+
"clusterConfiguration": {
133+
Type: "object",
134+
Properties: map[string]apiextensionsv1.JSONSchemaProps{
135+
"controllerManager": {
136+
Type: "object",
137+
Properties: map[string]apiextensionsv1.JSONSchemaProps{
138+
"extraArgs": {
139+
Type: "object",
140+
AdditionalProperties: &apiextensionsv1.JSONSchemaPropsOrBool{
141+
Schema: &apiextensionsv1.JSONSchemaProps{Type: "string"},
142+
},
143+
},
144+
},
145+
},
146+
},
147+
},
148+
},
149+
},
150+
},
151+
}
152+
)

0 commit comments

Comments
 (0)