@@ -19,6 +19,7 @@ package machinedeployment
1919import (
2020 "context"
2121 "fmt"
22+ "sort"
2223 "strings"
2324
2425 "github.com/pkg/errors"
@@ -332,24 +333,53 @@ func (r *Reconciler) reconcile(ctx context.Context, s *scope) error {
332333// Note: When the newMS has been created by the rollout planner, also wait for the cache to be up to date.
333334func (r * Reconciler ) createOrUpdateMachineSetsAndSyncMachineDeploymentRevision (ctx context.Context , p * rolloutPlanner ) error {
334335 log := ctrl .LoggerFrom (ctx )
335- allMSs := append (p .oldMSs , p .newMS )
336336
337- for _ , ms := range allMSs {
338- log = log .WithValues ("MachineSet" , klog .KObj (ms ))
339- ctx = ctrl .LoggerInto (ctx , log )
337+ // Note: newMS goes first so in the logs we will have first create/scale up newMS, then scale down oldMSs
338+ allMSs := append ([]* clusterv1.MachineSet {p .newMS }, p .oldMSs ... )
340339
340+ // Get all the diff introduced by the rollout planner.
341+ // Note: collect all the diff first, so for each change we can add an overview of all the MachineSets
342+ // in the MachineDeployment, because those info are required to understand why a change happened.
343+ machineSetsDiff := map [string ]machineSetDiff {}
344+ machineSetsSummary := map [string ]string {}
345+ for _ , ms := range allMSs {
346+ // Update spec.Replicas in the MachineSet.
341347 if scaleIntent , ok := p .scaleIntents [ms .Name ]; ok {
342- ms .Spec .Replicas = & scaleIntent
348+ ms .Spec .Replicas = ptr . To ( scaleIntent )
343349 }
344350
345- if ms .GetUID () == "" {
351+ diff := p .getMachineSetDiff (ms )
352+ machineSetsDiff [ms .Name ] = diff
353+ machineSetsSummary [ms .Name ] = diff .Summary
354+ }
355+
356+ // Apply changes to MachineSets.
357+ for _ , ms := range allMSs {
358+ log := log .WithValues ("MachineSet" , klog .KObj (ms ))
359+ ctx := ctrl .LoggerInto (ctx , log )
360+
361+ // Retrieve the diff for the MachineSet.
362+ // Note: no need to check for diff doesn't exist, all the diff have been computed right above.
363+ diff := machineSetsDiff [ms .Name ]
364+
365+ // Add to the log kv pairs providing the overview of all the MachineSets computed above.
366+ // Note: This value should not be added to the context to prevent propagation to other func.
367+ log = log .WithValues ("machineSets" , machineSetsSummary )
368+
369+ if diff .OriginalMS == nil {
346370 // Create the MachineSet.
347371 if err := ssa .Patch (ctx , r .Client , machineDeploymentManagerName , ms ); err != nil {
348372 r .recorder .Eventf (p .md , corev1 .EventTypeWarning , "FailedCreate" , "Failed to create MachineSet %s: %v" , klog .KObj (ms ), err )
349373 return errors .Wrapf (err , "failed to create MachineSet %s" , klog .KObj (ms ))
350374 }
351- log .Info (fmt .Sprintf ("MachineSet created (%s)" , p .createReason ))
352- r .recorder .Eventf (p .md , corev1 .EventTypeNormal , "SuccessfulCreate" , "Created MachineSet %s with %d replicas" , klog .KObj (ms ), ptr .Deref (ms .Spec .Replicas , 0 ))
375+ if len (p .oldMSs ) > 0 {
376+ log .Info (fmt .Sprintf ("MachineSets need rollout: %s" , strings .Join (machineSetNames (p .oldMSs ), ", " )), "reason" , p .createReason )
377+ }
378+ log .Info (fmt .Sprintf ("MachineSet %s created, it is now the current MachineSet" , ms .Name ))
379+ if diff .DesiredReplicas > 0 {
380+ log .Info (fmt .Sprintf ("Scaled up current MachineSet %s from 0 to %d replicas (+%[2]d)" , ms .Name , diff .DesiredReplicas ))
381+ }
382+ r .recorder .Eventf (p .md , corev1 .EventTypeNormal , "SuccessfulCreate" , "Created MachineSet %s with %d replicas" , klog .KObj (ms ), diff .DesiredReplicas )
353383
354384 // Keep trying to get the MachineSet. This will force the cache to update and prevent any future reconciliation of
355385 // the MachineDeployment to reconcile with an outdated list of MachineSets which could lead to unwanted creation of
@@ -361,14 +391,21 @@ func (r *Reconciler) createOrUpdateMachineSetsAndSyncMachineDeploymentRevision(c
361391 continue
362392 }
363393
364- // Update the MachineSet to propagate in-place mutable fields from the MachineDeployment and/or changes applied by the rollout planner.
365- originalMS , ok := p .originalMS [ms .Name ]
366- if ! ok {
367- return errors .Errorf ("failed to update MachineSet %s, original MS is missing" , klog .KObj (ms ))
394+ // Add to the log kv pairs providing context and details about changes in this MachineSet (reason, diff)
395+ // Note: Those values should not be added to the context to prevent propagation to other func.
396+ statusToLogKeyAndValues := []any {
397+ "reason" , diff .Reason ,
398+ "diff" , diff .OtherChanges ,
368399 }
369- originalReplicas := ptr .Deref (originalMS .Spec .Replicas , 0 )
400+ if len (p .acknowledgedMachineNames ) > 0 {
401+ statusToLogKeyAndValues = append (statusToLogKeyAndValues , "acknowledgedMachines" , sortAndJoin (p .acknowledgedMachineNames ))
402+ }
403+ if len (p .updatingMachineNames ) > 0 {
404+ statusToLogKeyAndValues = append (statusToLogKeyAndValues , "updatingMachines" , sortAndJoin (p .updatingMachineNames ))
405+ }
406+ log = log .WithValues (statusToLogKeyAndValues ... )
370407
371- err := ssa .Patch (ctx , r .Client , machineDeploymentManagerName , ms , ssa.WithCachingProxy {Cache : r .ssaCache , Original : originalMS })
408+ err := ssa .Patch (ctx , r .Client , machineDeploymentManagerName , ms , ssa.WithCachingProxy {Cache : r .ssaCache , Original : diff . OriginalMS })
372409 if err != nil {
373410 // Note: If we are Applying a MachineSet with UID set and the MachineSet does not exist anymore, the
374411 // kube-apiserver returns a conflict error.
@@ -379,25 +416,20 @@ func (r *Reconciler) createOrUpdateMachineSetsAndSyncMachineDeploymentRevision(c
379416 return errors .Wrapf (err , "failed to update MachineSet %s" , klog .KObj (ms ))
380417 }
381418
382- changes := getAnnotationChanges (originalMS , ms )
383-
384- newReplicas := ptr .Deref (ms .Spec .Replicas , 0 )
385- if newReplicas < originalReplicas {
386- changes = append (changes , fmt .Sprintf ("replicas %d" , newReplicas ))
387- log .Info (fmt .Sprintf ("Scaled down MachineSet %s to %d replicas (-%d)" , ms .Name , newReplicas , originalReplicas - newReplicas ), "diff" , strings .Join (changes , "," ))
388- r .recorder .Eventf (p .md , corev1 .EventTypeNormal , "SuccessfulScale" , "Scaled down MachineSet %v: %d -> %d" , ms .Name , originalReplicas , newReplicas )
419+ if diff .DesiredReplicas < diff .OriginalReplicas {
420+ log .Info (fmt .Sprintf ("Scaled down %s MachineSet %s from %d to %d replicas (-%d)" , diff .Type , ms .Name , diff .OriginalReplicas , diff .DesiredReplicas , diff .OriginalReplicas - diff .DesiredReplicas ))
421+ r .recorder .Eventf (p .md , corev1 .EventTypeNormal , "SuccessfulScale" , "Scaled down MachineSet %v: %d -> %d" , ms .Name , diff .OriginalReplicas , diff .DesiredReplicas )
389422 }
390- if newReplicas > originalReplicas {
391- changes = append (changes , fmt .Sprintf ("replicas %d" , newReplicas ))
392- log .Info (fmt .Sprintf ("Scaled up MachineSet %s to %d replicas (+%d)" , ms .Name , newReplicas , newReplicas - originalReplicas ), "diff" , strings .Join (changes , "," ))
393- r .recorder .Eventf (p .md , corev1 .EventTypeNormal , "SuccessfulScale" , "Scaled up MachineSet %v: %d -> %d" , ms .Name , originalReplicas , newReplicas )
423+ if diff .DesiredReplicas > diff .OriginalReplicas {
424+ log .Info (fmt .Sprintf ("Scaled up %s MachineSet %s from %d to %d replicas (+%d)" , diff .Type , ms .Name , diff .OriginalReplicas , diff .DesiredReplicas , diff .DesiredReplicas - diff .OriginalReplicas ))
425+ r .recorder .Eventf (p .md , corev1 .EventTypeNormal , "SuccessfulScale" , "Scaled up MachineSet %v: %d -> %d" , ms .Name , diff .OriginalReplicas , diff .DesiredReplicas )
394426 }
395- if newReplicas == originalReplicas && len ( changes ) > 0 {
396- log .Info (fmt .Sprintf ("Updated MachineSet %s" , ms . Name ), " diff" , strings . Join ( changes , "," ))
427+ if diff . DesiredReplicas == diff . OriginalReplicas && diff . OtherChanges != "" {
428+ log .Info (fmt .Sprintf ("Updated %s MachineSet %s" , diff . Type , ms . Name ))
397429 }
398430
399431 // Only wait for cache if the object was changed.
400- if originalMS .ResourceVersion != ms .ResourceVersion {
432+ if diff . OriginalMS .ResourceVersion != ms .ResourceVersion {
401433 if err := clientutil .WaitForCacheToBeUpToDate (ctx , r .Client , "MachineSet update" , ms ); err != nil {
402434 return err
403435 }
@@ -415,40 +447,13 @@ func (r *Reconciler) createOrUpdateMachineSetsAndSyncMachineDeploymentRevision(c
415447 return nil
416448}
417449
418- func getAnnotationChanges (originalMS * clusterv1.MachineSet , ms * clusterv1.MachineSet ) []string {
419- changes := []string {}
420- if originalMS .Annotations [clusterv1 .MachineSetMoveMachinesToMachineSetAnnotation ] != ms .Annotations [clusterv1 .MachineSetMoveMachinesToMachineSetAnnotation ] {
421- if value , ok := ms .Annotations [clusterv1 .MachineSetMoveMachinesToMachineSetAnnotation ]; ok {
422- changes = append (changes , fmt .Sprintf ("%s: %s" , clusterv1 .MachineSetMoveMachinesToMachineSetAnnotation , value ))
423- } else {
424- changes = append (changes , fmt .Sprintf ("%s removed" , clusterv1 .MachineSetMoveMachinesToMachineSetAnnotation ))
425- }
426- }
427-
428- if originalMS .Annotations [clusterv1 .MachineSetReceiveMachinesFromMachineSetsAnnotation ] != ms .Annotations [clusterv1 .MachineSetReceiveMachinesFromMachineSetsAnnotation ] {
429- if value , ok := ms .Annotations [clusterv1 .MachineSetReceiveMachinesFromMachineSetsAnnotation ]; ok {
430- changes = append (changes , fmt .Sprintf ("%s: %s" , clusterv1 .MachineSetReceiveMachinesFromMachineSetsAnnotation , value ))
431- } else {
432- changes = append (changes , fmt .Sprintf ("%s removed" , clusterv1 .MachineSetReceiveMachinesFromMachineSetsAnnotation ))
433- }
434- }
435-
436- if originalMS .Annotations [clusterv1 .AcknowledgedMoveAnnotation ] != ms .Annotations [clusterv1 .AcknowledgedMoveAnnotation ] {
437- if value , ok := ms .Annotations [clusterv1 .AcknowledgedMoveAnnotation ]; ok {
438- changes = append (changes , fmt .Sprintf ("%s: %s" , clusterv1 .AcknowledgedMoveAnnotation , value ))
439- } else {
440- changes = append (changes , fmt .Sprintf ("%s removed" , clusterv1 .AcknowledgedMoveAnnotation ))
441- }
442- }
443-
444- if originalMS .Annotations [clusterv1 .DisableMachineCreateAnnotation ] != ms .Annotations [clusterv1 .DisableMachineCreateAnnotation ] {
445- if value , ok := ms .Annotations [clusterv1 .DisableMachineCreateAnnotation ]; ok {
446- changes = append (changes , fmt .Sprintf ("%s: %s" , clusterv1 .DisableMachineCreateAnnotation , value ))
447- } else {
448- changes = append (changes , fmt .Sprintf ("%s removed" , clusterv1 .DisableMachineCreateAnnotation ))
449- }
450+ func machineSetNames (machineSets []* clusterv1.MachineSet ) []string {
451+ names := []string {}
452+ for _ , ms := range machineSets {
453+ names = append (names , ms .Name )
450454 }
451- return changes
455+ sort .Strings (names )
456+ return names
452457}
453458
454459func (r * Reconciler ) reconcileDelete (ctx context.Context , s * scope ) error {
0 commit comments