Skip to content

Commit b185a53

Browse files
authored
Merge pull request #10736 from fabriziopandini/do-not-update-observedGeneration-if-err
🌱 Do not update observed generation if there are reconcile errors
2 parents 7e2b0b2 + 110cad0 commit b185a53

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

controlplane/kubeadm/internal/controllers/controller.go

+19-16
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@ func (r *KubeadmControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.
214214
}
215215

216216
// Always attempt to Patch the KubeadmControlPlane object and status after each reconciliation.
217-
if err := patchKubeadmControlPlane(ctx, patchHelper, kcp); err != nil {
217+
patchOpts := []patch.Option{}
218+
if reterr == nil {
219+
patchOpts = append(patchOpts, patch.WithStatusObservedGeneration{})
220+
}
221+
if err := patchKubeadmControlPlane(ctx, patchHelper, kcp, patchOpts...); err != nil {
218222
log.Error(err, "Failed to patch KubeadmControlPlane")
219223
reterr = kerrors.NewAggregate([]error{reterr, err})
220224
}
@@ -307,7 +311,7 @@ func (r *KubeadmControlPlaneReconciler) initControlPlaneScope(ctx context.Contex
307311
return controlPlane, false, nil
308312
}
309313

310-
func patchKubeadmControlPlane(ctx context.Context, patchHelper *patch.Helper, kcp *controlplanev1.KubeadmControlPlane) error {
314+
func patchKubeadmControlPlane(ctx context.Context, patchHelper *patch.Helper, kcp *controlplanev1.KubeadmControlPlane, options ...patch.Option) error {
311315
// Always update the readyCondition by summarizing the state of other conditions.
312316
conditions.SetSummary(kcp,
313317
conditions.WithConditions(
@@ -321,20 +325,19 @@ func patchKubeadmControlPlane(ctx context.Context, patchHelper *patch.Helper, kc
321325
)
322326

323327
// Patch the object, ignoring conflicts on the conditions owned by this controller.
324-
return patchHelper.Patch(
325-
ctx,
326-
kcp,
327-
patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
328-
controlplanev1.MachinesCreatedCondition,
329-
clusterv1.ReadyCondition,
330-
controlplanev1.MachinesSpecUpToDateCondition,
331-
controlplanev1.ResizedCondition,
332-
controlplanev1.MachinesReadyCondition,
333-
controlplanev1.AvailableCondition,
334-
controlplanev1.CertificatesAvailableCondition,
335-
}},
336-
patch.WithStatusObservedGeneration{},
337-
)
328+
// Also, if requested, we are adding additional options like e.g. Patch ObservedGeneration when issuing the
329+
// patch at the end of the reconcile loop.
330+
options = append(options, patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
331+
controlplanev1.MachinesCreatedCondition,
332+
clusterv1.ReadyCondition,
333+
controlplanev1.MachinesSpecUpToDateCondition,
334+
controlplanev1.ResizedCondition,
335+
controlplanev1.MachinesReadyCondition,
336+
controlplanev1.AvailableCondition,
337+
controlplanev1.CertificatesAvailableCondition,
338+
}})
339+
340+
return patchHelper.Patch(ctx, kcp, options...)
338341
}
339342

340343
// reconcile handles KubeadmControlPlane reconciliation.

exp/addons/internal/controllers/clusterresourceset_controller.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,13 @@ func (r *ClusterResourceSetReconciler) Reconcile(ctx context.Context, req ctrl.R
118118
}
119119

120120
defer func() {
121-
// Always attempt to Patch the ClusterResourceSet object and status after each reconciliation.
122-
if err := patchHelper.Patch(ctx, clusterResourceSet, patch.WithStatusObservedGeneration{}); err != nil {
121+
// Always attempt to patch the object and status after each reconciliation.
122+
// Patch ObservedGeneration only if the reconciliation completed successfully.
123+
patchOpts := []patch.Option{}
124+
if reterr == nil {
125+
patchOpts = append(patchOpts, patch.WithStatusObservedGeneration{})
126+
}
127+
if err := patchHelper.Patch(ctx, clusterResourceSet, patchOpts...); err != nil {
123128
reterr = kerrors.NewAggregate([]error{reterr, err})
124129
}
125130
}()

0 commit comments

Comments
 (0)