Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flow control SubReconcilers #414

Merged
merged 2 commits into from Aug 31, 2023
Merged

Flow control SubReconcilers #414

merged 2 commits into from Aug 31, 2023

Conversation

scothis
Copy link
Contributor

@scothis scothis commented Aug 23, 2023

Spike of reconcilers that direct the flow of a reconcile request at runtime.

  • IfThen - branch execution based on a boolean condition
  • While - iterate over a reconciler while a condition is true
  • TryCatch - recover from errors or rewrite the result
  • OverrideSetup - suppress calls to the SetupWithManager method

@codecov
Copy link

codecov bot commented Aug 23, 2023

Codecov Report

Patch coverage: 60.00% and project coverage change: -0.04% ⚠️

Comparison is base (5be8c31) 60.62% compared to head (b21b653) 60.58%.

❗ Current head b21b653 differs from pull request most recent head 0c211d9. Consider uploading reports for the commit 0c211d9 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #414      +/-   ##
==========================================
- Coverage   60.62%   60.58%   -0.04%     
==========================================
  Files          24       25       +1     
  Lines        2334     2489     +155     
==========================================
+ Hits         1415     1508      +93     
- Misses        835      895      +60     
- Partials       84       86       +2     
Files Changed Coverage Δ
reconcilers/flow.go 60.00% <60.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +235 to +312
// Catch is called with the results from Try(). New results can be returned
// suppressing the original results.
//
// +optional
Catch func(ctx context.Context, resource Type, result Result, err error) (Result, error)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After trying TryCatch on for size mentally, I am wondering if an advice-style reconciler could be implemented with it. For example, could one update a single condition on a resource depending on whether a collection of steps was successful or not like so?

func ApplyResources(c reconcilers.Config) *reconcilers.SubReconciler {
	return &TryCatch{
		Try: reconcilers.Sequence{
			ApplyRegistryCredentialsPlaceholder(c),
			ApplyCerts(c),
			ApplyRedis(c, images.Redis),
			ApplyAuthServer(c, images.AuthServer),
			ApplyTokenSignatureKeys(c),
		},
		Catch: func(ctx, parent *v1alpha1.AuthServer, result Result, err error) {
			if err != nil {
				parent.Status.MarkResourcesError(ctx, err.Error())
				return result, nil
			} else {
				parent.Status.MarkResourcesApplied(ctx)
				return result, nil
			}
		},
		Config: c,
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. You'd probably want to do a little bit of detection on the err to figure out if it is something that should be suppressed or continued.

Reconcilers that direct the flow of a reconcile request at runtime.

- IfThen - branch execution based on a boolean condition
- While - iterate over a reconciler while a condition is true
- TryCatch - recover from errors or rewrite the result
- OverrideSetup - suppress calls to the SetupWithManager method

Signed-off-by: Scott Andrews <andrewssc@vmware.com>
@scothis scothis marked this pull request as ready for review August 29, 2023 22:05
Copy link
Collaborator

@mamachanko mamachanko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left a few questions and suggestions

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated

#### While

A [`While`](https://pkg.go.dev/github.com/vmware-labs/reconciler-runtime/reconcilers#While) calls the reconciler so long as the condition is true, up to the maximum number of iterations (defaults to 100). The current iteration index can be retried with [`RetrieveIteration`](https://pkg.go.dev/github.com/vmware-labs/reconciler-runtime/reconcilers#RetrieveIteration).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question, clarification] Iteration is done as a single resource's reconciliation, isn't it? And it's only iterating over the While and not the entire pipeline of reconcilers?

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
}
},
Catch: func(ctx context.Context, resource *buildv1alpha1.Function, result reconcile.Result, err error) (reconcile.Result, error) {
// suppress error
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// suppress error
// suppress error and/or
// log it and/or
// update the parent's status

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this example, it's surprising the error. Logging or updating status are other things that could be done.

Co-authored-by: Max Brauer <mamachanko@users.noreply.github.com>
Copy link
Collaborator

@mamachanko mamachanko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! Looking forward to drying up state maintenance with TryCatch

@scothis scothis merged commit 919abf6 into vmware-labs:main Aug 31, 2023
3 checks passed
@scothis scothis deleted the flow-control branch August 31, 2023 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants