Skip to content

Commit 744f3ff

Browse files
authored
Merge pull request #8110 from ykakarap/node-label-in-place-propagation_md-ms
⚠️ in-place propagation from MD to MS
2 parents 6b40f8f + 8e06d16 commit 744f3ff

File tree

8 files changed

+1256
-491
lines changed

8 files changed

+1256
-491
lines changed

api/v1beta1/machinedeployment_types.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,18 @@ const (
5454
// proportions in case the deployment has surge replicas.
5555
MaxReplicasAnnotation = "machinedeployment.clusters.x-k8s.io/max-replicas"
5656

57-
// MachineDeploymentUniqueLabel is the label applied to Machines
58-
// in a MachineDeployment containing the hash of the template.
57+
// MachineDeploymentUniqueLabel is used to uniquely identify the Machines of a MachineSet.
58+
// The MachineDeployment controller will set this label on a MachineSet when it is created.
59+
// The label is also applied to the Machines of the MachineSet and used in the MachineSet selector.
60+
// Note: For the lifetime of the MachineSet the label's value has to stay the same, otherwise the
61+
// MachineSet selector would no longer match its Machines.
62+
// Note: In previous Cluster API versions (< v1.4.0), the label value was the hash of the full machine template.
63+
// With the introduction of in-place mutation the machine template of the MachineSet can change.
64+
// Because of that it is impossible that the label's value to always be the hash of the full machine template.
65+
// (Because the hash changes when the machine template changes).
66+
// As a result, we use the hash of the machine template while ignoring all in-place mutable fields, i.e. the
67+
// machine template with only fields that could trigger a rollout for the machine-template-hash, making it
68+
// independent of the changes to any in-place mutable fields.
5969
MachineDeploymentUniqueLabel = "machine-template-hash"
6070
)
6171

internal/controllers/machinedeployment/machinedeployment_controller.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737

3838
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3939
"sigs.k8s.io/cluster-api/controllers/external"
40+
"sigs.k8s.io/cluster-api/internal/util/ssa"
4041
"sigs.k8s.io/cluster-api/util"
4142
"sigs.k8s.io/cluster-api/util/annotations"
4243
"sigs.k8s.io/cluster-api/util/conditions"
@@ -50,6 +51,10 @@ var (
5051
machineDeploymentKind = clusterv1.GroupVersion.WithKind("MachineDeployment")
5152
)
5253

54+
// machineDeploymentManagerName is the manager name used for Server-Side-Apply (SSA) operations
55+
// in the MachineDeployment controller.
56+
const machineDeploymentManagerName = "capi-machinedeployment"
57+
5358
// +kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch;create;patch
5459
// +kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch
5560
// +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,
248253
}
249254
}
250255

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+
251269
if d.Spec.Paused {
252270
return ctrl.Result{}, r.sync(ctx, d, msList)
253271
}
@@ -302,7 +320,7 @@ func (r *Reconciler) getMachineSetsForDeployment(ctx context.Context, d *cluster
302320
continue
303321
}
304322

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.
306324
if metav1.GetControllerOf(ms) == nil {
307325
if err := r.adoptOrphan(ctx, d, ms); err != nil {
308326
log.Error(err, "Failed to adopt MachineSet into MachineDeployment")

0 commit comments

Comments
 (0)