mirror of
https://github.com/coder/coder.git
synced 2026-06-04 13:38:21 +00:00
27bc60d1b9
Closes https://github.com/coder/internal/issues/510 <details> <summary> Refactoring Summary </summary> ### 1) `CalculateActions` Function #### Issues Before Refactoring: - Large function (~150 lines), making it difficult to read and maintain. - The control flow is hard to follow due to complex conditional logic. - The `ReconciliationActions` struct was partially initialized early, then mutated in multiple places, making the flow error-prone. Original source: https://github.com/coder/coder/blob/fe60b569ad754245e28bac71e0ef3c83536631bb/coderd/prebuilds/state.go#L13-L167 #### Improvements After Refactoring: - Simplified and broken down into smaller, focused helper methods. - The flow of the function is now more linear and easier to understand. - Struct initialization is cleaner, avoiding partial and incremental mutations. Refactored function: https://github.com/coder/coder/blob/eeb0407d783cdda71ec2418c113f325542c47b1c/coderd/prebuilds/state.go#L67-L84 --- ### 2) `ReconciliationActions` Struct #### Issues Before Refactoring: - The struct mixed both actionable decisions and diagnostic state, which blurred its purpose. - It was unclear which fields were necessary for reconciliation logic, and which were purely for logging/observability. #### Improvements After Refactoring: - Split into two clear, purpose-specific structs: - **`ReconciliationActions`** — defines the intended reconciliation action. - **`ReconciliationState`** — captures runtime state and metadata, primarily for logging and diagnostics. Original struct: https://github.com/coder/coder/blob/fe60b569ad754245e28bac71e0ef3c83536631bb/coderd/prebuilds/reconcile.go#L29-L41 </details> --------- Signed-off-by: Danny Kopping <dannykopping@gmail.com> Co-authored-by: Sas Swart <sas.swart.cdk@gmail.com> Co-authored-by: Danny Kopping <dannykopping@gmail.com> Co-authored-by: Dean Sheather <dean@deansheather.com> Co-authored-by: Spike Curtis <spike@coder.com> Co-authored-by: Danny Kopping <danny@coder.com>
28 lines
1022 B
Go
28 lines
1022 B
Go
package prebuilds
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
// ReconciliationOrchestrator manages the lifecycle of prebuild reconciliation.
|
|
// It runs a continuous loop to check and reconcile prebuild states, and can be stopped gracefully.
|
|
type ReconciliationOrchestrator interface {
|
|
Reconciler
|
|
|
|
// RunLoop starts a continuous reconciliation loop that periodically calls ReconcileAll
|
|
// to ensure all prebuilds are in their desired states. The loop runs until the context
|
|
// is canceled or Stop is called.
|
|
RunLoop(ctx context.Context)
|
|
|
|
// Stop gracefully shuts down the orchestrator with the given cause.
|
|
// The cause is used for logging and error reporting.
|
|
Stop(ctx context.Context, cause error)
|
|
}
|
|
|
|
type Reconciler interface {
|
|
// ReconcileAll orchestrates the reconciliation of all prebuilds across all templates.
|
|
// It takes a global snapshot of the system state and then reconciles each preset
|
|
// in parallel, creating or deleting prebuilds as needed to reach their desired states.
|
|
ReconcileAll(ctx context.Context) error
|
|
}
|