Skip to content

Commit 07f2337

Browse files
authored
Merge pull request #12464 from sbueringer/pr-remove-explicit-defaulting
🌱 Remove explicit defaulting of MS deletePolicy, MHC maxUnhealthy, KCPTemplate rolloutStrategy
2 parents 99b1c75 + 9d5e8cc commit 07f2337

File tree

10 files changed

+13
-29
lines changed

10 files changed

+13
-29
lines changed

controlplane/kubeadm/internal/webhooks/kubeadmcontrolplanetemplate.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ func (webhook *KubeadmControlPlaneTemplate) Default(_ context.Context, obj runti
5959

6060
k.Spec.Template.Spec.KubeadmConfigSpec.Default()
6161

62-
k.Spec.Template.Spec.RolloutStrategy = defaultRolloutStrategy(k.Spec.Template.Spec.RolloutStrategy)
63-
6462
return nil
6563
}
6664

controlplane/kubeadm/internal/webhooks/kubeadmcontrolplanetemplate_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ func TestKubeadmControlPlaneTemplateDefault(t *testing.T) {
5858
g.Expect(webhook.Default(ctx, kcpTemplate)).To(Succeed())
5959

6060
g.Expect(kcpTemplate.Spec.Template.Spec.KubeadmConfigSpec.Format).To(Equal(bootstrapv1.CloudConfig))
61-
g.Expect(kcpTemplate.Spec.Template.Spec.RolloutStrategy.Type).To(Equal(controlplanev1.RollingUpdateStrategyType))
62-
g.Expect(kcpTemplate.Spec.Template.Spec.RolloutStrategy.RollingUpdate.MaxSurge.IntVal).To(Equal(int32(1)))
6361
}
6462

6563
func TestKubeadmControlPlaneTemplateValidationFeatureGateEnabled(t *testing.T) {

exp/topology/desiredstate/desired_state_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3339,7 +3339,6 @@ func TestMergeMap(t *testing.T) {
33393339
}
33403340

33413341
func Test_computeMachineHealthCheck(t *testing.T) {
3342-
maxUnhealthyValue := intstr.FromString("100%")
33433342
mhcSpec := &clusterv1.MachineHealthCheckClass{
33443343
UnhealthyNodeConditions: []clusterv1.UnhealthyNodeCondition{
33453344
{
@@ -3382,8 +3381,6 @@ func Test_computeMachineHealthCheck(t *testing.T) {
33823381
Selector: metav1.LabelSelector{MatchLabels: map[string]string{
33833382
"foo": "bar",
33843383
}},
3385-
// MaxUnhealthy is added by defaulting values using MachineHealthCheck.Default()
3386-
MaxUnhealthy: &maxUnhealthyValue,
33873384
UnhealthyNodeConditions: []clusterv1.UnhealthyNodeCondition{
33883385
{
33893386
Type: corev1.NodeReady,

internal/controllers/machinehealthcheck/machinehealthcheck_controller.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ const (
7171
totalTargetKeyLog = "totalTarget"
7272
)
7373

74+
var (
75+
defaultMaxUnhealthy = intstr.FromString("100%")
76+
)
77+
7478
// +kubebuilder:rbac:groups=core,resources=events,verbs=create;patch
7579
// +kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch
7680
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machines;machines/status,verbs=get;list;watch;delete
@@ -251,16 +255,17 @@ func (r *Reconciler) reconcile(ctx context.Context, logger logr.Logger, cluster
251255
var message string
252256

253257
if m.Spec.UnhealthyRange == "" {
258+
maxUnhealthyValue := ptr.To(ptr.Deref(m.Spec.MaxUnhealthy, defaultMaxUnhealthy)).String()
254259
logger.V(3).Info(
255260
"Short-circuiting remediation",
256261
totalTargetKeyLog, totalTargets,
257-
maxUnhealthyKeyLog, m.Spec.MaxUnhealthy,
262+
maxUnhealthyKeyLog, maxUnhealthyValue,
258263
unhealthyTargetsKeyLog, len(unhealthy),
259264
)
260265
message = fmt.Sprintf("Remediation is not allowed, the number of not started or unhealthy machines exceeds maxUnhealthy (total: %v, unhealthy: %v, maxUnhealthy: %v)",
261266
totalTargets,
262267
len(unhealthy),
263-
m.Spec.MaxUnhealthy)
268+
maxUnhealthyValue)
264269
} else {
265270
logger.V(3).Info(
266271
"Short-circuiting remediation",
@@ -328,7 +333,7 @@ func (r *Reconciler) reconcile(ctx context.Context, logger logr.Logger, cluster
328333
logger.V(3).Info(
329334
"Remediations are allowed",
330335
totalTargetKeyLog, totalTargets,
331-
maxUnhealthyKeyLog, m.Spec.MaxUnhealthy,
336+
maxUnhealthyKeyLog, ptr.To(ptr.Deref(m.Spec.MaxUnhealthy, defaultMaxUnhealthy)).String(),
332337
unhealthyTargetsKeyLog, len(unhealthy),
333338
)
334339
} else {
@@ -706,10 +711,7 @@ func getUnhealthyRange(mhc *clusterv1.MachineHealthCheck) (int, int, error) {
706711
}
707712

708713
func getMaxUnhealthy(mhc *clusterv1.MachineHealthCheck) (int, error) {
709-
if mhc.Spec.MaxUnhealthy == nil {
710-
return 0, errors.New("spec.maxUnhealthy must be set")
711-
}
712-
maxUnhealthy, err := intstr.GetScaledValueFromIntOrPercent(mhc.Spec.MaxUnhealthy, int(ptr.Deref[int32](mhc.Status.ExpectedMachines, 0)), false)
714+
maxUnhealthy, err := intstr.GetScaledValueFromIntOrPercent(ptr.To(ptr.Deref(mhc.Spec.MaxUnhealthy, defaultMaxUnhealthy)), int(ptr.Deref[int32](mhc.Status.ExpectedMachines, 0)), false)
713715
if err != nil {
714716
return 0, err
715717
}

internal/controllers/machinehealthcheck/machinehealthcheck_controller_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,7 +2279,7 @@ func TestIsAllowedRemediation(t *testing.T) {
22792279
maxUnhealthy: nil,
22802280
expectedMachines: int32(3),
22812281
currentHealthy: int32(0),
2282-
allowed: false,
2282+
allowed: true,
22832283
},
22842284
{
22852285
name: "when maxUnhealthy is not an int or percentage",
@@ -2365,9 +2365,8 @@ func TestGetMaxUnhealthy(t *testing.T) {
23652365
{
23662366
name: "when maxUnhealthy is nil",
23672367
maxUnhealthy: nil,
2368-
expectedMaxUnhealthy: 0,
2368+
expectedMaxUnhealthy: 7,
23692369
actualMachineCount: 7,
2370-
expectedErr: errors.New("spec.maxUnhealthy must be set"),
23712370
},
23722371
{
23732372
name: "when maxUnhealthy is not an int or percentage",

internal/controllers/machineset/machineset_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package machineset
1818

1919
import (
20+
"cmp"
2021
"context"
2122
"fmt"
2223
"math"
@@ -825,7 +826,7 @@ func (r *Reconciler) syncReplicas(ctx context.Context, s *scope) (ctrl.Result, e
825826
}
826827
return ctrl.Result{}, r.waitForMachineCreation(ctx, machineList)
827828
case diff > 0:
828-
log.Info(fmt.Sprintf("MachineSet is scaling down to %d replicas by deleting %d machines", *(ms.Spec.Replicas), diff), "replicas", *(ms.Spec.Replicas), "machineCount", len(machines), "deletePolicy", ms.Spec.DeletePolicy)
829+
log.Info(fmt.Sprintf("MachineSet is scaling down to %d replicas by deleting %d machines", *(ms.Spec.Replicas), diff), "replicas", *(ms.Spec.Replicas), "machineCount", len(machines), "deletePolicy", cmp.Or(ms.Spec.DeletePolicy, clusterv1.RandomMachineSetDeletePolicy))
829830

830831
deletePriorityFunc, err := getDeletePriorityFunc(ms)
831832
if err != nil {

internal/webhooks/machinehealthcheck.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,6 @@ func (webhook *MachineHealthCheck) Default(_ context.Context, obj runtime.Object
7777
}
7878
m.Labels[clusterv1.ClusterNameLabel] = m.Spec.ClusterName
7979

80-
if m.Spec.MaxUnhealthy == nil {
81-
defaultMaxUnhealthy := intstr.FromString("100%")
82-
m.Spec.MaxUnhealthy = &defaultMaxUnhealthy
83-
}
84-
8580
if m.Spec.NodeStartupTimeoutSeconds == nil {
8681
m.Spec.NodeStartupTimeoutSeconds = &clusterv1.DefaultNodeStartupTimeoutSeconds
8782
}

internal/webhooks/machinehealthcheck_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func TestMachineHealthCheckDefault(t *testing.T) {
5353
g.Expect(webhook.Default(ctx, mhc)).To(Succeed())
5454

5555
g.Expect(mhc.Labels[clusterv1.ClusterNameLabel]).To(Equal(mhc.Spec.ClusterName))
56-
g.Expect(mhc.Spec.MaxUnhealthy.String()).To(Equal("100%"))
5756
g.Expect(mhc.Spec.NodeStartupTimeoutSeconds).ToNot(BeNil())
5857
g.Expect(*mhc.Spec.NodeStartupTimeoutSeconds).To(Equal(int32(10 * 60)))
5958
}

internal/webhooks/machineset.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,6 @@ func (webhook *MachineSet) Default(ctx context.Context, obj runtime.Object) erro
103103
}
104104
m.Spec.Replicas = ptr.To[int32](replicas)
105105

106-
if m.Spec.DeletePolicy == "" {
107-
m.Spec.DeletePolicy = clusterv1.RandomMachineSetDeletePolicy
108-
}
109-
110106
if m.Spec.Selector.MatchLabels == nil {
111107
m.Spec.Selector.MatchLabels = make(map[string]string)
112108
}

internal/webhooks/machineset_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ func TestMachineSetDefault(t *testing.T) {
6060
g.Expect(webhook.Default(reqCtx, ms)).To(Succeed())
6161

6262
g.Expect(ms.Labels[clusterv1.ClusterNameLabel]).To(Equal(ms.Spec.ClusterName))
63-
g.Expect(ms.Spec.DeletePolicy).To(Equal(clusterv1.RandomMachineSetDeletePolicy))
6463
g.Expect(ms.Spec.Selector.MatchLabels).To(HaveKeyWithValue(clusterv1.MachineSetNameLabel, "test-ms"))
6564
g.Expect(ms.Spec.Template.Labels).To(HaveKeyWithValue(clusterv1.MachineSetNameLabel, "test-ms"))
6665
g.Expect(ms.Spec.Template.Spec.Version).To(Equal("v1.19.10"))

0 commit comments

Comments
 (0)