Skip to content

Commit 4e30d45

Browse files
Add clone for MachineHealthCheck Builders
Signed-off-by: killianmuldoon <[email protected]>
1 parent 31fb217 commit 4e30d45

File tree

2 files changed

+62
-154
lines changed

2 files changed

+62
-154
lines changed

internal/controllers/topology/cluster/reconcile_state_test.go

Lines changed: 36 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,18 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) {
17751775
Build()
17761776

17771777
maxUnhealthy := intstr.Parse("45%")
1778+
// TODO: (killianmuldoon) This builder should be copied and not just passed around.
1779+
mhcBuilder := builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
1780+
WithSelector(*selectorForMachineDeploymentMHC(md)).
1781+
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1782+
{
1783+
Type: corev1.NodeReady,
1784+
Status: corev1.ConditionUnknown,
1785+
Timeout: metav1.Duration{Duration: 5 * time.Minute},
1786+
},
1787+
}).
1788+
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1789+
WithClusterName("cluster1")
17781790

17791791
infrastructureMachineTemplate := builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "infrastructure-machine-1").Build()
17801792
bootstrapTemplate := builder.BootstrapTemplate(metav1.NamespaceDefault, "bootstrap-config-1").Build()
@@ -1790,31 +1802,10 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) {
17901802
current: nil,
17911803
desired: []*scope.MachineDeploymentState{
17921804
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
1793-
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
1794-
WithSelector(*selectorForMachineDeploymentMHC(md)).
1795-
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1796-
{
1797-
Type: corev1.NodeReady,
1798-
Status: corev1.ConditionUnknown,
1799-
Timeout: metav1.Duration{Duration: 5 * time.Minute},
1800-
},
1801-
}).
1802-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1803-
WithClusterName("cluster1").
1804-
Build()),
1805+
mhcBuilder.Clone().Build()),
18051806
},
18061807
want: []*clusterv1.MachineHealthCheck{
1807-
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
1808-
WithSelector(*selectorForMachineDeploymentMHC(md)).
1809-
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1810-
{
1811-
Type: corev1.NodeReady,
1812-
Status: corev1.ConditionUnknown,
1813-
Timeout: metav1.Duration{Duration: 5 * time.Minute},
1814-
},
1815-
}).
1816-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1817-
WithClusterName("cluster1").
1808+
mhcBuilder.
18181809
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
18191810
Build()},
18201811
},
@@ -1826,141 +1817,43 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) {
18261817
// MHC is added in the desired state of the MachineDeployment
18271818
desired: []*scope.MachineDeploymentState{
18281819
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
1829-
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
1830-
WithSelector(*selectorForMachineDeploymentMHC(md)).
1831-
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1832-
{
1833-
Type: corev1.NodeReady,
1834-
Status: corev1.ConditionUnknown,
1835-
Timeout: metav1.Duration{Duration: 5 * time.Minute},
1836-
},
1837-
}).
1838-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1839-
WithClusterName("cluster1").
1840-
Build()),
1820+
mhcBuilder.Clone().Build()),
18411821
},
18421822
want: []*clusterv1.MachineHealthCheck{
1843-
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
1844-
WithSelector(*selectorForMachineDeploymentMHC(md)).
1845-
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1846-
{
1847-
Type: corev1.NodeReady,
1848-
Status: corev1.ConditionUnknown,
1849-
Timeout: metav1.Duration{Duration: 5 * time.Minute},
1850-
},
1851-
}).
1852-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1853-
WithClusterName("cluster1").
1854-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1855-
Build()}},
1823+
mhcBuilder.Clone().Build()}},
18561824
{
18571825
name: "Update MachineHealthCheck spec adding a field if the spec adds a field",
18581826
current: []*scope.MachineDeploymentState{
18591827
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
1860-
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
1861-
WithSelector(*selectorForMachineDeploymentMHC(md)).
1862-
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1863-
{
1864-
Type: corev1.NodeReady,
1865-
Status: corev1.ConditionUnknown,
1866-
Timeout: metav1.Duration{Duration: 5 * time.Minute},
1867-
},
1868-
}).
1869-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1870-
WithClusterName("cluster1").
1871-
Build()),
1828+
mhcBuilder.Clone().Build()),
18721829
},
18731830
desired: []*scope.MachineDeploymentState{
18741831
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
1875-
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
1876-
WithSelector(*selectorForMachineDeploymentMHC(md)).
1877-
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1878-
{
1879-
Type: corev1.NodeReady,
1880-
Status: corev1.ConditionUnknown,
1881-
Timeout: metav1.Duration{Duration: 5 * time.Minute},
1882-
},
1883-
}).
1884-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1885-
WithClusterName("cluster1").WithMaxUnhealthy(&maxUnhealthy).
1886-
Build())},
1832+
mhcBuilder.Clone().WithMaxUnhealthy(&maxUnhealthy).Build())},
18871833
want: []*clusterv1.MachineHealthCheck{
1888-
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
1889-
WithSelector(*selectorForMachineDeploymentMHC(md)).
1890-
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1891-
{
1892-
Type: corev1.NodeReady,
1893-
Status: corev1.ConditionUnknown,
1894-
Timeout: metav1.Duration{Duration: 5 * time.Minute},
1895-
},
1896-
}).
1834+
mhcBuilder.Clone().
18971835
WithMaxUnhealthy(&maxUnhealthy).
1898-
WithClusterName("cluster1").
1899-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
19001836
Build()},
19011837
},
19021838
{
19031839
name: "Update MachineHealthCheck spec removing a field if the spec removes a field",
19041840
current: []*scope.MachineDeploymentState{
19051841
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
1906-
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
1907-
WithSelector(*selectorForMachineDeploymentMHC(md)).
1908-
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1909-
{
1910-
Type: corev1.NodeReady,
1911-
Status: corev1.ConditionUnknown,
1912-
Timeout: metav1.Duration{Duration: 5 * time.Minute},
1913-
},
1914-
}).
1915-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1916-
WithClusterName("cluster1").WithMaxUnhealthy(&maxUnhealthy).
1917-
Build()),
1842+
mhcBuilder.Clone().WithMaxUnhealthy(&maxUnhealthy).Build()),
19181843
},
19191844
desired: []*scope.MachineDeploymentState{
19201845
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
1921-
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
1922-
WithSelector(*selectorForMachineDeploymentMHC(md)).
1923-
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1924-
{
1925-
Type: corev1.NodeReady,
1926-
Status: corev1.ConditionUnknown,
1927-
Timeout: metav1.Duration{Duration: 5 * time.Minute},
1928-
},
1929-
}).
1930-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1931-
WithClusterName("cluster1").
1932-
Build()),
1846+
mhcBuilder.Clone().Build()),
19331847
},
19341848
want: []*clusterv1.MachineHealthCheck{
1935-
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
1936-
WithSelector(*selectorForMachineDeploymentMHC(md)).
1937-
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1938-
{
1939-
Type: corev1.NodeReady,
1940-
Status: corev1.ConditionUnknown,
1941-
Timeout: metav1.Duration{Duration: 5 * time.Minute},
1942-
},
1943-
}).
1944-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1945-
WithClusterName("cluster1").
1946-
Build()},
1849+
mhcBuilder.Clone().Build(),
1850+
},
19471851
},
19481852
{
19491853
name: "Delete MachineHealthCheck spec if the MachineDeployment is modified to remove an existing one",
19501854
current: []*scope.MachineDeploymentState{
19511855
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
1952-
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
1953-
WithSelector(*selectorForMachineDeploymentMHC(md)).
1954-
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1955-
{
1956-
Type: corev1.NodeReady,
1957-
Status: corev1.ConditionUnknown,
1958-
Timeout: metav1.Duration{Duration: 5 * time.Minute},
1959-
},
1960-
}).
1961-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1962-
WithClusterName("cluster1").
1963-
Build()),
1856+
mhcBuilder.Clone().Build()),
19641857
},
19651858
desired: []*scope.MachineDeploymentState{newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, nil)},
19661859
want: []*clusterv1.MachineHealthCheck{},
@@ -1969,18 +1862,7 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) {
19691862
name: "Delete MachineHealthCheck spec if the MachineDeployment is deleted",
19701863
current: []*scope.MachineDeploymentState{
19711864
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
1972-
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
1973-
WithSelector(*selectorForMachineDeploymentMHC(md)).
1974-
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1975-
{
1976-
Type: corev1.NodeReady,
1977-
Status: corev1.ConditionUnknown,
1978-
Timeout: metav1.Duration{Duration: 5 * time.Minute},
1979-
},
1980-
}).
1981-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1982-
WithClusterName("cluster1").
1983-
Build()),
1865+
mhcBuilder.Clone().Build()),
19841866
},
19851867
desired: []*scope.MachineDeploymentState{},
19861868
want: []*clusterv1.MachineHealthCheck{},
@@ -2082,36 +1964,36 @@ func TestReconciler_reconcileMachineHealthCheck(t *testing.T) {
20821964
{
20831965
name: "Create a MachineHealthCheck",
20841966
current: nil,
2085-
desired: mhcBuilder.Build(),
2086-
want: mhcBuilder.Build(),
1967+
desired: mhcBuilder.Clone().Build(),
1968+
want: mhcBuilder.Clone().Build(),
20871969
},
20881970
{
20891971
name: "Successfully create a valid Ownerreference on the MachineHealthCheck",
20901972
current: nil,
20911973
// update the unhealthy conditions in the MachineHealthCheck
2092-
desired: mhcBuilder.
1974+
desired: mhcBuilder.Clone().
20931975
// Desired object has an incomplete owner reference which has no UID.
20941976
WithOwnerReferences([]metav1.OwnerReference{{Name: cp.GetName(), Kind: cp.GetKind(), APIVersion: cp.GetAPIVersion()}}).
20951977
Build(),
20961978
// Want a reconciled object with a full ownerReference including UID
2097-
want: mhcBuilder.
1979+
want: mhcBuilder.Clone().
20981980
WithOwnerReferences([]metav1.OwnerReference{{Name: cp.GetName(), Kind: cp.GetKind(), APIVersion: cp.GetAPIVersion(), UID: cp.GetUID()}}).
20991981
Build(),
21001982
wantErr: false,
21011983
},
21021984

21031985
{
21041986
name: "Update a MachineHealthCheck with changes",
2105-
current: mhcBuilder.Build(),
1987+
current: mhcBuilder.Clone().Build(),
21061988
// update the unhealthy conditions in the MachineHealthCheck
2107-
desired: mhcBuilder.WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1989+
desired: mhcBuilder.Clone().WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
21081990
{
21091991
Type: corev1.NodeReady,
21101992
Status: corev1.ConditionUnknown,
21111993
Timeout: metav1.Duration{Duration: 1000 * time.Minute},
21121994
},
21131995
}).Build(),
2114-
want: mhcBuilder.WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1996+
want: mhcBuilder.Clone().WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
21151997
{
21161998
Type: corev1.NodeReady,
21171999
Status: corev1.ConditionUnknown,
@@ -2121,14 +2003,14 @@ func TestReconciler_reconcileMachineHealthCheck(t *testing.T) {
21212003
},
21222004
{
21232005
name: "Don't change a MachineHealthCheck with no difference between desired and current",
2124-
current: mhcBuilder.Build(),
2006+
current: mhcBuilder.Clone().Build(),
21252007
// update the unhealthy conditions in the MachineHealthCheck
2126-
desired: mhcBuilder.Build(),
2127-
want: mhcBuilder.Build(),
2008+
desired: mhcBuilder.Clone().Build(),
2009+
want: mhcBuilder.Clone().Build(),
21282010
},
21292011
{
21302012
name: "Delete a MachineHealthCheck",
2131-
current: mhcBuilder.Build(),
2013+
current: mhcBuilder.Clone().Build(),
21322014
// update the unhealthy conditions in the MachineHealthCheck
21332015
desired: nil,
21342016
want: nil,

internal/test/builder/builders.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,3 +990,29 @@ func (m *MachineHealthCheckBuilder) Build() *clusterv1.MachineHealthCheck {
990990
},
991991
}
992992
}
993+
994+
// Clone returns a copy of the MachineHealthCheckBuilder.
995+
func (m *MachineHealthCheckBuilder) Clone() *MachineHealthCheckBuilder {
996+
out := &MachineHealthCheckBuilder{
997+
name: m.name,
998+
namespace: m.namespace,
999+
clusterName: m.clusterName,
1000+
selector: *m.selector.DeepCopy(),
1001+
}
1002+
1003+
if m.maxUnhealthy != nil {
1004+
maxUnhealthy := *m.maxUnhealthy
1005+
out.maxUnhealthy = &maxUnhealthy
1006+
}
1007+
1008+
if len(m.conditions) > 0 {
1009+
out.conditions = make([]clusterv1.UnhealthyCondition, len(m.conditions))
1010+
copy(out.conditions, m.conditions)
1011+
}
1012+
if len(m.ownerRefs) > 0 {
1013+
for _, ref := range m.ownerRefs {
1014+
out.ownerRefs = append(out.ownerRefs, *ref.DeepCopy())
1015+
}
1016+
}
1017+
return out
1018+
}

0 commit comments

Comments
 (0)