Skip to content

Commit 4c54905

Browse files
authored
handle requeues after setting Synced condition (#73)
Issue: aws-controllers-k8s/community#1186 Description of changes: * Call `handleRequeues` method after setting the `ACK.ResourceSynced` condition * Use `AWSResourceManager.IsSynced` method to determine the `ACK.ResourceSynced` condition * Only set `ACK.ResourceSynced` condition inside `ensureConditions` function when the condition is originally missing * Update existing unit-tests to include `rm.IsSynced()` call and Add a new unit-test when `rm.IsSynced` returns false ------ * Also tested locally using Sagemaker controller, whose e2e tests verify that SyncedCondition is properly set in success and failure scenarios. This ensures there is no breakage in existing functionality. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 0701cb8 commit 4c54905

File tree

3 files changed

+256
-46
lines changed

3 files changed

+256
-46
lines changed

pkg/condition/condition.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ var (
2929
NotManagedReason = "This resource already exists but is not managed by ACK. " +
3030
"To bring the resource under ACK management, you should explicitly adopt " +
3131
"the resource by creating a services.k8s.aws/AdoptedResource"
32+
NotSyncedMessage = "Resource not synced"
33+
SyncedMessage = "Resource synced successfully"
3234
)
3335

3436
// Synced returns the Condition in the resource's Conditions collection that is

pkg/runtime/reconciler.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@ func (r *resourceReconciler) reconcile(
188188
res, _ = rm.ResolveReferences(ctx, r.apiReader, res)
189189
return r.deleteResource(ctx, rm, res)
190190
}
191-
return r.Sync(ctx, rm, res)
191+
latest, err := r.Sync(ctx, rm, res)
192+
if err != nil {
193+
return latest, err
194+
}
195+
return r.handleRequeues(ctx, latest)
192196
}
193197

194198
// Sync ensures that the supplied AWSResource's backing API resource
@@ -209,7 +213,7 @@ func (r *resourceReconciler) Sync(
209213

210214
r.resetConditions(ctx, desired)
211215
defer func() {
212-
r.ensureConditions(ctx, latest, err)
216+
r.ensureConditions(ctx, rm, latest, err)
213217
}()
214218

215219
isAdopted := IsAdopted(desired)
@@ -244,18 +248,9 @@ func (r *resourceReconciler) Sync(
244248
// Attempt to late initialize the resource. If there are no fields to
245249
// late initialize, this operation will be a no-op.
246250
if latest, err = r.lateInitializeResource(ctx, rm, latest); err != nil {
247-
// TODO(vijtrip2): move this condition handling to generated
248-
// AWSResourceManager.LateInitialize() method
249-
250-
// Whenever late initialization fails for a resource, set ACK.ResourceSynced
251-
// condition explicitly to "False"
252-
// Setting this explicitly to False is required because ACK.ResourceSynced
253-
// condition can be True due to successful Create/Update call OR no
254-
// Create/Update call in reconciler loop
255-
ackcondition.SetSynced(latest, corev1.ConditionFalse, nil, nil)
256251
return latest, err
257252
}
258-
return r.handleRequeues(ctx, latest)
253+
return latest, nil
259254
}
260255

261256
// resetConditions strips the supplied resource of all objects in its
@@ -280,6 +275,7 @@ func (r *resourceReconciler) resetConditions(
280275
// objects and ensures that an ACK.ResourceSynced condition is present.
281276
func (r *resourceReconciler) ensureConditions(
282277
ctx context.Context,
278+
rm acktypes.AWSResourceManager,
283279
res acktypes.AWSResource,
284280
reconcileErr error,
285281
) {
@@ -292,24 +288,37 @@ func (r *resourceReconciler) ensureConditions(
292288
exit := rlog.Trace("r.ensureConditions")
293289
defer exit(err)
294290

295-
if syncedCond := ackcondition.Synced(res); syncedCond == nil {
296-
rlog.Debug("resource missing ACK.ResourceSynced condition")
297-
// only the resource manager will know whether the resource is in a
298-
// stable sync state. Even if we got no error back from the
299-
// create/update operations, we can only set this to Unknown.
300-
condStatus := corev1.ConditionUnknown
291+
// If the ACK.ResourceSynced condition is not set using the custom hooks,
292+
// determine the Synced condition using "rm.IsSynced" method
293+
if ackcondition.Synced(res) == nil {
294+
condStatus := corev1.ConditionFalse
295+
synced := false
296+
condMessage := ackcondition.NotSyncedMessage
297+
var condReason string
298+
rlog.Enter("rm.IsSynced")
299+
if synced, err = rm.IsSynced(ctx, res); err == nil && synced {
300+
condStatus = corev1.ConditionTrue
301+
condMessage = ackcondition.SyncedMessage
302+
} else if err != nil {
303+
condReason = err.Error()
304+
}
305+
rlog.Exit("rm.IsSynced", err)
306+
301307
if reconcileErr != nil {
308+
condReason = reconcileErr.Error()
302309
if reconcileErr == ackerr.Terminal {
303310
// A terminal condition by its very nature indicates a stable state
304311
// for a resource being synced. The resource is considered synced
305312
// because its state will not change.
306313
condStatus = corev1.ConditionTrue
314+
condMessage = ackcondition.SyncedMessage
307315
} else {
308316
// For any other reconciler error, set synced condition to false
309317
condStatus = corev1.ConditionFalse
318+
condMessage = ackcondition.NotSyncedMessage
310319
}
311320
}
312-
ackcondition.SetSynced(res, condStatus, nil, nil)
321+
ackcondition.SetSynced(res, condStatus, &condMessage, &condReason)
313322
}
314323
}
315324

0 commit comments

Comments
 (0)