Skip to content

Commit

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

[release-1.2] 🌱 Make InfrastructureMachineTemplateBuilder DeepCopy-able
  • Loading branch information
k8s-ci-robot authored Jun 27, 2022
2 parents 9d70124 + f6069ab commit b3cd04d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
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.

0 comments on commit b3cd04d

Please sign in to comment.