Skip to content

Commit 78882b3

Browse files
darkowlzzhiddeco
authored andcommitted
Consolidate result conversion and computation
Consolidate BuildRuntimeResult() into summarizeAndPatch() to simplify where the results are computed, summarized and patched. Move the event recording and logging of context specific errors into RecordContextualError() and call it in summarizeAndPatch(). Introduce Waiting error for wait and requeue scenarios. Update ComputeReconcileResult() and RecordContextualError() to consider Waiting error. Signed-off-by: Sunny <[email protected]>
1 parent 474658a commit 78882b3

File tree

8 files changed

+160
-93
lines changed

8 files changed

+160
-93
lines changed

controllers/bucket_controller.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,19 @@ func (r *BucketReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
139139
return ctrl.Result{}, nil
140140
}
141141

142-
// Initialize the patch helper
142+
// Initialize the patch helper with the current version of the object.
143143
patchHelper, err := patch.NewHelper(obj, r.Client)
144144
if err != nil {
145145
return ctrl.Result{}, err
146146
}
147147

148+
// recResult stores the abstracted reconcile result.
148149
var recResult sreconcile.Result
149150

150151
// Always attempt to patch the object and status after each reconciliation
151-
// NOTE: This deferred block only modifies the named return error. The
152-
// result from the reconciliation remains the same. Any requeue attributes
153-
// set in the result will continue to be effective.
152+
// NOTE: The final runtime result and error are set in this block.
154153
defer func() {
155-
retErr = r.summarizeAndPatch(ctx, obj, patchHelper, recResult, retErr)
154+
result, retErr = r.summarizeAndPatch(ctx, obj, patchHelper, recResult, retErr)
156155

157156
// Always record readiness and duration metrics
158157
r.Metrics.RecordReadiness(ctx, obj)
@@ -163,13 +162,13 @@ func (r *BucketReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
163162
if !controllerutil.ContainsFinalizer(obj, sourcev1.SourceFinalizer) {
164163
controllerutil.AddFinalizer(obj, sourcev1.SourceFinalizer)
165164
recResult = sreconcile.ResultRequeue
166-
return ctrl.Result{Requeue: true}, nil
165+
return
167166
}
168167

169168
// Examine if the object is under deletion
170169
if !obj.ObjectMeta.DeletionTimestamp.IsZero() {
171-
res, err := r.reconcileDelete(ctx, obj)
172-
return sreconcile.BuildRuntimeResult(ctx, r.EventRecorder, obj, res, err)
170+
recResult, retErr = r.reconcileDelete(ctx, obj)
171+
return
173172
}
174173

175174
// Reconcile actual object
@@ -178,21 +177,30 @@ func (r *BucketReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
178177
r.reconcileSource,
179178
r.reconcileArtifact,
180179
}
181-
recResult, err = r.reconcile(ctx, obj, reconcilers)
182-
return sreconcile.BuildRuntimeResult(ctx, r.EventRecorder, obj, recResult, err)
180+
recResult, retErr = r.reconcile(ctx, obj, reconcilers)
181+
return
183182
}
184183

185184
// summarizeAndPatch analyzes the object conditions to create a summary of the
186-
// status conditions and patches the object with the calculated summary.
187-
func (r *BucketReconciler) summarizeAndPatch(ctx context.Context, obj *sourcev1.Bucket, patchHelper *patch.Helper, res sreconcile.Result, recErr error) error {
185+
// status conditions, computes runtime results and patches the object in the K8s
186+
// API server.
187+
func (r *BucketReconciler) summarizeAndPatch(
188+
ctx context.Context,
189+
obj *sourcev1.Bucket,
190+
patchHelper *patch.Helper,
191+
res sreconcile.Result,
192+
recErr error) (ctrl.Result, error) {
193+
sreconcile.RecordContextualError(ctx, r.EventRecorder, obj, recErr)
194+
188195
// Record the value of the reconciliation request if any.
189196
if v, ok := meta.ReconcileAnnotationValue(obj.GetAnnotations()); ok {
190197
obj.Status.SetLastHandledReconcileRequest(v)
191198
}
192199

193200
// Compute the reconcile results, obtain patch options and reconcile error.
194201
var patchOpts []patch.Option
195-
patchOpts, recErr = sreconcile.ComputeReconcileResult(obj, res, recErr, bucketOwnedConditions)
202+
var result ctrl.Result
203+
patchOpts, result, recErr = sreconcile.ComputeReconcileResult(obj, obj.GetRequeueAfter(), res, recErr, bucketOwnedConditions)
196204

197205
// Summarize the Ready condition based on abnormalities that may have been observed.
198206
conditions.SetSummary(obj,
@@ -214,7 +222,7 @@ func (r *BucketReconciler) summarizeAndPatch(ctx context.Context, obj *sourcev1.
214222
recErr = kerrors.NewAggregate([]error{recErr, err})
215223
}
216224

217-
return recErr
225+
return result, recErr
218226
}
219227

220228
// reconcile steps iterates through the actual reconciliation tasks for objec,

controllers/gitrepository_controller.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,19 @@ func (r *GitRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Reques
145145
return ctrl.Result{}, nil
146146
}
147147

148-
// Initialize the patch helper
148+
// Initialize the patch helper with the current version of the object.
149149
patchHelper, err := patch.NewHelper(obj, r.Client)
150150
if err != nil {
151151
return ctrl.Result{}, err
152152
}
153153

154+
// recResult stores the abstracted reconcile result.
154155
var recResult sreconcile.Result
155156

156157
// Always attempt to patch the object and status after each reconciliation
157-
// NOTE: This deferred block only modifies the named return error. The
158-
// result from the reconciliation remains the same. Any requeue attributes
159-
// set in the result will continue to be effective.
158+
// NOTE: The final runtime result and error are set in this block.
160159
defer func() {
161-
retErr = r.summarizeAndPatch(ctx, obj, patchHelper, recResult, retErr)
160+
result, retErr = r.summarizeAndPatch(ctx, obj, patchHelper, recResult, retErr)
162161

163162
// Always record readiness and duration metrics
164163
r.Metrics.RecordReadiness(ctx, obj)
@@ -170,13 +169,13 @@ func (r *GitRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Reques
170169
if !controllerutil.ContainsFinalizer(obj, sourcev1.SourceFinalizer) {
171170
controllerutil.AddFinalizer(obj, sourcev1.SourceFinalizer)
172171
recResult = sreconcile.ResultRequeue
173-
return ctrl.Result{Requeue: true}, nil
172+
return
174173
}
175174

176175
// Examine if the object is under deletion
177176
if !obj.ObjectMeta.DeletionTimestamp.IsZero() {
178-
res, err := r.reconcileDelete(ctx, obj)
179-
return sreconcile.BuildRuntimeResult(ctx, r.EventRecorder, obj, res, err)
177+
recResult, retErr = r.reconcileDelete(ctx, obj)
178+
return
180179
}
181180

182181
// Reconcile actual object
@@ -186,21 +185,30 @@ func (r *GitRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Reques
186185
r.reconcileInclude,
187186
r.reconcileArtifact,
188187
}
189-
recResult, err = r.reconcile(ctx, obj, reconcilers)
190-
return sreconcile.BuildRuntimeResult(ctx, r.EventRecorder, obj, recResult, err)
188+
recResult, retErr = r.reconcile(ctx, obj, reconcilers)
189+
return
191190
}
192191

193192
// summarizeAndPatch analyzes the object conditions to create a summary of the
194-
// status conditions and patches the object with the calculated summary.
195-
func (r *GitRepositoryReconciler) summarizeAndPatch(ctx context.Context, obj *sourcev1.GitRepository, patchHelper *patch.Helper, res sreconcile.Result, recErr error) error {
193+
// status conditions, computes runtime results and patches the object in the K8s
194+
// API server.
195+
func (r *GitRepositoryReconciler) summarizeAndPatch(
196+
ctx context.Context,
197+
obj *sourcev1.GitRepository,
198+
patchHelper *patch.Helper,
199+
res sreconcile.Result,
200+
recErr error) (ctrl.Result, error) {
201+
sreconcile.RecordContextualError(ctx, r.EventRecorder, obj, recErr)
202+
196203
// Record the value of the reconciliation request if any.
197204
if v, ok := meta.ReconcileAnnotationValue(obj.GetAnnotations()); ok {
198205
obj.Status.SetLastHandledReconcileRequest(v)
199206
}
200207

201208
// Compute the reconcile results, obtain patch options and reconcile error.
202209
var patchOpts []patch.Option
203-
patchOpts, recErr = sreconcile.ComputeReconcileResult(obj, res, recErr, gitRepoOwnedConditions)
210+
var result ctrl.Result
211+
patchOpts, result, recErr = sreconcile.ComputeReconcileResult(obj, obj.GetRequeueAfter(), res, recErr, gitRepoOwnedConditions)
204212

205213
// Summarize the Ready condition based on abnormalities that may have been observed.
206214
conditions.SetSummary(obj,
@@ -222,7 +230,7 @@ func (r *GitRepositoryReconciler) summarizeAndPatch(ctx context.Context, obj *so
222230
recErr = kerrors.NewAggregate([]error{recErr, err})
223231
}
224232

225-
return recErr
233+
return result, recErr
226234
}
227235

228236
// reconcile steps iterates through the actual reconciliation tasks for objec,

controllers/helmchart_controller.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,21 +169,19 @@ func (r *HelmChartReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
169169
return ctrl.Result{}, nil
170170
}
171171

172-
// Initialize the patch helper
172+
// Initialize the patch helper with the current version of the object.
173173
patchHelper, err := patch.NewHelper(obj, r.Client)
174174
if err != nil {
175175
return ctrl.Result{}, err
176176
}
177177

178-
// Result of the sub-reconciliation
178+
// recResult stores the abstracted reconcile result.
179179
var recResult sreconcile.Result
180180

181181
// Always attempt to patch the object after each reconciliation.
182-
// NOTE: This deferred block only modifies the named return error. The
183-
// result from the reconciliation remains the same. Any requeue attributes
184-
// set in the result will continue to be effective.
182+
// NOTE: The final runtime result and error are set in this block.
185183
defer func() {
186-
retErr = r.summarizeAndPatch(ctx, obj, patchHelper, recResult, retErr)
184+
result, retErr = r.summarizeAndPatch(ctx, obj, patchHelper, recResult, retErr)
187185

188186
// Always record readiness and duration metrics
189187
r.Metrics.RecordReadiness(ctx, obj)
@@ -195,13 +193,13 @@ func (r *HelmChartReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
195193
if !controllerutil.ContainsFinalizer(obj, sourcev1.SourceFinalizer) {
196194
controllerutil.AddFinalizer(obj, sourcev1.SourceFinalizer)
197195
recResult = sreconcile.ResultRequeue
198-
return ctrl.Result{Requeue: true}, nil
196+
return
199197
}
200198

201199
// Examine if the object is under deletion
202200
if !obj.ObjectMeta.DeletionTimestamp.IsZero() {
203-
res, err := r.reconcileDelete(ctx, obj)
204-
return sreconcile.BuildRuntimeResult(ctx, r.EventRecorder, obj, res, err)
201+
recResult, retErr = r.reconcileDelete(ctx, obj)
202+
return
205203
}
206204

207205
// Reconcile actual object
@@ -210,23 +208,30 @@ func (r *HelmChartReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
210208
r.reconcileSource,
211209
r.reconcileArtifact,
212210
}
213-
recResult, err = r.reconcile(ctx, obj, reconcilers)
214-
return sreconcile.BuildRuntimeResult(ctx, r.EventRecorder, obj, recResult, err)
211+
recResult, retErr = r.reconcile(ctx, obj, reconcilers)
212+
return
215213
}
216214

217215
// summarizeAndPatch analyzes the object conditions to create a summary of the
218-
// status conditions and patches the object with the calculated summary. The
219-
// reconciler error type is also used to determine the conditions and the
220-
// returned error.
221-
func (r *HelmChartReconciler) summarizeAndPatch(ctx context.Context, obj *sourcev1.HelmChart, patchHelper *patch.Helper, res sreconcile.Result, recErr error) error {
216+
// status conditions, computes runtime results and patches the object in the K8s
217+
// API server.
218+
func (r *HelmChartReconciler) summarizeAndPatch(
219+
ctx context.Context,
220+
obj *sourcev1.HelmChart,
221+
patchHelper *patch.Helper,
222+
res sreconcile.Result,
223+
recErr error) (ctrl.Result, error) {
224+
sreconcile.RecordContextualError(ctx, r.EventRecorder, obj, recErr)
225+
222226
// Record the value of the reconciliation request, if any
223227
if v, ok := meta.ReconcileAnnotationValue(obj.GetAnnotations()); ok {
224228
obj.Status.SetLastHandledReconcileRequest(v)
225229
}
226230

227231
// Compute the reconcile results, obtain patch options and reconcile error.
228232
var patchOpts []patch.Option
229-
patchOpts, recErr = sreconcile.ComputeReconcileResult(obj, res, recErr, helmChartOwnedConditions)
233+
var result ctrl.Result
234+
patchOpts, result, recErr = sreconcile.ComputeReconcileResult(obj, obj.GetRequeueAfter(), res, recErr, helmChartOwnedConditions)
230235

231236
// Summarize Ready condition
232237
conditions.SetSummary(obj,
@@ -247,7 +252,7 @@ func (r *HelmChartReconciler) summarizeAndPatch(ctx context.Context, obj *source
247252
}
248253
recErr = kerrors.NewAggregate([]error{recErr, err})
249254
}
250-
return recErr
255+
return result, recErr
251256
}
252257

253258
// reconcile steps through the actual reconciliation tasks for the object, it returns early on the first step that

controllers/helmchart_controller_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,8 @@ func TestHelmChartReconciler_summarizeAndPatch(t *testing.T) {
13721372

13731373
builder := fake.NewClientBuilder().WithScheme(testEnv.GetScheme())
13741374
r := &HelmChartReconciler{
1375-
Client: builder.Build(),
1375+
Client: builder.Build(),
1376+
EventRecorder: record.NewFakeRecorder(32),
13761377
}
13771378
obj := &sourcev1.HelmChart{
13781379
ObjectMeta: metav1.ObjectMeta{
@@ -1393,7 +1394,7 @@ func TestHelmChartReconciler_summarizeAndPatch(t *testing.T) {
13931394
patchHelper, err := patch.NewHelper(obj, r.Client)
13941395
g.Expect(err).ToNot(HaveOccurred())
13951396

1396-
gotErr := r.summarizeAndPatch(ctx, obj, patchHelper, tt.result, tt.reconcileErr)
1397+
_, gotErr := r.summarizeAndPatch(ctx, obj, patchHelper, tt.result, tt.reconcileErr)
13971398
g.Expect(gotErr != nil).To(Equal(tt.wantErr))
13981399

13991400
g.Expect(obj.Status.Conditions).To(conditions.MatchConditions(tt.assertConditions))

controllers/helmrepository_controller.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,19 @@ func (r *HelmRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Reque
132132
return ctrl.Result{}, nil
133133
}
134134

135-
// Initialize the patch helper
135+
// Initialize the patch helper with the current version of the object.
136136
patchHelper, err := patch.NewHelper(obj, r.Client)
137137
if err != nil {
138138
return ctrl.Result{}, err
139139
}
140140

141-
// Result of the sub-reconciliation.
141+
// recResult stores the abstracted reconcile result.
142142
var recResult sreconcile.Result
143143

144144
// Always attempt to patch the object after each reconciliation.
145-
// NOTE: This deferred block only modifies the named return error. The
146-
// result from the reconciliation remains the same. Any requeue attributes
147-
// set in the result will continue to be effective.
145+
// NOTE: The final runtime result and error are set in this block.
148146
defer func() {
149-
retErr = r.summarizeAndPatch(ctx, obj, patchHelper, recResult, retErr)
147+
result, retErr = r.summarizeAndPatch(ctx, obj, patchHelper, recResult, retErr)
150148

151149
// Always record readiness and duration metrics
152150
r.Metrics.RecordReadiness(ctx, obj)
@@ -158,13 +156,13 @@ func (r *HelmRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Reque
158156
if !controllerutil.ContainsFinalizer(obj, sourcev1.SourceFinalizer) {
159157
controllerutil.AddFinalizer(obj, sourcev1.SourceFinalizer)
160158
recResult = sreconcile.ResultRequeue
161-
return ctrl.Result{Requeue: true}, nil
159+
return
162160
}
163161

164162
// Examine if the object is under deletion
165163
if !obj.ObjectMeta.DeletionTimestamp.IsZero() {
166-
res, err := r.reconcileDelete(ctx, obj)
167-
return sreconcile.BuildRuntimeResult(ctx, r.EventRecorder, obj, res, err)
164+
recResult, retErr = r.reconcileDelete(ctx, obj)
165+
return
168166
}
169167

170168
// Reconcile actual object
@@ -173,23 +171,30 @@ func (r *HelmRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Reque
173171
r.reconcileSource,
174172
r.reconcileArtifact,
175173
}
176-
recResult, err = r.reconcile(ctx, obj, reconcilers)
177-
return sreconcile.BuildRuntimeResult(ctx, r.EventRecorder, obj, recResult, err)
174+
recResult, retErr = r.reconcile(ctx, obj, reconcilers)
175+
return
178176
}
179177

180178
// summarizeAndPatch analyzes the object conditions to create a summary of the
181-
// status conditions and patches the object with the calculated summary. The
182-
// reconciler error type is also used to determine the conditions and the
183-
// returned error.
184-
func (r *HelmRepositoryReconciler) summarizeAndPatch(ctx context.Context, obj *sourcev1.HelmRepository, patchHelper *patch.Helper, res sreconcile.Result, recErr error) error {
179+
// status conditions, computes runtime results and patches the object in the K8s
180+
// API server.
181+
func (r *HelmRepositoryReconciler) summarizeAndPatch(
182+
ctx context.Context,
183+
obj *sourcev1.HelmRepository,
184+
patchHelper *patch.Helper,
185+
res sreconcile.Result,
186+
recErr error) (ctrl.Result, error) {
187+
sreconcile.RecordContextualError(ctx, r.EventRecorder, obj, recErr)
188+
185189
// Record the value of the reconciliation request, if any.
186190
if v, ok := meta.ReconcileAnnotationValue(obj.GetAnnotations()); ok {
187191
obj.Status.SetLastHandledReconcileRequest(v)
188192
}
189193

190194
// Compute the reconcile results, obtain patch options and reconcile error.
191195
var patchOpts []patch.Option
192-
patchOpts, recErr = sreconcile.ComputeReconcileResult(obj, res, recErr, helmRepoOwnedConditions)
196+
var result ctrl.Result
197+
patchOpts, result, recErr = sreconcile.ComputeReconcileResult(obj, obj.GetRequeueAfter(), res, recErr, helmRepoOwnedConditions)
193198

194199
// Summarize Ready condition.
195200
conditions.SetSummary(obj,
@@ -211,7 +216,7 @@ func (r *HelmRepositoryReconciler) summarizeAndPatch(ctx context.Context, obj *s
211216
recErr = kerrors.NewAggregate([]error{recErr, err})
212217
}
213218

214-
return recErr
219+
return result, recErr
215220
}
216221

217222
// reconcile iterates through the sub-reconcilers and processes the source

controllers/helmrepository_controller_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,8 @@ func TestHelmRepositoryReconciler_summarizeAndPatch(t *testing.T) {
752752

753753
builder := fakeclient.NewClientBuilder().WithScheme(testEnv.GetScheme())
754754
r := &HelmRepositoryReconciler{
755-
Client: builder.Build(),
755+
Client: builder.Build(),
756+
EventRecorder: record.NewFakeRecorder(32),
756757
}
757758
obj := &sourcev1.HelmRepository{
758759
ObjectMeta: metav1.ObjectMeta{
@@ -773,7 +774,7 @@ func TestHelmRepositoryReconciler_summarizeAndPatch(t *testing.T) {
773774
patchHelper, err := patch.NewHelper(obj, r.Client)
774775
g.Expect(err).ToNot(HaveOccurred())
775776

776-
gotErr := r.summarizeAndPatch(ctx, obj, patchHelper, tt.result, tt.reconcileErr)
777+
_, gotErr := r.summarizeAndPatch(ctx, obj, patchHelper, tt.result, tt.reconcileErr)
777778
g.Expect(gotErr != nil).To(Equal(tt.wantErr))
778779

779780
g.Expect(obj.Status.Conditions).To(conditions.MatchConditions(tt.assertConditions))

0 commit comments

Comments
 (0)