Skip to content

Commit 4261236

Browse files
committed
Add ProgressingWithRetry reason to Reconciling condition
ProgressingWithRetry signals that the controller is going to retry the last failed reconciliation at a later time. Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
1 parent 933a8db commit 4261236

4 files changed

Lines changed: 49 additions & 20 deletions

File tree

api/v1beta2/condition_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ const (
4848
// ReconciliationFailedReason represents the fact that
4949
// the reconciliation failed.
5050
ReconciliationFailedReason string = "ReconciliationFailed"
51+
52+
// ProgressingWithRetryReason represents the fact that
53+
// the reconciliation encountered an error that will be retried.
54+
ProgressingWithRetryReason string = "ProgressingWithRetry"
5155
)

controllers/kustomization_controller.go

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,7 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
170170

171171
// Finalise the reconciliation and report the results.
172172
defer func() {
173-
// Set the value of the reconciliation request in status.
174-
if v, ok := meta.ReconcileAnnotationValue(obj.GetAnnotations()); ok {
175-
obj.Status.LastHandledReconcileAt = v
176-
}
177-
178-
// Remove the Reconciling condition and update the observed generation
179-
// if the reconciliation was successful.
180-
if conditions.IsTrue(obj, meta.ReadyCondition) {
181-
conditions.Delete(obj, meta.ReconcilingCondition)
182-
obj.Status.ObservedGeneration = obj.Generation
183-
}
184-
185-
// Patch metadata, status and conditions.
186-
retErr = r.patch(ctx, obj, patcher)
173+
retErr = r.finalizeStatus(ctx, obj, patcher)
187174

188175
// Record Prometheus metrics.
189176
r.Metrics.RecordReadiness(ctx, obj)
@@ -221,7 +208,7 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
221208

222209
// Set reconciling condition.
223210
if obj.Generation != obj.Status.ObservedGeneration {
224-
conditions.MarkReconciling(obj, "NewGeneration", "Reconciling new object generation (%d)", obj.Generation)
211+
conditions.MarkReconciling(obj, meta.ProgressingReason, "Reconciling new object generation (%d)", obj.Generation)
225212
}
226213

227214
// Resolve the source reference and requeue the reconciliation if the source is not found.
@@ -465,7 +452,7 @@ func (r *KustomizationReconciler) checkDependencies(ctx context.Context,
465452
var k kustomizev1.Kustomization
466453
err := r.Get(ctx, dName, &k)
467454
if err != nil {
468-
return fmt.Errorf("unable to get '%s' dependency: %w", dName, err)
455+
return fmt.Errorf("dependency '%s' not found: %w", dName, err)
469456
}
470457

471458
if len(k.Status.Conditions) == 0 || k.Generation != k.Status.ObservedGeneration {
@@ -480,7 +467,7 @@ func (r *KustomizationReconciler) checkDependencies(ctx context.Context,
480467
k.Spec.SourceRef.Namespace == obj.Spec.SourceRef.Namespace &&
481468
k.Spec.SourceRef.Kind == obj.Spec.SourceRef.Kind &&
482469
source.GetArtifact().Revision != k.Status.LastAppliedRevision {
483-
return fmt.Errorf("dependency '%s' is not updated yet", dName)
470+
return fmt.Errorf("dependency '%s' revision is not up to date", dName)
484471
}
485472
}
486473

@@ -979,6 +966,34 @@ func (r *KustomizationReconciler) event(obj *kustomizev1.Kustomization,
979966
r.EventRecorder.AnnotatedEventf(obj, metadata, eventtype, reason, msg)
980967
}
981968

969+
func (r *KustomizationReconciler) finalizeStatus(ctx context.Context,
970+
obj *kustomizev1.Kustomization,
971+
patcher *patch.SerialPatcher) error {
972+
// Set the value of the reconciliation request in status.
973+
if v, ok := meta.ReconcileAnnotationValue(obj.GetAnnotations()); ok {
974+
obj.Status.LastHandledReconcileAt = v
975+
}
976+
977+
// Remove the Reconciling condition and update the observed generation
978+
// if the reconciliation was successful.
979+
if conditions.IsTrue(obj, meta.ReadyCondition) {
980+
conditions.Delete(obj, meta.ReconcilingCondition)
981+
obj.Status.ObservedGeneration = obj.Generation
982+
}
983+
984+
// Set the Reconciling reason to ProgressingWithRetry if the
985+
// reconciliation has failed.
986+
if conditions.IsFalse(obj, meta.ReadyCondition) &&
987+
conditions.Has(obj, meta.ReconcilingCondition) {
988+
rc := conditions.Get(obj, meta.ReconcilingCondition)
989+
rc.Reason = kustomizev1.ProgressingWithRetryReason
990+
conditions.Set(obj, rc)
991+
}
992+
993+
// Patch metadata, status and conditions.
994+
return r.patch(ctx, obj, patcher)
995+
}
996+
982997
func (r *KustomizationReconciler) patch(ctx context.Context,
983998
obj *kustomizev1.Kustomization,
984999
patcher *patch.SerialPatcher) (retErr error) {

controllers/kustomization_wait_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ parameters:
154154

155155
g.Eventually(func() bool {
156156
_ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), resultK)
157-
return conditions.IsReconciling(resultK)
157+
return isReconcileRunning(resultK)
158158
}, timeout, time.Second).Should(BeTrue())
159159
logStatus(t, resultK)
160160

@@ -183,7 +183,7 @@ parameters:
183183
}
184184

185185
expectedMessage := "Running health checks"
186-
g.Expect(conditions.GetReason(resultK, meta.ReconcilingCondition)).To(BeIdenticalTo(meta.ProgressingReason))
186+
g.Expect(conditions.GetReason(resultK, meta.ReconcilingCondition)).To(BeIdenticalTo(kustomizev1.ProgressingWithRetryReason))
187187
g.Expect(conditions.GetMessage(resultK, meta.ReconcilingCondition)).To(ContainSubstring(expectedMessage))
188188

189189
g.Expect(resultK.Status.LastHandledReconcileAt).To(BeIdenticalTo(reconcileRequestAt))

controllers/suite_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ func randStringRunes(n int) string {
196196
return string(b)
197197
}
198198

199+
func isReconcileRunning(k *kustomizev1.Kustomization) bool {
200+
return conditions.IsReconciling(k) &&
201+
conditions.GetReason(k, meta.ReconcilingCondition) != kustomizev1.ProgressingWithRetryReason
202+
}
203+
199204
func isReconcileSuccess(k *kustomizev1.Kustomization) bool {
200205
return conditions.IsReady(k) &&
201206
conditions.GetObservedGeneration(k, meta.ReadyCondition) == k.Generation &&
@@ -204,14 +209,19 @@ func isReconcileSuccess(k *kustomizev1.Kustomization) bool {
204209
}
205210

206211
func isReconcileFailure(k *kustomizev1.Kustomization) bool {
212+
if conditions.IsStalled(k) {
213+
return true
214+
}
215+
207216
isHandled := true
208217
if v, ok := meta.ReconcileAnnotationValue(k.GetAnnotations()); ok {
209218
isHandled = k.Status.LastHandledReconcileAt == v
210219
}
211220

212221
return isHandled && conditions.IsReconciling(k) &&
213222
conditions.IsFalse(k, meta.ReadyCondition) &&
214-
conditions.GetObservedGeneration(k, meta.ReadyCondition) == k.Generation
223+
conditions.GetObservedGeneration(k, meta.ReadyCondition) == k.Generation &&
224+
conditions.GetReason(k, meta.ReconcilingCondition) == kustomizev1.ProgressingWithRetryReason
215225
}
216226

217227
func logStatus(t *testing.T, k *kustomizev1.Kustomization) {

0 commit comments

Comments
 (0)