Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Make InfrastructureMachineTemplateBuilder DeepCopy-able #6670

Merged
merged 2 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions internal/test/builder/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,18 +403,26 @@ func (m *MachineDeploymentClassBuilder) Build() *clusterv1.MachineDeploymentClas
}

// InfrastructureMachineTemplateBuilder holds the variables and objects needed to build an InfrastructureMachineTemplate.
// +kubebuilder:object:generate=false
type InfrastructureMachineTemplateBuilder struct {
namespace string
name string
specFields map[string]interface{}
obj *unstructured.Unstructured
}

// InfrastructureMachineTemplate creates an InfrastructureMachineTemplateBuilder with the given name and namespace.
func InfrastructureMachineTemplate(namespace, name string) *InfrastructureMachineTemplateBuilder {
obj := &unstructured.Unstructured{}
obj.SetName(name)
obj.SetNamespace(namespace)
obj.SetAPIVersion(InfrastructureGroupVersion.String())
obj.SetKind(GenericInfrastructureMachineTemplateKind)
// 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 &InfrastructureMachineTemplateBuilder{
namespace: namespace,
name: name,
obj,
}
}

Expand All @@ -427,23 +435,13 @@ func InfrastructureMachineTemplate(namespace, name string) *InfrastructureMachin
// "spec.version": "v1.2.3",
// }.
func (i *InfrastructureMachineTemplateBuilder) WithSpecFields(fields map[string]interface{}) *InfrastructureMachineTemplateBuilder {
i.specFields = fields
setSpecFields(i.obj, fields)
return i
}

// Build takes the objects and variables in the InfrastructureMachineTemplateBuilder and generates an unstructured object.
func (i *InfrastructureMachineTemplateBuilder) Build() *unstructured.Unstructured {
obj := &unstructured.Unstructured{}
obj.SetAPIVersion(InfrastructureGroupVersion.String())
obj.SetKind(GenericInfrastructureMachineTemplateKind)
obj.SetNamespace(i.namespace)
obj.SetName(i.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, i.specFields)
return obj
return i.obj
}

// TestInfrastructureMachineTemplateBuilder holds the variables and objects needed to build an TestInfrastructureMachineTemplate.
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.