Skip to content

Commit

Permalink
Merge pull request #6750 from k8s-infra-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…6713-to-release-1.2

[release-1.2] 🌱  make TestControlPlaneTemplate deepcopy-able
  • Loading branch information
k8s-ci-robot authored Jun 27, 2022
2 parents e41dc69 + 1f859d0 commit 9d70124
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
45 changes: 19 additions & 26 deletions internal/test/builder/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,19 +716,26 @@ func (c *ControlPlaneTemplateBuilder) Build() *unstructured.Unstructured {
}

// TestControlPlaneTemplateBuilder holds the variables and objects needed to build a generic ControlPlane template.
// +kubebuilder:object:generate=false
type TestControlPlaneTemplateBuilder struct {
namespace string
name string
infrastructureMachineTemplate *unstructured.Unstructured
specFields map[string]interface{}
obj *unstructured.Unstructured
}

// TestControlPlaneTemplate creates a NewControlPlaneTemplate builder with the given name and namespace.
func TestControlPlaneTemplate(namespace, name string) *TestControlPlaneTemplateBuilder {
obj := &unstructured.Unstructured{}
obj.SetName(name)
obj.SetNamespace(namespace)
obj.SetAPIVersion(ControlPlaneGroupVersion.String())
obj.SetKind(TestControlPlaneTemplateKind)
// Set the mandatory spec fields for the object.
if err := unstructured.SetNestedField(obj.Object, map[string]interface{}{}, "spec"); err != nil {
panic(err)
}
if err := unstructured.SetNestedField(obj.Object, map[string]interface{}{}, "spec", "template", "spec"); err != nil {
panic(err)
}
return &TestControlPlaneTemplateBuilder{
namespace: namespace,
name: name,
obj,
}
}

Expand All @@ -741,35 +748,21 @@ func TestControlPlaneTemplate(namespace, name string) *TestControlPlaneTemplateB
// "spec.version": "v1.2.3",
// }.
func (c *TestControlPlaneTemplateBuilder) WithSpecFields(fields map[string]interface{}) *TestControlPlaneTemplateBuilder {
c.specFields = fields
setSpecFields(c.obj, fields)
return c
}

// WithInfrastructureMachineTemplate adds the given Unstructured object to the ControlPlaneTemplateBuilder as its InfrastructureMachineTemplate.
func (c *TestControlPlaneTemplateBuilder) WithInfrastructureMachineTemplate(t *unstructured.Unstructured) *TestControlPlaneTemplateBuilder {
c.infrastructureMachineTemplate = t
if err := setNestedRef(c.obj, t, "spec", "template", "spec", "machineTemplate", "infrastructureRef"); err != nil {
panic(err)
}
return c
}

// Build creates an Unstructured object from the variables passed to the ControlPlaneTemplateBuilder.
func (c *TestControlPlaneTemplateBuilder) Build() *unstructured.Unstructured {
obj := &unstructured.Unstructured{}
obj.SetAPIVersion(ControlPlaneGroupVersion.String())
obj.SetKind(TestControlPlaneTemplateKind)
obj.SetNamespace(c.namespace)
obj.SetName(c.name)

// Initialize the spec.template.spec to make the object valid in reconciliation.
setSpecFields(obj, map[string]interface{}{"spec.template.spec": map[string]interface{}{}})

setSpecFields(obj, c.specFields)

if c.infrastructureMachineTemplate != nil {
if err := setNestedRef(obj, c.infrastructureMachineTemplate, "spec", "template", "spec", "machineTemplate", "infrastructureRef"); err != nil {
panic(err)
}
}
return obj
return c.obj
}

// InfrastructureClusterBuilder holds the variables and objects needed to build a generic InfrastructureCluster.
Expand Down
19 changes: 19 additions & 0 deletions internal/test/builder/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9d70124

Please sign in to comment.