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

refactor: create separate reconciler for control flow Stage #2848

Merged
merged 10 commits into from
Oct 28, 2024

Conversation

hiddeco
Copy link
Contributor

@hiddeco hiddeco commented Oct 25, 2024

The main problem with the Stage reconciler at present is the level of complexity, which makes it hard to reason about behavior due to the sheer number of responsibilities it handles, mixing both regular Stage and control flow Stage reconciliation logic into a single controller.

This PR is the first step in a larger refactoring effort aimed at improving the Stage reconciliation logic. The effort consists of two phases:

  1. Splitting the reconciler into two separate controllers:

    • A new ControlFlowStageReconciler that handles Stages without promotion templates (i.e. "control flow" Stages)
    • The existing Stage reconciler (to be renamed) that handles Stages with promotion templates

    This PR implements the first part by extracting control flow Stage logic into a dedicated controller.

  2. Rewriting the remaining Stage reconciler to follow the same patterns established in this PR:

    • Clear separation of concerns and more focused reconciliation logic
    • Better testability through smaller components
    • A less procedural and more observant flow

    This will be addressed in a follow-up PR.

The end goal is to have two well-structured controllers that each handle a specific type of Stage, making the codebase easier to understand, test, and modify.


⚠️ Though the LOC count may appear large, the majority of changes are newly added tests — the actual logic is only around 1200 lines.

Signed-off-by: Hidde Beydals <hidde@hhh.computer>
Signed-off-by: Hidde Beydals <hidde@hhh.computer>
Copy link

netlify bot commented Oct 25, 2024

Deploy Preview for docs-kargo-io ready!

Name Link
🔨 Latest commit 405409d
🔍 Latest deploy log https://app.netlify.com/sites/docs-kargo-io/deploys/67200f12093c8d0008292369
😎 Deploy Preview https://deploy-preview-2848.docs.kargo.io
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

codecov bot commented Oct 25, 2024

Codecov Report

Attention: Patch coverage is 66.13419% with 212 lines in your changes missing coverage. Please review.

Project coverage is 50.01%. Comparing base (3b4e223) to head (405409d).
Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
internal/controller/stages/control_flow_stages.go 76.39% 95 Missing and 2 partials ⚠️
internal/controller/stages/stages.go 0.00% 85 Missing ⚠️
internal/controller/stages/event_handlers.go 80.00% 10 Missing and 5 partials ⚠️
cmd/controlplane/controller.go 0.00% 9 Missing ⚠️
internal/indexer/indexer.go 62.50% 3 Missing ⚠️
internal/indexer/shared_field_indexer.go 81.25% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2848      +/-   ##
==========================================
+ Coverage   48.82%   50.01%   +1.18%     
==========================================
  Files         270      274       +4     
  Lines       23941    24386     +445     
==========================================
+ Hits        11690    12197     +507     
+ Misses      11620    11539      -81     
- Partials      631      650      +19     

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

// This index is used to find all Freight that are directly available from
// a Warehouse. It is used to find Freight that can be sourced directly from
// the Warehouse for the control flow Stage.
if err := mgr.GetFieldIndexer().IndexField(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a matter of personal preference, but I never liked the way the registration was factored out because it hid two important things from me when I wanted to know what a reconciler cared about:

  1. The index field that's important to the reconciler.
  2. The actual thing that decides how the value of this field is determined.

Given this, I took the liberty of not using the abstraction but rather doing the registration by hand here.

Copy link
Member

Choose a reason for hiding this comment

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

Excellent rationale. 👍

// SetupWithManager sets up the control flow Stage reconciler with the given
// controller manager. It registers the reconciler with the manager and sets up
// watches on the required objects.
func (r *ControlFlowStageReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even if I like to be idiomatic and puristic at times, this is how controller-runtime projects do it — and would fit more into what we have set out for ourselves with #1479.

https://xkcd.com/927/

Copy link
Member

Choose a reason for hiding this comment

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

Far from being a hill I would die on, I think the advantage of the other approach was that a caller in another package could obtain a fully-initialized reconciler after a single function call. With this approach, it requires two, which creates the possibility of obtaining a partially-initialized reconciler.

As I said, it's not a hill I would die on. Merely explaining the rationale behind the prior approach.

)
// If the Stage is a control flow Stage, there is no need to reconcile it.
if stage.IsControlFlow() {
return
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously, this would always result in a reconciliation — even if there wasn't any actual interest.

@@ -478,6 +513,11 @@ func (p *phaseChangedAnalysisRunHandler[T]) Update(
)
}
for _, stage := range stages.Items {
// If the Stage is a control flow Stage, there is no need to reconcile it.
if stage.IsControlFlow() {
continue
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously, this would always result in a reconciliation — even if there wasn't any actual interest.

}
return ctrl.Result{}, fmt.Errorf("failed to update Stage status: %w", err)
}
return ctrl.Result{}, reconcileErr
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I need to test this more thoroughly, but I believe there is no actual reason for us to requeue on an interval here because we should be informed by a watcher or change event.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tested this, and what's expected from it is triggered just fine without it.

@hiddeco hiddeco force-pushed the factor-out-flow-stage branch from d7b4ca4 to 33a6bce Compare October 25, 2024 23:33
@hiddeco hiddeco marked this pull request as ready for review October 25, 2024 23:51
@hiddeco hiddeco requested a review from a team as a code owner October 25, 2024 23:51
@hiddeco hiddeco force-pushed the factor-out-flow-stage branch from 97b6bfd to c963f03 Compare October 28, 2024 20:45
Signed-off-by: Hidde Beydals <hidde@hhh.computer>
Signed-off-by: Hidde Beydals <hidde@hhh.computer>
Signed-off-by: Hidde Beydals <hidde@hhh.computer>
Signed-off-by: Hidde Beydals <hidde@hhh.computer>
Add a `SharedFieldIndexer` wrapper around `client.FieldIndexer` to
prevent multiple reconcilers from creating duplicate indices for the
same fields. This avoids errors by ensuring each field is only indexed
once, while still allowing reconcilers to manage their indices
independently.

Signed-off-by: Hidde Beydals <hidde@hhh.computer>
Signed-off-by: Hidde Beydals <hidde@hhh.computer>
Signed-off-by: Hidde Beydals <hidde@hhh.computer>
Signed-off-by: Hidde Beydals <hidde@hhh.computer>
@hiddeco hiddeco force-pushed the factor-out-flow-stage branch from 2d4af35 to 405409d Compare October 28, 2024 22:24
@hiddeco hiddeco enabled auto-merge October 28, 2024 22:25
@hiddeco hiddeco added this pull request to the merge queue Oct 28, 2024
Merged via the queue into akuity:main with commit b2be266 Oct 28, 2024
17 checks passed
@hiddeco hiddeco deleted the factor-out-flow-stage branch October 28, 2024 22:45
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.

2 participants