Skip to content

Commit

Permalink
Merge pull request #8110 from ykakarap/node-label-in-place-propagatio…
Browse files Browse the repository at this point in the history
…n_md-ms

⚠️ in-place propagation from MD to MS
  • Loading branch information
k8s-ci-robot authored Feb 22, 2023
2 parents 6b40f8f + 8e06d16 commit 744f3ff
Show file tree
Hide file tree
Showing 8 changed files with 1,256 additions and 491 deletions.
14 changes: 12 additions & 2 deletions api/v1beta1/machinedeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,18 @@ const (
// proportions in case the deployment has surge replicas.
MaxReplicasAnnotation = "machinedeployment.clusters.x-k8s.io/max-replicas"

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/external"
"sigs.k8s.io/cluster-api/internal/util/ssa"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/conditions"
Expand All @@ -50,6 +51,10 @@ var (
machineDeploymentKind = clusterv1.GroupVersion.WithKind("MachineDeployment")
)

// machineDeploymentManagerName is the manager name used for Server-Side-Apply (SSA) operations
// in the MachineDeployment controller.
const machineDeploymentManagerName = "capi-machinedeployment"

// +kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch;create;patch
// +kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch
// +kubebuilder:rbac:groups=core,resources=nodes,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -248,6 +253,19 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster,
}
}

// Loop over all MachineSets and cleanup managed fields.
// We do this so that MachineSets that were created/patched before (< v1.4.0) the controller adopted
// Server-Side-Apply (SSA) can also work with SSA. Otherwise, fields would be co-owned by our "old" "manager" and
// "capi-machinedeployment" and then we would not be able to e.g. drop labels and annotations.
// Note: We are cleaning up managed fields for all MachineSets, so we're able to remove this code in a few
// Cluster API releases. If we do this only for selected MachineSets, we would have to keep this code forever.
for idx := range msList {
machineSet := msList[idx]
if err := ssa.CleanUpManagedFieldsForSSAAdoption(ctx, machineSet, machineDeploymentManagerName, r.Client); err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to clean up managedFields of MachineSet %s", klog.KObj(machineSet))
}
}

if d.Spec.Paused {
return ctrl.Result{}, r.sync(ctx, d, msList)
}
Expand Down Expand Up @@ -302,7 +320,7 @@ func (r *Reconciler) getMachineSetsForDeployment(ctx context.Context, d *cluster
continue
}

// Attempt to adopt machine if it meets previous conditions and it has no controller references.
// Attempt to adopt MachineSet if it meets previous conditions and it has no controller references.
if metav1.GetControllerOf(ms) == nil {
if err := r.adoptOrphan(ctx, d, ms); err != nil {
log.Error(err, "Failed to adopt MachineSet into MachineDeployment")
Expand Down
Loading

0 comments on commit 744f3ff

Please sign in to comment.