From 463db97b487e367d16dce950dd9b7bd04f00960d Mon Sep 17 00:00:00 2001 From: sayantani11 Date: Sat, 18 Jun 2022 22:54:38 +0530 Subject: [PATCH 1/2] Make InfrastructureMachineTemplateBuilder DeepCopy-able --- internal/test/builder/builders.go | 36 +++++++++---------- .../test/builder/zz_generated.deepcopy.go | 19 ++++++++++ 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/internal/test/builder/builders.go b/internal/test/builder/builders.go index a3c8cc6f4167..fdb028c2c930 100644 --- a/internal/test/builder/builders.go +++ b/internal/test/builder/builders.go @@ -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, } } @@ -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. @@ -471,7 +469,7 @@ func TestInfrastructureMachineTemplate(namespace, name string) *TestInfrastructu // "spec.version": "v1.2.3", // }. func (i *TestInfrastructureMachineTemplateBuilder) WithSpecFields(fields map[string]interface{}) *TestInfrastructureMachineTemplateBuilder { - i.specFields = fields + i.specFields = fielda return i } diff --git a/internal/test/builder/zz_generated.deepcopy.go b/internal/test/builder/zz_generated.deepcopy.go index 75fb38b60c92..cf18a8d605df 100644 --- a/internal/test/builder/zz_generated.deepcopy.go +++ b/internal/test/builder/zz_generated.deepcopy.go @@ -155,6 +155,25 @@ func (in *ClusterTopologyBuilder) DeepCopy() *ClusterTopologyBuilder { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *InfrastructureMachineTemplateBuilder) DeepCopyInto(out *InfrastructureMachineTemplateBuilder) { + *out = *in + if in.obj != nil { + in, out := &in.obj, &out.obj + *out = (*in).DeepCopy() + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InfrastructureMachineTemplateBuilder. +func (in *InfrastructureMachineTemplateBuilder) DeepCopy() *InfrastructureMachineTemplateBuilder { + if in == nil { + return nil + } + out := new(InfrastructureMachineTemplateBuilder) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *MachineBuilder) DeepCopyInto(out *MachineBuilder) { *out = *in From f6069ab64e93ebb7815dd25cee12abcf4c5c0d36 Mon Sep 17 00:00:00 2001 From: sayantani11 Date: Sat, 25 Jun 2022 19:37:58 +0530 Subject: [PATCH 2/2] small error --- internal/test/builder/builders.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/test/builder/builders.go b/internal/test/builder/builders.go index fdb028c2c930..bc4266cadd59 100644 --- a/internal/test/builder/builders.go +++ b/internal/test/builder/builders.go @@ -469,7 +469,7 @@ func TestInfrastructureMachineTemplate(namespace, name string) *TestInfrastructu // "spec.version": "v1.2.3", // }. func (i *TestInfrastructureMachineTemplateBuilder) WithSpecFields(fields map[string]interface{}) *TestInfrastructureMachineTemplateBuilder { - i.specFields = fielda + i.specFields = fields return i }