@@ -96,6 +96,11 @@ type KubeadmControlPlaneReconciler struct {
9696 managementCluster internal.ManagementCluster
9797 managementClusterUncached internal.ManagementCluster
9898 ssaCache ssa.Cache
99+
100+ // Only used for testing
101+ overrideTryInPlaceUpdateFunc func (ctx context.Context , controlPlane * internal.ControlPlane , machineToInPlaceUpdate * clusterv1.Machine , machineUpToDateResult internal.UpToDateResult ) (bool , ctrl.Result , error )
102+ overrideScaleUpControlPlaneFunc func (ctx context.Context , controlPlane * internal.ControlPlane ) (ctrl.Result , error )
103+ overrideScaleDownControlPlaneFunc func (ctx context.Context , controlPlane * internal.ControlPlane , machineToDelete * clusterv1.Machine ) (ctrl.Result , error )
99104}
100105
101106func (r * KubeadmControlPlaneReconciler ) SetupWithManager (ctx context.Context , mgr ctrl.Manager , options controller.Options ) error {
@@ -469,16 +474,16 @@ func (r *KubeadmControlPlaneReconciler) reconcile(ctx context.Context, controlPl
469474 }
470475
471476 // Control plane machines rollout due to configuration changes (e.g. upgrades) takes precedence over other operations.
472- machinesNeedingRollout , machinesNeedingRolloutResults := controlPlane .MachinesNeedingRollout ()
477+ machinesNeedingRollout , machinesUpToDateResults := controlPlane .MachinesNeedingRollout ()
473478 switch {
474479 case len (machinesNeedingRollout ) > 0 :
475480 var allMessages []string
476- for machine , machinesNeedingRolloutResult := range machinesNeedingRolloutResults {
477- allMessages = append (allMessages , fmt .Sprintf ("Machine %s needs rollout: %s" , machine , strings .Join (machinesNeedingRolloutResult .LogMessages , "," )))
481+ for machine , machineUpToDateResult := range machinesUpToDateResults {
482+ allMessages = append (allMessages , fmt .Sprintf ("Machine %s needs rollout: %s" , machine , strings .Join (machineUpToDateResult .LogMessages , "," )))
478483 }
479484 log .Info (fmt .Sprintf ("Rolling out Control Plane machines: %s" , strings .Join (allMessages , "," )), "machinesNeedingRollout" , machinesNeedingRollout .Names ())
480485 v1beta1conditions .MarkFalse (controlPlane .KCP , controlplanev1 .MachinesSpecUpToDateV1Beta1Condition , controlplanev1 .RollingUpdateInProgressV1Beta1Reason , clusterv1 .ConditionSeverityWarning , "Rolling %d replicas with outdated spec (%d replicas up to date)" , len (machinesNeedingRollout ), len (controlPlane .Machines )- len (machinesNeedingRollout ))
481- return r .upgradeControlPlane (ctx , controlPlane , machinesNeedingRollout )
486+ return r .updateControlPlane (ctx , controlPlane , machinesNeedingRollout , machinesUpToDateResults )
482487 default :
483488 // make sure last upgrade operation is marked as completed.
484489 // NOTE: we are checking the condition already exists in order to avoid to set this condition at the first
@@ -508,7 +513,12 @@ func (r *KubeadmControlPlaneReconciler) reconcile(ctx context.Context, controlPl
508513 case numMachines > desiredReplicas :
509514 log .Info ("Scaling down control plane" , "desired" , desiredReplicas , "existing" , numMachines )
510515 // The last parameter (i.e. machines needing to be rolled out) should always be empty here.
511- return r .scaleDownControlPlane (ctx , controlPlane , collections.Machines {})
516+ // Pick the Machine that we should scale down.
517+ machineToDelete , err := selectMachineForInPlaceUpdateOrScaleDown (ctx , controlPlane , collections.Machines {})
518+ if err != nil {
519+ return ctrl.Result {}, errors .Wrap (err , "failed to select machine for scale down" )
520+ }
521+ return r .scaleDownControlPlane (ctx , controlPlane , machineToDelete )
512522 }
513523
514524 // Get the workload cluster client.
@@ -977,16 +987,16 @@ func (r *KubeadmControlPlaneReconciler) reconcileControlPlaneAndMachinesConditio
977987}
978988
979989func reconcileMachineUpToDateCondition (_ context.Context , controlPlane * internal.ControlPlane ) {
980- machinesNotUptoDate , machinesNotUpToDateResults := controlPlane .NotUpToDateMachines ()
990+ machinesNotUptoDate , machinesUpToDateResults := controlPlane .NotUpToDateMachines ()
981991 machinesNotUptoDateNames := sets .New (machinesNotUptoDate .Names ()... )
982992
983993 for _ , machine := range controlPlane .Machines {
984994 if machinesNotUptoDateNames .Has (machine .Name ) {
985995 // Note: the code computing the message for KCP's RolloutOut condition is making assumptions on the format/content of this message.
986996 message := ""
987- if machinesNotUpToDateResult , ok := machinesNotUpToDateResults [machine .Name ]; ok && len (machinesNotUpToDateResult .ConditionMessages ) > 0 {
997+ if machineUpToDateResult , ok := machinesUpToDateResults [machine .Name ]; ok && len (machineUpToDateResult .ConditionMessages ) > 0 {
988998 var reasons []string
989- for _ , conditionMessage := range machinesNotUpToDateResult .ConditionMessages {
999+ for _ , conditionMessage := range machineUpToDateResult .ConditionMessages {
9901000 reasons = append (reasons , fmt .Sprintf ("* %s" , conditionMessage ))
9911001 }
9921002 message = strings .Join (reasons , "\n " )
0 commit comments