Skip to content

Commit

Permalink
add defaulting for MHC in desired state computation
Browse files Browse the repository at this point in the history
  • Loading branch information
killianmuldoon committed Jan 19, 2022
1 parent f47ef28 commit 1772af4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 5 additions & 1 deletion internal/controllers/topology/cluster/desired_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion internal/controllers/topology/cluster/desired_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
{
Expand All @@ -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",
}}
Expand All @@ -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,
Expand Down

0 comments on commit 1772af4

Please sign in to comment.