From 1772af44e96257c1bc2cb1d8027abb8cc5e363f9 Mon Sep 17 00:00:00 2001 From: killianmuldoon Date: Wed, 19 Jan 2022 13:40:17 +0000 Subject: [PATCH] add defaulting for MHC in desired state computation --- internal/controllers/topology/cluster/desired_state.go | 6 +++++- .../controllers/topology/cluster/desired_state_test.go | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/controllers/topology/cluster/desired_state.go b/internal/controllers/topology/cluster/desired_state.go index d4585207a7b8..4ccaabfa8e01 100644 --- a/internal/controllers/topology/cluster/desired_state.go +++ b/internal/controllers/topology/cluster/desired_state.go @@ -738,7 +738,7 @@ func ownerReferenceTo(obj client.Object) *metav1.OwnerReference { func computeMachineHealthCheck(healthCheckTarget client.Object, selector *metav1.LabelSelector, clusterName string, check *clusterv1.MachineHealthCheckClass) *clusterv1.MachineHealthCheck { // Create a MachineHealthCheck with the spec given in the ClusterClass. - return &clusterv1.MachineHealthCheck{ + mhc := &clusterv1.MachineHealthCheck{ TypeMeta: metav1.TypeMeta{ Kind: clusterv1.GroupVersion.WithKind("MachineHealthCheck").Kind, APIVersion: clusterv1.GroupVersion.String(), @@ -757,6 +757,10 @@ func computeMachineHealthCheck(healthCheckTarget client.Object, selector *metav1 RemediationTemplate: check.RemediationTemplate, }, } + // Default all fields in the MachineHealthCheck using the same function called in the webhook. This ensures the desired + // state of the object won't be different from the current state due to webhook Defaulting. + mhc.Default() + return mhc } func selectorForControlPlaneMHC() *metav1.LabelSelector { diff --git a/internal/controllers/topology/cluster/desired_state_test.go b/internal/controllers/topology/cluster/desired_state_test.go index 5f6a4dffd4c6..04459395f5db 100644 --- a/internal/controllers/topology/cluster/desired_state_test.go +++ b/internal/controllers/topology/cluster/desired_state_test.go @@ -21,6 +21,8 @@ import ( "testing" "time" + "k8s.io/apimachinery/pkg/util/intstr" + "github.com/google/go-cmp/cmp" . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" @@ -1430,6 +1432,7 @@ func TestMergeMap(t *testing.T) { } func Test_computeMachineHealthCheck(t *testing.T) { + maxUnhealthyValue := intstr.FromString("100%") mhcSpec := &clusterv1.MachineHealthCheckClass{ UnhealthyConditions: []clusterv1.UnhealthyCondition{ { @@ -1446,7 +1449,6 @@ func Test_computeMachineHealthCheck(t *testing.T) { NodeStartupTimeout: &metav1.Duration{ Duration: time.Duration(1)}, } - selector := &metav1.LabelSelector{MatchLabels: map[string]string{ "foo": "bar", }} @@ -1460,12 +1462,16 @@ func Test_computeMachineHealthCheck(t *testing.T) { ObjectMeta: metav1.ObjectMeta{ Name: "md1", Namespace: "ns1", + // Label is added by defaulting values using MachineHealthCheck.Default() + Labels: map[string]string{"cluster.x-k8s.io/cluster-name": "cluster1"}, }, Spec: clusterv1.MachineHealthCheckSpec{ ClusterName: "cluster1", Selector: metav1.LabelSelector{MatchLabels: map[string]string{ "foo": "bar", }}, + // MaxUnhealthy is added by defaulting values using MachineHealthCheck.Default() + MaxUnhealthy: &maxUnhealthyValue, UnhealthyConditions: []clusterv1.UnhealthyCondition{ { Type: corev1.NodeReady,