diff --git a/internal/controllers/topology/cluster/reconcile_state_test.go b/internal/controllers/topology/cluster/reconcile_state_test.go index 26f844589f61..e982180bda31 100644 --- a/internal/controllers/topology/cluster/reconcile_state_test.go +++ b/internal/controllers/topology/cluster/reconcile_state_test.go @@ -1775,6 +1775,18 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) { Build() maxUnhealthy := intstr.Parse("45%") + // TODO: (killianmuldoon) This builder should be copied and not just passed around. + mhcBuilder := builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1"). + WithSelector(*selectorForMachineDeploymentMHC(md)). + WithUnhealthyConditions([]clusterv1.UnhealthyCondition{ + { + Type: corev1.NodeReady, + Status: corev1.ConditionUnknown, + Timeout: metav1.Duration{Duration: 5 * time.Minute}, + }, + }). + WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}). + WithClusterName("cluster1") infrastructureMachineTemplate := builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "infrastructure-machine-1").Build() bootstrapTemplate := builder.BootstrapTemplate(metav1.NamespaceDefault, "bootstrap-config-1").Build() @@ -1790,31 +1802,10 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) { current: nil, desired: []*scope.MachineDeploymentState{ newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, - builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1"). - WithSelector(*selectorForMachineDeploymentMHC(md)). - WithUnhealthyConditions([]clusterv1.UnhealthyCondition{ - { - Type: corev1.NodeReady, - Status: corev1.ConditionUnknown, - Timeout: metav1.Duration{Duration: 5 * time.Minute}, - }, - }). - WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}). - WithClusterName("cluster1"). - Build()), + mhcBuilder.Clone().Build()), }, want: []*clusterv1.MachineHealthCheck{ - builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1"). - WithSelector(*selectorForMachineDeploymentMHC(md)). - WithUnhealthyConditions([]clusterv1.UnhealthyCondition{ - { - Type: corev1.NodeReady, - Status: corev1.ConditionUnknown, - Timeout: metav1.Duration{Duration: 5 * time.Minute}, - }, - }). - WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}). - WithClusterName("cluster1"). + mhcBuilder. WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}). Build()}, }, @@ -1826,95 +1817,29 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) { // MHC is added in the desired state of the MachineDeployment desired: []*scope.MachineDeploymentState{ newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, - builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1"). - WithSelector(*selectorForMachineDeploymentMHC(md)). - WithUnhealthyConditions([]clusterv1.UnhealthyCondition{ - { - Type: corev1.NodeReady, - Status: corev1.ConditionUnknown, - Timeout: metav1.Duration{Duration: 5 * time.Minute}, - }, - }). - WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}). - WithClusterName("cluster1"). - Build()), + mhcBuilder.Clone().Build()), }, want: []*clusterv1.MachineHealthCheck{ - builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1"). - WithSelector(*selectorForMachineDeploymentMHC(md)). - WithUnhealthyConditions([]clusterv1.UnhealthyCondition{ - { - Type: corev1.NodeReady, - Status: corev1.ConditionUnknown, - Timeout: metav1.Duration{Duration: 5 * time.Minute}, - }, - }). - WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}). - WithClusterName("cluster1"). - WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}). - Build()}}, + mhcBuilder.Clone().Build()}}, { name: "Update MachineHealthCheck spec adding a field if the spec adds a field", current: []*scope.MachineDeploymentState{ - newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, - builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1"). - WithSelector(*selectorForMachineDeploymentMHC(md)). - WithUnhealthyConditions([]clusterv1.UnhealthyCondition{ - { - Type: corev1.NodeReady, - Status: corev1.ConditionUnknown, - Timeout: metav1.Duration{Duration: 5 * time.Minute}, - }, - }). - WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}). - WithClusterName("cluster1"). - Build()), + newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, mhcBuilder.Clone().Build()), }, desired: []*scope.MachineDeploymentState{ newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, - builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1"). - WithSelector(*selectorForMachineDeploymentMHC(md)). - WithUnhealthyConditions([]clusterv1.UnhealthyCondition{ - { - Type: corev1.NodeReady, - Status: corev1.ConditionUnknown, - Timeout: metav1.Duration{Duration: 5 * time.Minute}, - }, - }). - WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}). - WithClusterName("cluster1").WithMaxUnhealthy(&maxUnhealthy). - Build())}, + mhcBuilder.Clone().WithMaxUnhealthy(&maxUnhealthy).Build())}, want: []*clusterv1.MachineHealthCheck{ - builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1"). - WithSelector(*selectorForMachineDeploymentMHC(md)). - WithUnhealthyConditions([]clusterv1.UnhealthyCondition{ - { - Type: corev1.NodeReady, - Status: corev1.ConditionUnknown, - Timeout: metav1.Duration{Duration: 5 * time.Minute}, - }, - }). + mhcBuilder.Clone(). WithMaxUnhealthy(&maxUnhealthy). WithClusterName("cluster1"). - WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}). Build()}, }, { name: "Update MachineHealthCheck spec removing a field if the spec removes a field", current: []*scope.MachineDeploymentState{ newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, - builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1"). - WithSelector(*selectorForMachineDeploymentMHC(md)). - WithUnhealthyConditions([]clusterv1.UnhealthyCondition{ - { - Type: corev1.NodeReady, - Status: corev1.ConditionUnknown, - Timeout: metav1.Duration{Duration: 5 * time.Minute}, - }, - }). - WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}). - WithClusterName("cluster1").WithMaxUnhealthy(&maxUnhealthy). - Build()), + mhcBuilder.Clone().WithMaxUnhealthy(&maxUnhealthy).Build()), }, desired: []*scope.MachineDeploymentState{ newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, @@ -1932,35 +1857,13 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) { Build()), }, want: []*clusterv1.MachineHealthCheck{ - builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1"). - WithSelector(*selectorForMachineDeploymentMHC(md)). - WithUnhealthyConditions([]clusterv1.UnhealthyCondition{ - { - Type: corev1.NodeReady, - Status: corev1.ConditionUnknown, - Timeout: metav1.Duration{Duration: 5 * time.Minute}, - }, - }). - WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}). - WithClusterName("cluster1"). - Build()}, + mhcBuilder.Clone().Build(), + }, }, { name: "Delete MachineHealthCheck spec if the MachineDeployment is modified to remove an existing one", current: []*scope.MachineDeploymentState{ - newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, - builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1"). - WithSelector(*selectorForMachineDeploymentMHC(md)). - WithUnhealthyConditions([]clusterv1.UnhealthyCondition{ - { - Type: corev1.NodeReady, - Status: corev1.ConditionUnknown, - Timeout: metav1.Duration{Duration: 5 * time.Minute}, - }, - }). - WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}). - WithClusterName("cluster1"). - Build()), + newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, mhcBuilder.Clone().Build()), }, desired: []*scope.MachineDeploymentState{newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, nil)}, want: []*clusterv1.MachineHealthCheck{}, @@ -1968,19 +1871,7 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) { { name: "Delete MachineHealthCheck spec if the MachineDeployment is deleted", current: []*scope.MachineDeploymentState{ - newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, - builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1"). - WithSelector(*selectorForMachineDeploymentMHC(md)). - WithUnhealthyConditions([]clusterv1.UnhealthyCondition{ - { - Type: corev1.NodeReady, - Status: corev1.ConditionUnknown, - Timeout: metav1.Duration{Duration: 5 * time.Minute}, - }, - }). - WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}). - WithClusterName("cluster1"). - Build()), + newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, mhcBuilder.Clone().Build()), }, desired: []*scope.MachineDeploymentState{}, want: []*clusterv1.MachineHealthCheck{}, @@ -2082,19 +1973,19 @@ func TestReconciler_reconcileMachineHealthCheck(t *testing.T) { { name: "Create a MachineHealthCheck", current: nil, - desired: mhcBuilder.Build(), - want: mhcBuilder.Build(), + desired: mhcBuilder.Clone().Build(), + want: mhcBuilder.Clone().Build(), }, { name: "Successfully create a valid Ownerreference on the MachineHealthCheck", current: nil, // update the unhealthy conditions in the MachineHealthCheck - desired: mhcBuilder. + desired: mhcBuilder.Clone(). // Desired object has an incomplete owner reference which has no UID. WithOwnerReferences([]metav1.OwnerReference{{Name: cp.GetName(), Kind: cp.GetKind(), APIVersion: cp.GetAPIVersion()}}). Build(), // Want a reconciled object with a full ownerReference including UID - want: mhcBuilder. + want: mhcBuilder.Clone(). WithOwnerReferences([]metav1.OwnerReference{{Name: cp.GetName(), Kind: cp.GetKind(), APIVersion: cp.GetAPIVersion(), UID: cp.GetUID()}}). Build(), wantErr: false, @@ -2102,16 +1993,16 @@ func TestReconciler_reconcileMachineHealthCheck(t *testing.T) { { name: "Update a MachineHealthCheck with changes", - current: mhcBuilder.Build(), + current: mhcBuilder.Clone().Build(), // update the unhealthy conditions in the MachineHealthCheck - desired: mhcBuilder.WithUnhealthyConditions([]clusterv1.UnhealthyCondition{ + desired: mhcBuilder.Clone().WithUnhealthyConditions([]clusterv1.UnhealthyCondition{ { Type: corev1.NodeReady, Status: corev1.ConditionUnknown, Timeout: metav1.Duration{Duration: 1000 * time.Minute}, }, }).Build(), - want: mhcBuilder.WithUnhealthyConditions([]clusterv1.UnhealthyCondition{ + want: mhcBuilder.Clone().WithUnhealthyConditions([]clusterv1.UnhealthyCondition{ { Type: corev1.NodeReady, Status: corev1.ConditionUnknown, @@ -2121,14 +2012,14 @@ func TestReconciler_reconcileMachineHealthCheck(t *testing.T) { }, { name: "Don't change a MachineHealthCheck with no difference between desired and current", - current: mhcBuilder.Build(), + current: mhcBuilder.Clone().Build(), // update the unhealthy conditions in the MachineHealthCheck - desired: mhcBuilder.Build(), - want: mhcBuilder.Build(), + desired: mhcBuilder.Clone().Build(), + want: mhcBuilder.Clone().Build(), }, { name: "Delete a MachineHealthCheck", - current: mhcBuilder.Build(), + current: mhcBuilder.Clone().Build(), // update the unhealthy conditions in the MachineHealthCheck desired: nil, want: nil, diff --git a/internal/test/builder/builders.go b/internal/test/builder/builders.go index a29b0edd593d..deefb6fe1212 100644 --- a/internal/test/builder/builders.go +++ b/internal/test/builder/builders.go @@ -990,3 +990,19 @@ func (m *MachineHealthCheckBuilder) Build() *clusterv1.MachineHealthCheck { }, } } + +// Clone returns a copy of the MachineHealthCheckBuilder. +func (m *MachineHealthCheckBuilder) Clone() *MachineHealthCheckBuilder { + out := &MachineHealthCheckBuilder{ + name: m.name, + namespace: m.namespace, + clusterName: m.clusterName, + selector: *m.selector.DeepCopy(), + maxUnhealthy: m.maxUnhealthy, + } + copy(m.conditions, out.conditions) + for _, ref := range m.ownerRefs { + out.ownerRefs = append(out.ownerRefs, *ref.DeepCopy()) + } + return out +}