Skip to content

Commit

Permalink
Add clone for MachineHealthCheck Builders
Browse files Browse the repository at this point in the history
Signed-off-by: killianmuldoon <[email protected]>
  • Loading branch information
killianmuldoon committed Jan 27, 2022
1 parent 31fb217 commit a5d1b5c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 156 deletions.
191 changes: 35 additions & 156 deletions internal/controllers/topology/cluster/reconcile_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,17 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) {
Build()

maxUnhealthy := intstr.Parse("45%")
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()
Expand All @@ -1790,33 +1801,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").
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
Build()},
mhcBuilder.Clone().Build()},
},
{
name: "Create a new MachineHealthCheck if the MachineDeployment is modified to include one",
Expand All @@ -1826,141 +1814,43 @@ 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()),
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,
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").
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()),
mhcBuilder.Clone().Build()),
},
desired: []*scope.MachineDeploymentState{newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, nil)},
want: []*clusterv1.MachineHealthCheck{},
Expand All @@ -1969,18 +1859,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()),
mhcBuilder.Clone().Build()),
},
desired: []*scope.MachineDeploymentState{},
want: []*clusterv1.MachineHealthCheck{},
Expand Down Expand Up @@ -2082,36 +1961,36 @@ 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,
},

{
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,
Expand All @@ -2121,14 +2000,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,
Expand Down
26 changes: 26 additions & 0 deletions internal/test/builder/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,3 +990,29 @@ 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(),
}

if m.maxUnhealthy != nil {
maxUnhealthy := *m.maxUnhealthy
out.maxUnhealthy = &maxUnhealthy
}

if len(m.conditions) > 0 {
out.conditions = make([]clusterv1.UnhealthyCondition, len(m.conditions))
copy(out.conditions, m.conditions)
}
if len(m.ownerRefs) > 0 {
for _, ref := range m.ownerRefs {
out.ownerRefs = append(out.ownerRefs, *ref.DeepCopy())
}
}
return out
}

0 comments on commit a5d1b5c

Please sign in to comment.