@@ -20,15 +20,13 @@ import (
2020 "context"
2121 "fmt"
2222 "strings"
23- "time"
2423
2524 "github.com/pkg/errors"
2625 corev1 "k8s.io/api/core/v1"
2726 apierrors "k8s.io/apimachinery/pkg/api/errors"
2827 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2928 "k8s.io/apimachinery/pkg/labels"
3029 kerrors "k8s.io/apimachinery/pkg/util/errors"
31- "k8s.io/apimachinery/pkg/util/wait"
3230 "k8s.io/client-go/tools/record"
3331 "k8s.io/klog/v2"
3432 "k8s.io/utils/ptr"
@@ -41,6 +39,7 @@ import (
4139
4240 clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
4341 "sigs.k8s.io/cluster-api/controllers/external"
42+ clientutil "sigs.k8s.io/cluster-api/internal/util/client"
4443 "sigs.k8s.io/cluster-api/internal/util/ssa"
4544 "sigs.k8s.io/cluster-api/util"
4645 "sigs.k8s.io/cluster-api/util/collections"
@@ -349,24 +348,10 @@ func (r *Reconciler) createOrUpdateMachineSetsAndSyncMachineDeploymentRevision(c
349348 // Keep trying to get the MachineSet. This will force the cache to update and prevent any future reconciliation of
350349 // the MachineDeployment to reconcile with an outdated list of MachineSets which could lead to unwanted creation of
351350 // a duplicate MachineSet.
352- var pollErrors []error
353- tmpMS := & clusterv1.MachineSet {}
354- if err := wait .PollUntilContextTimeout (ctx , 100 * time .Millisecond , 10 * time .Second , true , func (ctx context.Context ) (bool , error ) {
355- if err := r .Client .Get (ctx , client .ObjectKeyFromObject (ms ), tmpMS ); err != nil {
356- // Do not return error here. Continue to poll even if we hit an error
357- // so that we avoid existing because of transient errors like network flakes.
358- // Capture all the errors and return the aggregate error if the poll fails eventually.
359- pollErrors = append (pollErrors , err )
360- return false , nil
361- }
362- return true , nil
363- }); err != nil {
364- return errors .Wrapf (kerrors .NewAggregate (pollErrors ), "failed to get the MachineSet %s after creation" , klog .KObj (ms ))
351+ if err := clientutil .WaitForCacheToBeUpToDate (ctx , r .Client , "MachineSet creation" , ms ); err != nil {
352+ return err
365353 }
366354
367- // Report back creation timestamp, because legacy scale func uses it to sort machines.
368- // TODO(in-place): drop this as soon as handling of MD with paused rollouts is moved into rollout planner (see scale in machinedeployment_sync.go).
369- ms .CreationTimestamp = tmpMS .CreationTimestamp
370355 continue
371356 }
372357
@@ -387,17 +372,24 @@ func (r *Reconciler) createOrUpdateMachineSetsAndSyncMachineDeploymentRevision(c
387372
388373 newReplicas := ptr .Deref (ms .Spec .Replicas , 0 )
389374 if newReplicas < originalReplicas {
390- changes = append (changes , "replicas" , newReplicas )
391- log .Info (fmt .Sprintf ("Scaled down MachineSet %s to %d replicas (-%d)" , ms .Name , newReplicas , originalReplicas - newReplicas ), changes ... )
375+ changes = append (changes , fmt . Sprintf ( "replicas %d " , newReplicas ) )
376+ log .Info (fmt .Sprintf ("Scaled down MachineSet %s to %d replicas (-%d)" , ms .Name , newReplicas , originalReplicas - newReplicas ), "diff" , strings . Join ( changes , "," ) )
392377 r .recorder .Eventf (p .md , corev1 .EventTypeNormal , "SuccessfulScale" , "Scaled down MachineSet %v: %d -> %d" , ms .Name , originalReplicas , newReplicas )
393378 }
394379 if newReplicas > originalReplicas {
395- changes = append (changes , "replicas" , newReplicas )
396- log .Info (fmt .Sprintf ("Scaled up MachineSet %s to %d replicas (+%d)" , ms .Name , newReplicas , newReplicas - originalReplicas ), changes ... )
380+ changes = append (changes , fmt . Sprintf ( "replicas %d " , newReplicas ) )
381+ log .Info (fmt .Sprintf ("Scaled up MachineSet %s to %d replicas (+%d)" , ms .Name , newReplicas , newReplicas - originalReplicas ), "diff" , strings . Join ( changes , "," ) )
397382 r .recorder .Eventf (p .md , corev1 .EventTypeNormal , "SuccessfulScale" , "Scaled up MachineSet %v: %d -> %d" , ms .Name , originalReplicas , newReplicas )
398383 }
399384 if newReplicas == originalReplicas && len (changes ) > 0 {
400- log .Info (fmt .Sprintf ("MachineSet %s updated" , ms .Name ), changes ... )
385+ log .Info (fmt .Sprintf ("MachineSet %s updated" , ms .Name ), "diff" , strings .Join (changes , "," ))
386+ }
387+
388+ // Only wait for cache if the object was changed.
389+ if originalMS .ResourceVersion != ms .ResourceVersion {
390+ if err := clientutil .WaitForCacheToBeUpToDate (ctx , r .Client , "MachineSet update" , ms ); err != nil {
391+ return err
392+ }
401393 }
402394 }
403395
@@ -412,37 +404,37 @@ func (r *Reconciler) createOrUpdateMachineSetsAndSyncMachineDeploymentRevision(c
412404 return nil
413405}
414406
415- func getAnnotationChanges (originalMS * clusterv1.MachineSet , ms * clusterv1.MachineSet ) []any {
416- changes := []any {}
407+ func getAnnotationChanges (originalMS * clusterv1.MachineSet , ms * clusterv1.MachineSet ) []string {
408+ changes := []string {}
417409 if originalMS .Annotations [clusterv1 .MachineSetMoveMachinesToMachineSetAnnotation ] != ms .Annotations [clusterv1 .MachineSetMoveMachinesToMachineSetAnnotation ] {
418410 if value , ok := ms .Annotations [clusterv1 .MachineSetMoveMachinesToMachineSetAnnotation ]; ok {
419- changes = append (changes , clusterv1 .MachineSetMoveMachinesToMachineSetAnnotation , value )
411+ changes = append (changes , fmt . Sprintf ( "%s: %s" , clusterv1 .MachineSetMoveMachinesToMachineSetAnnotation , value ) )
420412 } else {
421- changes = append (changes , clusterv1 . MachineSetMoveMachinesToMachineSetAnnotation , "(annotation removed)" )
413+ changes = append (changes , fmt . Sprintf ( "%s removed" , clusterv1 . MachineSetMoveMachinesToMachineSetAnnotation ) )
422414 }
423415 }
424416
425417 if originalMS .Annotations [clusterv1 .MachineSetReceiveMachinesFromMachineSetsAnnotation ] != ms .Annotations [clusterv1 .MachineSetReceiveMachinesFromMachineSetsAnnotation ] {
426418 if value , ok := ms .Annotations [clusterv1 .MachineSetReceiveMachinesFromMachineSetsAnnotation ]; ok {
427- changes = append (changes , clusterv1 .MachineSetReceiveMachinesFromMachineSetsAnnotation , value )
419+ changes = append (changes , fmt . Sprintf ( "%s: %s" , clusterv1 .MachineSetReceiveMachinesFromMachineSetsAnnotation , value ) )
428420 } else {
429- changes = append (changes , clusterv1 . MachineSetReceiveMachinesFromMachineSetsAnnotation , "(annotation removed)" )
421+ changes = append (changes , fmt . Sprintf ( "%s removed" , clusterv1 . MachineSetReceiveMachinesFromMachineSetsAnnotation ) )
430422 }
431423 }
432424
433425 if originalMS .Annotations [clusterv1 .AcknowledgedMoveAnnotation ] != ms .Annotations [clusterv1 .AcknowledgedMoveAnnotation ] {
434426 if value , ok := ms .Annotations [clusterv1 .AcknowledgedMoveAnnotation ]; ok {
435- changes = append (changes , clusterv1 .AcknowledgedMoveAnnotation , value )
427+ changes = append (changes , fmt . Sprintf ( "%s: %s" , clusterv1 .AcknowledgedMoveAnnotation , value ) )
436428 } else {
437- changes = append (changes , clusterv1 . AcknowledgedMoveAnnotation , "(annotation removed)" )
429+ changes = append (changes , fmt . Sprintf ( "%s removed" , clusterv1 . AcknowledgedMoveAnnotation ) )
438430 }
439431 }
440432
441433 if originalMS .Annotations [clusterv1 .DisableMachineCreateAnnotation ] != ms .Annotations [clusterv1 .DisableMachineCreateAnnotation ] {
442434 if value , ok := ms .Annotations [clusterv1 .DisableMachineCreateAnnotation ]; ok {
443- changes = append (changes , clusterv1 .DisableMachineCreateAnnotation , value )
435+ changes = append (changes , fmt . Sprintf ( "%s: %s" , clusterv1 .DisableMachineCreateAnnotation , value ) )
444436 } else {
445- changes = append (changes , clusterv1 . DisableMachineCreateAnnotation , "(annotation removed)" )
437+ changes = append (changes , fmt . Sprintf ( "%s removed" , clusterv1 . DisableMachineCreateAnnotation ) )
446438 }
447439 }
448440 return changes
0 commit comments