Skip to content

Commit 1c845c3

Browse files
committed
Refactor BootstrapConfig/InfraMachine managedFields for in-place
1 parent f9af846 commit 1c845c3

File tree

17 files changed

+1431
-762
lines changed

17 files changed

+1431
-762
lines changed

controlplane/kubeadm/controllers/alias.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
// KubeadmControlPlaneReconciler reconciles a KubeadmControlPlane object.
3434
type KubeadmControlPlaneReconciler struct {
3535
Client client.Client
36+
APIReader client.Reader
3637
SecretCachingClient client.Client
3738
RuntimeClient runtimeclient.Client
3839
ClusterCache clustercache.ClusterCache
@@ -51,6 +52,7 @@ type KubeadmControlPlaneReconciler struct {
5152
func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
5253
return (&kubeadmcontrolplanecontrollers.KubeadmControlPlaneReconciler{
5354
Client: r.Client,
55+
APIReader: r.APIReader,
5456
SecretCachingClient: r.SecretCachingClient,
5557
RuntimeClient: r.RuntimeClient,
5658
ClusterCache: r.ClusterCache,

controlplane/kubeadm/internal/controllers/controller.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6766
const (
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.
8181
type 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

111115
func (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

Comments
 (0)