@@ -444,14 +444,18 @@ func (r *KubeadmControlPlaneReconciler) reconcile(ctx context.Context, controlPl
444444 return result , err
445445 }
446446
447- if err := r .syncMachines (ctx , controlPlane ); err != nil {
447+ stopReconcile , err := r .syncMachines (ctx , controlPlane )
448+ if err != nil {
448449 // Note: If any of the calls got a NotFound error, it means that at least one Machine got deleted.
449450 // Let's return here so that the next Reconcile will get the updated list of Machines.
450451 if apierrors .IsNotFound (err ) {
451452 return ctrl.Result {}, nil // Note: Requeue is not needed, changes to Machines trigger another reconcile.
452453 }
453454 return ctrl.Result {}, errors .Wrap (err , "failed to sync Machines" )
454455 }
456+ if stopReconcile {
457+ return ctrl.Result {RequeueAfter : 1 * time .Second }, nil // Explicitly requeue as we are not watching all objects.
458+ }
455459
456460 // Aggregate the operational state of all the machines; while aggregating we are adding the
457461 // source ref (reason@machine/name) so the problem can be easily tracked down to its source machine.
@@ -845,16 +849,17 @@ func (r *KubeadmControlPlaneReconciler) ClusterToKubeadmControlPlane(_ context.C
845849}
846850
847851// syncMachines updates Machines, InfrastructureMachines and KubeadmConfigs to propagate in-place mutable fields from KCP.
848- func (r * KubeadmControlPlaneReconciler ) syncMachines (ctx context.Context , controlPlane * internal.ControlPlane ) error {
852+ func (r * KubeadmControlPlaneReconciler ) syncMachines (ctx context.Context , controlPlane * internal.ControlPlane ) ( bool , error ) {
849853 patchHelpers := map [string ]* patch.Helper {}
854+ var anyManagedFieldIssueMitigated bool
850855 for machineName := range controlPlane .Machines {
851856 m := controlPlane .Machines [machineName ]
852857 // If the Machine is already being deleted, we only need to sync
853858 // the subset of fields that impact tearing down the Machine.
854859 if ! m .DeletionTimestamp .IsZero () {
855860 patchHelper , err := patch .NewHelper (m , r .Client )
856861 if err != nil {
857- return err
862+ return true , err
858863 }
859864
860865 // Set all other in-place mutable fields that impact the ability to tear down existing machines.
@@ -865,84 +870,105 @@ func (r *KubeadmControlPlaneReconciler) syncMachines(ctx context.Context, contro
865870
866871 // Note: We intentionally don't set "minReadySeconds" on Machines because we consider it enough to have machine availability driven by readiness of control plane components.
867872 if err := patchHelper .Patch (ctx , m ); err != nil {
868- return err
873+ return true , err
869874 }
870875
871876 controlPlane .Machines [machineName ] = m
872877 patchHelper , err = patch .NewHelper (m , r .Client )
873878 if err != nil {
874- return err
879+ return true , err
875880 }
876881 patchHelpers [machineName ] = patchHelper
877882 continue
878883 }
879884
880- // Update Machine to propagate in-place mutable fields from KCP.
881- updatedMachine , err := r .updateMachine (ctx , m , controlPlane .KCP , controlPlane .Cluster )
882- if err != nil {
883- return errors .Wrapf (err , "failed to update Machine: %s" , klog .KObj (m ))
884- }
885- // Note: Ensure ControlPlane has the latest version of the Machine. This is required because
886- // e.g. the in-place update code that is called later has to use the latest version of the Machine.
887- controlPlane .Machines [machineName ] = updatedMachine
888- if _ , ok := controlPlane .MachinesNotUpToDate [machineName ]; ok {
889- controlPlane .MachinesNotUpToDate [machineName ] = updatedMachine
890- }
891- // Since the machine is updated, re-create the patch helper so that any subsequent
892- // Patch calls use the correct base machine object to calculate the diffs.
893- // Example: reconcileControlPlaneAndMachinesConditions patches the machine objects in a subsequent call
894- // and, it should use the updated machine to calculate the diff.
895- // Note: If the patchHelpers are not re-computed based on the new updated machines, subsequent
896- // Patch calls will fail because the patch will be calculated based on an outdated machine and will error
897- // because of outdated resourceVersion.
898- // TODO: This should be cleaned-up to have a more streamline way of constructing and using patchHelpers.
899- patchHelper , err := patch .NewHelper (updatedMachine , r .Client )
885+ managedFieldIssueMitigated , err := ssa .MitigateManagedFieldsIssue (ctx , r .Client , m , kcpManagerName )
900886 if err != nil {
901- return err
887+ return true , err
888+ }
889+ anyManagedFieldIssueMitigated = anyManagedFieldIssueMitigated || managedFieldIssueMitigated
890+ if ! anyManagedFieldIssueMitigated {
891+ // Update Machine to propagate in-place mutable fields from KCP.
892+ updatedMachine , err := r .updateMachine (ctx , m , controlPlane .KCP , controlPlane .Cluster )
893+ if err != nil {
894+ return true , errors .Wrapf (err , "failed to update Machine: %s" , klog .KObj (m ))
895+ }
896+ // Note: Ensure ControlPlane has the latest version of the Machine. This is required because
897+ // e.g. the in-place update code that is called later has to use the latest version of the Machine.
898+ controlPlane .Machines [machineName ] = updatedMachine
899+ if _ , ok := controlPlane .MachinesNotUpToDate [machineName ]; ok {
900+ controlPlane .MachinesNotUpToDate [machineName ] = updatedMachine
901+ }
902+ // Since the machine is updated, re-create the patch helper so that any subsequent
903+ // Patch calls use the correct base machine object to calculate the diffs.
904+ // Example: reconcileControlPlaneAndMachinesConditions patches the machine objects in a subsequent call
905+ // and, it should use the updated machine to calculate the diff.
906+ // Note: If the patchHelpers are not re-computed based on the new updated machines, subsequent
907+ // Patch calls will fail because the patch will be calculated based on an outdated machine and will error
908+ // because of outdated resourceVersion.
909+ // TODO: This should be cleaned-up to have a more streamline way of constructing and using patchHelpers.
910+ patchHelper , err := patch .NewHelper (updatedMachine , r .Client )
911+ if err != nil {
912+ return true , err
913+ }
914+ patchHelpers [machineName ] = patchHelper
902915 }
903- patchHelpers [machineName ] = patchHelper
904916
905917 infraMachine , infraMachineFound := controlPlane .InfraResources [machineName ]
906918 // Only update the InfraMachine if it is already found, otherwise just skip it.
907919 // This could happen e.g. if the cache is not up-to-date yet.
908920 if infraMachineFound {
909- // Drop managedFields for manager:Update and capi-kubeadmcontrolplane:Apply for all objects created with CAPI <= v1.11.
910- // Starting with CAPI v1.12 we have a new managedField structure where capi-kubeadmcontrolplane-metadata will own
911- // labels and annotations and capi-kubeadmcontrolplane everything else.
912- // Note: We have to call ssa.MigrateManagedFields for every Machine created with CAPI <= v1.11 once.
913- // Given that this was introduced in CAPI v1.12 and our n-3 upgrade policy this can
914- // be removed with CAPI v1.15.
915- if err := ssa .MigrateManagedFields (ctx , r .Client , infraMachine , kcpManagerName , kcpMetadataManagerName ); err != nil {
916- return errors .Wrapf (err , "failed to clean up managedFields of InfrastructureMachine %s" , klog .KObj (infraMachine ))
921+ managedFieldIssueMitigated , err = ssa .MitigateManagedFieldsIssue (ctx , r .Client , infraMachine , kcpMetadataManagerName )
922+ if err != nil {
923+ return true , err
917924 }
918- // Update in-place mutating fields on InfrastructureMachine.
919- if err := r .updateLabelsAndAnnotations (ctx , infraMachine , infraMachine .GroupVersionKind (), controlPlane .KCP , controlPlane .Cluster ); err != nil {
920- return errors .Wrapf (err , "failed to update InfrastructureMachine %s" , klog .KObj (infraMachine ))
925+ anyManagedFieldIssueMitigated = anyManagedFieldIssueMitigated || managedFieldIssueMitigated
926+ if ! anyManagedFieldIssueMitigated {
927+ // Drop managedFields for manager:Update and capi-kubeadmcontrolplane:Apply for all objects created with CAPI <= v1.11.
928+ // Starting with CAPI v1.12 we have a new managedField structure where capi-kubeadmcontrolplane-metadata will own
929+ // labels and annotations and capi-kubeadmcontrolplane everything else.
930+ // Note: We have to call ssa.MigrateManagedFields for every Machine created with CAPI <= v1.11 once.
931+ // Given that this was introduced in CAPI v1.12 and our n-3 upgrade policy this can
932+ // be removed with CAPI v1.15.
933+ if err := ssa .MigrateManagedFields (ctx , r .Client , infraMachine , kcpManagerName , kcpMetadataManagerName ); err != nil {
934+ return true , errors .Wrapf (err , "failed to clean up managedFields of InfrastructureMachine %s" , klog .KObj (infraMachine ))
935+ }
936+ // Update in-place mutating fields on InfrastructureMachine.
937+ if err := r .updateLabelsAndAnnotations (ctx , infraMachine , infraMachine .GroupVersionKind (), controlPlane .KCP , controlPlane .Cluster ); err != nil {
938+ return true , errors .Wrapf (err , "failed to update InfrastructureMachine %s" , klog .KObj (infraMachine ))
939+ }
921940 }
922941 }
923942
924943 kubeadmConfig , kubeadmConfigFound := controlPlane .KubeadmConfigs [machineName ]
925944 // Only update the KubeadmConfig if it is already found, otherwise just skip it.
926945 // This could happen e.g. if the cache is not up-to-date yet.
927946 if kubeadmConfigFound {
928- // Drop managedFields for manager:Update and capi-kubeadmcontrolplane:Apply for all objects created with CAPI <= v1.11.
929- // Starting with CAPI v1.12 we have a new managedField structure where capi-kubeadmcontrolplane-metadata will own
930- // labels and annotations and capi-kubeadmcontrolplane everything else.
931- // Note: We have to call ssa.MigrateManagedFields for every Machine created with CAPI <= v1.11 once.
932- // Given that this was introduced in CAPI v1.12 and our n-3 upgrade policy this can
933- // be removed with CAPI v1.15.
934- if err := ssa .MigrateManagedFields (ctx , r .Client , kubeadmConfig , kcpManagerName , kcpMetadataManagerName ); err != nil {
935- return errors .Wrapf (err , "failed to clean up managedFields of KubeadmConfig %s" , klog .KObj (kubeadmConfig ))
947+ managedFieldIssueMitigated , err = ssa .MitigateManagedFieldsIssue (ctx , r .Client , kubeadmConfig , kcpMetadataManagerName )
948+ if err != nil {
949+ return true , err
936950 }
937- // Update in-place mutating fields on BootstrapConfig.
938- if err := r .updateLabelsAndAnnotations (ctx , kubeadmConfig , bootstrapv1 .GroupVersion .WithKind ("KubeadmConfig" ), controlPlane .KCP , controlPlane .Cluster ); err != nil {
939- return errors .Wrapf (err , "failed to update KubeadmConfig %s" , klog .KObj (kubeadmConfig ))
951+ anyManagedFieldIssueMitigated = anyManagedFieldIssueMitigated || managedFieldIssueMitigated
952+ if ! anyManagedFieldIssueMitigated {
953+ // Drop managedFields for manager:Update and capi-kubeadmcontrolplane:Apply for all objects created with CAPI <= v1.11.
954+ // Starting with CAPI v1.12 we have a new managedField structure where capi-kubeadmcontrolplane-metadata will own
955+ // labels and annotations and capi-kubeadmcontrolplane everything else.
956+ // Note: We have to call ssa.MigrateManagedFields for every Machine created with CAPI <= v1.11 once.
957+ // Given that this was introduced in CAPI v1.12 and our n-3 upgrade policy this can
958+ // be removed with CAPI v1.15.
959+ if err := ssa .MigrateManagedFields (ctx , r .Client , kubeadmConfig , kcpManagerName , kcpMetadataManagerName ); err != nil {
960+ return true , errors .Wrapf (err , "failed to clean up managedFields of KubeadmConfig %s" , klog .KObj (kubeadmConfig ))
961+ }
962+ // Update in-place mutating fields on BootstrapConfig.
963+ if err := r .updateLabelsAndAnnotations (ctx , kubeadmConfig , bootstrapv1 .GroupVersion .WithKind ("KubeadmConfig" ), controlPlane .KCP , controlPlane .Cluster ); err != nil {
964+ return true , errors .Wrapf (err , "failed to update KubeadmConfig %s" , klog .KObj (kubeadmConfig ))
965+ }
940966 }
941967 }
942968 }
943969 // Update the patch helpers.
944970 controlPlane .SetPatchHelpers (patchHelpers )
945- return nil
971+ return anyManagedFieldIssueMitigated , nil
946972}
947973
948974// reconcileControlPlaneAndMachinesConditions is responsible of reconciling conditions reporting the status of static pods and
0 commit comments