mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
feat: implement reconciliation loop (#17261)
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>
This commit is contained in:
committed by
GitHub
parent
6a79965948
commit
27bc60d1b9
@@ -90,6 +90,17 @@ func Find[T any](haystack []T, cond func(T) bool) (T, bool) {
|
||||
return empty, false
|
||||
}
|
||||
|
||||
// Filter returns all elements that satisfy the condition.
|
||||
func Filter[T any](haystack []T, cond func(T) bool) []T {
|
||||
out := make([]T, 0, len(haystack))
|
||||
for _, hay := range haystack {
|
||||
if cond(hay) {
|
||||
out = append(out, hay)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Overlap returns if the 2 sets have any overlap (element(s) in common)
|
||||
func Overlap[T comparable](a []T, b []T) bool {
|
||||
return OverlapCompare(a, b, func(a, b T) bool {
|
||||
|
||||
@@ -2,6 +2,7 @@ package slice_test
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -82,6 +83,64 @@ func TestContains(t *testing.T) {
|
||||
)
|
||||
}
|
||||
|
||||
func TestFilter(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
type testCase[T any] struct {
|
||||
haystack []T
|
||||
cond func(T) bool
|
||||
expected []T
|
||||
}
|
||||
|
||||
{
|
||||
testCases := []*testCase[int]{
|
||||
{
|
||||
haystack: []int{1, 2, 3, 4, 5},
|
||||
cond: func(num int) bool {
|
||||
return num%2 == 1
|
||||
},
|
||||
expected: []int{1, 3, 5},
|
||||
},
|
||||
{
|
||||
haystack: []int{1, 2, 3, 4, 5},
|
||||
cond: func(num int) bool {
|
||||
return num%2 == 0
|
||||
},
|
||||
expected: []int{2, 4},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
actual := slice.Filter(tc.haystack, tc.cond)
|
||||
require.Equal(t, tc.expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
testCases := []*testCase[string]{
|
||||
{
|
||||
haystack: []string{"hello", "hi", "bye"},
|
||||
cond: func(str string) bool {
|
||||
return strings.HasPrefix(str, "h")
|
||||
},
|
||||
expected: []string{"hello", "hi"},
|
||||
},
|
||||
{
|
||||
haystack: []string{"hello", "hi", "bye"},
|
||||
cond: func(str string) bool {
|
||||
return strings.HasPrefix(str, "b")
|
||||
},
|
||||
expected: []string{"bye"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
actual := slice.Filter(tc.haystack, tc.cond)
|
||||
require.Equal(t, tc.expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOverlap(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user