@@ -37,6 +37,7 @@ import (
37
37
38
38
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
39
39
"sigs.k8s.io/cluster-api/controllers/external"
40
+ "sigs.k8s.io/cluster-api/internal/util/ssa"
40
41
"sigs.k8s.io/cluster-api/util"
41
42
"sigs.k8s.io/cluster-api/util/annotations"
42
43
"sigs.k8s.io/cluster-api/util/conditions"
50
51
machineDeploymentKind = clusterv1 .GroupVersion .WithKind ("MachineDeployment" )
51
52
)
52
53
54
+ // machineDeploymentManagerName is the manager name used for Server-Side-Apply (SSA) operations
55
+ // in the MachineDeployment controller.
56
+ const machineDeploymentManagerName = "capi-machinedeployment"
57
+
53
58
// +kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch;create;patch
54
59
// +kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch
55
60
// +kubebuilder:rbac:groups=core,resources=nodes,verbs=get;list;watch;create;update;patch;delete
@@ -248,6 +253,19 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster,
248
253
}
249
254
}
250
255
256
+ // Loop over all MachineSets and cleanup managed fields.
257
+ // We do this so that MachineSets that were created/patched before (< v1.4.0) the controller adopted
258
+ // Server-Side-Apply (SSA) can also work with SSA. Otherwise, fields would be co-owned by our "old" "manager" and
259
+ // "capi-machinedeployment" and then we would not be able to e.g. drop labels and annotations.
260
+ // Note: We are cleaning up managed fields for all MachineSets, so we're able to remove this code in a few
261
+ // Cluster API releases. If we do this only for selected MachineSets, we would have to keep this code forever.
262
+ for idx := range msList {
263
+ machineSet := msList [idx ]
264
+ if err := ssa .CleanUpManagedFieldsForSSAAdoption (ctx , machineSet , machineDeploymentManagerName , r .Client ); err != nil {
265
+ return ctrl.Result {}, errors .Wrapf (err , "failed to clean up managedFields of MachineSet %s" , klog .KObj (machineSet ))
266
+ }
267
+ }
268
+
251
269
if d .Spec .Paused {
252
270
return ctrl.Result {}, r .sync (ctx , d , msList )
253
271
}
@@ -302,7 +320,7 @@ func (r *Reconciler) getMachineSetsForDeployment(ctx context.Context, d *cluster
302
320
continue
303
321
}
304
322
305
- // Attempt to adopt machine if it meets previous conditions and it has no controller references.
323
+ // Attempt to adopt MachineSet if it meets previous conditions and it has no controller references.
306
324
if metav1 .GetControllerOf (ms ) == nil {
307
325
if err := r .adoptOrphan (ctx , d , ms ); err != nil {
308
326
log .Error (err , "Failed to adopt MachineSet into MachineDeployment" )
0 commit comments