Skip to content

Commit 9d1e1f1

Browse files
Add e2e tests and fix panic
Signed-off-by: Furkat Gofurov <[email protected]> Co-authored-by: Andrea Mazzotti <[email protected]>
1 parent c2b53ae commit 9d1e1f1

File tree

5 files changed

+378
-74
lines changed

5 files changed

+378
-74
lines changed

controlplane/internal/controllers/rke2controlplane_controller.go

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ func (r *RKE2ControlPlaneReconciler) SetupWithManager(ctx context.Context, mgr c
258258

259259
r.controller = c
260260
r.recorder = mgr.GetEventRecorderFor("rke2-control-plane-controller")
261+
r.ssaCache = ssa.NewCache("rke2-control-plane")
261262

262263
// Set up a clusterCache to provide to controllers
263264
// requiring a connection to a remote cluster
@@ -503,10 +504,6 @@ func (r *RKE2ControlPlaneReconciler) reconcileNormal(
503504
return result, err
504505
}
505506

506-
if err := r.syncMachines(ctx, rke2.ControlPlane{}); err != nil {
507-
return ctrl.Result{}, errors.Wrap(err, "failed to sync Machines")
508-
}
509-
510507
controlPlaneMachines, err := r.managementClusterUncached.GetMachinesForCluster(
511508
ctx,
512509
util.ObjectKey(cluster),
@@ -531,6 +528,10 @@ func (r *RKE2ControlPlaneReconciler) reconcileNormal(
531528
return ctrl.Result{}, err
532529
}
533530

531+
if err := r.syncMachines(ctx, controlPlane); err != nil {
532+
return ctrl.Result{}, errors.Wrap(err, "failed to sync Machines")
533+
}
534+
534535
// Aggregate the operational state of all the machines; while aggregating we are adding the
535536
// source ref (reason@machine/name) so the problem can be easily tracked down to its source machine.
536537
conditions.SetAggregate(controlPlane.RCP, controlplanev1.MachinesReadyCondition,
@@ -629,7 +630,7 @@ func (r *RKE2ControlPlaneReconciler) GetWorkloadCluster(ctx context.Context, con
629630
// reconcileEtcdMembers ensures the number of etcd members is in sync with the number of machines/nodes.
630631
// This is usually required after a machine deletion.
631632
//
632-
// NOTE: this func uses KCP conditions, it is required to call reconcileControlPlaneConditions before this.
633+
// NOTE: this func uses RKE2ControlPlane conditions, it is required to call reconcileControlPlaneConditions before this.
633634
func (r *RKE2ControlPlaneReconciler) reconcileEtcdMembers(ctx context.Context, controlPlane *rke2.ControlPlane) error {
634635
log := ctrl.LoggerFrom(ctx)
635636

@@ -1106,20 +1107,41 @@ func (r *RKE2ControlPlaneReconciler) getWorkloadCluster(ctx context.Context, clu
11061107
return workloadCluster, nil
11071108
}
11081109

1109-
// syncMachines updates Machines, InfrastructureMachines and Rke2Configs to propagate in-place mutable fields from KCP.
1110-
// Note: It also cleans up managed fields of all Machines so that Machines that were
1111-
// created/patched before (< v1.4.0)“ the controller adopted Server-Side-Apply (SSA) can also work with SSA.
1110+
// syncMachines updates Machines, InfrastructureMachines and Rke2Configs to propagate in-place mutable fields from RKE2ControlPlane.
11121111
// Note: For InfrastructureMachines and Rke2Configs it also drops ownership of "metadata.labels" and
11131112
// "metadata.annotations" from "manager" so that "rke2controlplane" can own these fields and can work with SSA.
11141113
// Otherwise, fields would be co-owned by our "old" "manager" and "rke2controlplane" and then we would not be
11151114
// able to e.g. drop labels and annotations.
1116-
func (r *RKE2ControlPlaneReconciler) syncMachines(ctx context.Context, controlPlane rke2.ControlPlane) error {
1115+
func (r *RKE2ControlPlaneReconciler) syncMachines(ctx context.Context, controlPlane *rke2.ControlPlane) error {
11171116
patchHelpers := map[string]*patch.Helper{}
11181117

11191118
for machineName := range controlPlane.Machines {
11201119
m := controlPlane.Machines[machineName]
1121-
// If the machine is already being deleted, we don't need to update it.
1120+
// If the Machine is already being deleted, we only need to sync
1121+
// the subset of fields that impact tearing down the Machine.
11221122
if !m.DeletionTimestamp.IsZero() {
1123+
patchHelper, err := patch.NewHelper(m, r.Client)
1124+
if err != nil {
1125+
return err
1126+
}
1127+
1128+
// Set all other in-place mutable fields that impact the ability to tear down existing machines.
1129+
m.Spec.NodeDrainTimeout = controlPlane.RCP.Spec.MachineTemplate.NodeDrainTimeout
1130+
m.Spec.NodeDeletionTimeout = controlPlane.RCP.Spec.MachineTemplate.NodeDeletionTimeout
1131+
m.Spec.NodeVolumeDetachTimeout = controlPlane.RCP.Spec.MachineTemplate.NodeVolumeDetachTimeout
1132+
1133+
if err := patchHelper.Patch(ctx, m); err != nil {
1134+
return err
1135+
}
1136+
1137+
controlPlane.Machines[machineName] = m
1138+
patchHelper, err = patch.NewHelper(m, r.Client)
1139+
if err != nil { //nolint:wsl
1140+
return err
1141+
}
1142+
1143+
patchHelpers[machineName] = patchHelper
1144+
11231145
continue
11241146
}
11251147

@@ -1155,32 +1177,40 @@ func (r *RKE2ControlPlaneReconciler) syncMachines(ctx context.Context, controlPl
11551177
{"f:metadata", "f:annotations"},
11561178
{"f:metadata", "f:labels"},
11571179
}
1158-
infraMachine := controlPlane.InfraResources[machineName]
1159-
// Cleanup managed fields of all InfrastructureMachines to drop ownership of labels and annotations
1160-
// from "manager". We do this so that InfrastructureMachines that are created using the Create method
1161-
// can also work with SSA. Otherwise, labels and annotations would be co-owned by our "old" "manager"
1162-
// and "rke2-kubeadmcontrolplane" and then we would not be able to e.g. drop labels and annotations.
1163-
if err := ssa.DropManagedFields(ctx, r.Client, infraMachine, rke2ManagerName, labelsAndAnnotationsManagedFieldPaths); err != nil {
1164-
return errors.Wrapf(err, "failed to clean up managedFields of InfrastructureMachine %s", klog.KObj(infraMachine))
1165-
}
1166-
// Update in-place mutating fields on InfrastructureMachine.
1167-
if err := r.UpdateExternalObject(ctx, infraMachine, controlPlane.RCP, controlPlane.Cluster); err != nil {
1168-
return errors.Wrapf(err, "failed to update InfrastructureMachine %s", klog.KObj(infraMachine))
1180+
infraMachine, infraMachineFound := controlPlane.InfraResources[machineName]
1181+
// Only update the InfraMachine if it is already found, otherwise just skip it.
1182+
// This could happen e.g. if the cache is not up-to-date yet.
1183+
if infraMachineFound {
1184+
// Cleanup managed fields of all InfrastructureMachines to drop ownership of labels and annotations
1185+
// from "manager". We do this so that InfrastructureMachines that are created using the Create method
1186+
// can also work with SSA. Otherwise, labels and annotations would be co-owned by our "old" "manager"
1187+
// and "rke2-controlplane" and then we would not be able to e.g. drop labels and annotations.
1188+
if err := ssa.DropManagedFields(ctx, r.Client, infraMachine, rke2ManagerName, labelsAndAnnotationsManagedFieldPaths); err != nil {
1189+
return errors.Wrapf(err, "failed to clean up managedFields of InfrastructureMachine %s", klog.KObj(infraMachine))
1190+
}
1191+
// Update in-place mutating fields on InfrastructureMachine.
1192+
if err := r.UpdateExternalObject(ctx, infraMachine, controlPlane.RCP, controlPlane.Cluster); err != nil {
1193+
return errors.Wrapf(err, "failed to update InfrastructureMachine %s", klog.KObj(infraMachine))
1194+
}
11691195
}
11701196

1171-
rke2Config := controlPlane.Rke2Configs[machineName]
1172-
// Note: Set the GroupVersionKind because updateExternalObject depends on it.
1173-
rke2Config.SetGroupVersionKind(m.Spec.Bootstrap.ConfigRef.GroupVersionKind())
1174-
// Cleanup managed fields of all Rke2Configs to drop ownership of labels and annotations
1175-
// from "manager". We do this so that Rke2Configs that are created using the Create method
1176-
// can also work with SSA. Otherwise, labels and annotations would be co-owned by our "old" "manager"
1177-
// and "rke2controlplane" and then we would not be able to e.g. drop labels and annotations.
1178-
if err := ssa.DropManagedFields(ctx, r.Client, rke2Config, rke2ManagerName, labelsAndAnnotationsManagedFieldPaths); err != nil {
1179-
return errors.Wrapf(err, "failed to clean up managedFields of KubeadmConfig %s", klog.KObj(rke2Config))
1180-
}
1181-
// Update in-place mutating fields on BootstrapConfig.
1182-
if err := r.UpdateExternalObject(ctx, rke2Config, controlPlane.RCP, controlPlane.Cluster); err != nil {
1183-
return errors.Wrapf(err, "failed to update Rke2Config %s", klog.KObj(rke2Config))
1197+
rke2Config, rke2ConfigFound := controlPlane.Rke2Configs[machineName]
1198+
// Only update the RKE2Config if it is already found, otherwise just skip it.
1199+
// This could happen e.g. if the cache is not up-to-date yet.
1200+
if rke2ConfigFound {
1201+
// Note: Set the GroupVersionKind because updateExternalObject depends on it.
1202+
rke2Config.SetGroupVersionKind(m.Spec.Bootstrap.ConfigRef.GroupVersionKind())
1203+
// Cleanup managed fields of all RKE2Configs to drop ownership of labels and annotations
1204+
// from "manager". We do this so that RKE2Configs that are created using the Create method
1205+
// can also work with SSA. Otherwise, labels and annotations would be co-owned by our "old" "manager"
1206+
// and "rke2-controlplane" and then we would not be able to e.g. drop labels and annotations.
1207+
if err := ssa.DropManagedFields(ctx, r.Client, rke2Config, rke2ManagerName, labelsAndAnnotationsManagedFieldPaths); err != nil {
1208+
return errors.Wrapf(err, "failed to clean up managedFields of RKE2Config %s", klog.KObj(rke2Config))
1209+
}
1210+
// Update in-place mutating fields on BootstrapConfig.
1211+
if err := r.UpdateExternalObject(ctx, rke2Config, controlPlane.RCP, controlPlane.Cluster); err != nil {
1212+
return errors.Wrapf(err, "failed to update RKE2Config %s", klog.KObj(rke2Config))
1213+
}
11841214
}
11851215
}
11861216
// Update the patch helpers.

pkg/rke2/control_plane.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ func NewControlPlane(
6565
rcp *controlplanev1.RKE2ControlPlane,
6666
ownedMachines collections.Machines,
6767
) (*ControlPlane, error) {
68-
infraObjects, err := getInfraResources(ctx, client, ownedMachines)
68+
infraObjects, err := GetInfraResources(ctx, client, ownedMachines)
6969
if err != nil {
7070
return nil, err
7171
}
7272

73-
rke2Configs, err := getRKE2Configs(ctx, client, ownedMachines)
73+
rke2Configs, err := GetRKE2Configs(ctx, client, ownedMachines)
7474
if err != nil {
7575
return nil, err
7676
}
@@ -315,9 +315,9 @@ func (c *ControlPlane) UpToDateMachines() collections.Machines {
315315
return machines.Difference(c.MachinesNeedingRollout())
316316
}
317317

318-
// getInfraResources fetches the external infrastructure resource for each machine in the collection
318+
// GetInfraResources fetches the external infrastructure resource for each machine in the collection
319319
// and returns a map of machine.Name -> infraResource.
320-
func getInfraResources(ctx context.Context, cl client.Client, machines collections.Machines) (map[string]*unstructured.Unstructured, error) {
320+
func GetInfraResources(ctx context.Context, cl client.Client, machines collections.Machines) (map[string]*unstructured.Unstructured, error) {
321321
result := map[string]*unstructured.Unstructured{}
322322

323323
for _, m := range machines {
@@ -336,8 +336,8 @@ func getInfraResources(ctx context.Context, cl client.Client, machines collectio
336336
return result, nil
337337
}
338338

339-
// getRKE2Configs fetches the RKE2 config for each machine in the collection and returns a map of machine.Name -> RKE2Config.
340-
func getRKE2Configs(ctx context.Context, cl client.Client, machines collections.Machines) (map[string]*bootstrapv1.RKE2Config, error) {
339+
// GetRKE2Configs fetches the RKE2 config for each machine in the collection and returns a map of machine.Name -> RKE2Config.
340+
func GetRKE2Configs(ctx context.Context, cl client.Client, machines collections.Machines) (map[string]*bootstrapv1.RKE2Config, error) {
341341
result := map[string]*bootstrapv1.RKE2Config{}
342342

343343
for name, m := range machines {

0 commit comments

Comments
 (0)