@@ -48,7 +48,6 @@ import (
4848 "sigs.k8s.io/cluster-api/controlplane/kubeadm/internal"
4949 runtimeclient "sigs.k8s.io/cluster-api/exp/runtime/client"
5050 "sigs.k8s.io/cluster-api/feature"
51- "sigs.k8s.io/cluster-api/internal/contract"
5251 "sigs.k8s.io/cluster-api/internal/util/ssa"
5352 "sigs.k8s.io/cluster-api/util"
5453 "sigs.k8s.io/cluster-api/util/cache"
@@ -66,6 +65,7 @@ import (
6665
6766const (
6867 kcpManagerName = "capi-kubeadmcontrolplane"
68+ kcpMetadataManagerName = "capi-kubeadmcontrolplane-metadata"
6969 kubeadmControlPlaneKind = "KubeadmControlPlane"
7070)
7171
@@ -80,6 +80,7 @@ const (
8080// KubeadmControlPlaneReconciler reconciles a KubeadmControlPlane object.
8181type KubeadmControlPlaneReconciler struct {
8282 Client client.Client
83+ APIReader client.Reader
8384 SecretCachingClient client.Client
8485 RuntimeClient runtimeclient.Client
8586 controller controller.Controller
@@ -106,6 +107,9 @@ type KubeadmControlPlaneReconciler struct {
106107 overridePreflightChecksFunc func (ctx context.Context , controlPlane * internal.ControlPlane , excludeFor ... * clusterv1.Machine ) ctrl.Result
107108 overrideCanUpdateMachineFunc func (ctx context.Context , machine * clusterv1.Machine , machineUpToDateResult internal.UpToDateResult ) (bool , error )
108109 overrideCanExtensionsUpdateMachine func (ctx context.Context , machine * clusterv1.Machine , machineUpToDateResult internal.UpToDateResult , extensionHandlers []string ) (bool , []string , error )
110+ // Note: This field is only used for unit tests that use fake client because the fake client does not properly set resourceVersion
111+ // on BootstrapConfig/InfraMachine after ssa.Patch and then ssa.RemoveManagedFieldsForLabelsAndAnnotations would fail.
112+ disableRemoveManagedFieldsForLabelsAndAnnotations bool
109113}
110114
111115func (r * KubeadmControlPlaneReconciler ) SetupWithManager (ctx context.Context , mgr ctrl.Manager , options controller.Options ) error {
@@ -857,23 +861,15 @@ func (r *KubeadmControlPlaneReconciler) syncMachines(ctx context.Context, contro
857861 }
858862 patchHelpers [machineName ] = patchHelper
859863
860- labelsAndAnnotationsManagedFieldPaths := []contract.Path {
861- {"f:metadata" , "f:annotations" },
862- {"f:metadata" , "f:labels" },
863- }
864864 infraMachine , infraMachineFound := controlPlane .InfraResources [machineName ]
865865 // Only update the InfraMachine if it is already found, otherwise just skip it.
866866 // This could happen e.g. if the cache is not up-to-date yet.
867867 if infraMachineFound {
868- // Cleanup managed fields of all InfrastructureMachines to drop ownership of labels and annotations
869- // from "manager". We do this so that InfrastructureMachines that are created using the Create method
870- // can also work with SSA. Otherwise, labels and annotations would be co-owned by our "old" "manager"
871- // and "capi-kubeadmcontrolplane" and then we would not be able to e.g. drop labels and annotations.
872- if err := ssa .DropManagedFields (ctx , r .Client , infraMachine , kcpManagerName , labelsAndAnnotationsManagedFieldPaths ); err != nil {
868+ if err := ssa .MigrateManagedFields (ctx , r .Client , infraMachine , kcpManagerName , kcpMetadataManagerName ); err != nil {
873869 return errors .Wrapf (err , "failed to clean up managedFields of InfrastructureMachine %s" , klog .KObj (infraMachine ))
874870 }
875871 // Update in-place mutating fields on InfrastructureMachine.
876- if err := r .updateExternalObject (ctx , infraMachine , infraMachine .GroupVersionKind (), controlPlane .KCP , controlPlane .Cluster ); err != nil {
872+ if err := r .updateLabelsAndAnnotations (ctx , infraMachine , infraMachine .GroupVersionKind (), controlPlane .KCP , controlPlane .Cluster ); err != nil {
877873 return errors .Wrapf (err , "failed to update InfrastructureMachine %s" , klog .KObj (infraMachine ))
878874 }
879875 }
@@ -882,15 +878,11 @@ func (r *KubeadmControlPlaneReconciler) syncMachines(ctx context.Context, contro
882878 // Only update the KubeadmConfig if it is already found, otherwise just skip it.
883879 // This could happen e.g. if the cache is not up-to-date yet.
884880 if kubeadmConfigFound {
885- // Cleanup managed fields of all KubeadmConfigs to drop ownership of labels and annotations
886- // from "manager". We do this so that KubeadmConfigs that are created using the Create method
887- // can also work with SSA. Otherwise, labels and annotations would be co-owned by our "old" "manager"
888- // and "capi-kubeadmcontrolplane" and then we would not be able to e.g. drop labels and annotations.
889- if err := ssa .DropManagedFields (ctx , r .Client , kubeadmConfig , kcpManagerName , labelsAndAnnotationsManagedFieldPaths ); err != nil {
881+ if err := ssa .MigrateManagedFields (ctx , r .Client , kubeadmConfig , kcpManagerName , kcpMetadataManagerName ); err != nil {
890882 return errors .Wrapf (err , "failed to clean up managedFields of KubeadmConfig %s" , klog .KObj (kubeadmConfig ))
891883 }
892884 // Update in-place mutating fields on BootstrapConfig.
893- if err := r .updateExternalObject (ctx , kubeadmConfig , bootstrapv1 .GroupVersion .WithKind ("KubeadmConfig" ), controlPlane .KCP , controlPlane .Cluster ); err != nil {
885+ if err := r .updateLabelsAndAnnotations (ctx , kubeadmConfig , bootstrapv1 .GroupVersion .WithKind ("KubeadmConfig" ), controlPlane .KCP , controlPlane .Cluster ); err != nil {
894886 return errors .Wrapf (err , "failed to update KubeadmConfig %s" , klog .KObj (kubeadmConfig ))
895887 }
896888 }
0 commit comments