Skip to content

Commit b552674

Browse files
Merge pull request #90 from numtide/defaults-package
refactor: extract defaulting logic to its own default module
2 parents f8648bb + 792bcbc commit b552674

File tree

21 files changed

+2408
-815
lines changed

21 files changed

+2408
-815
lines changed

pkg/cluster-handler/controller/multigrescluster/constants.go

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Package multigrescluster implements the controller for the root MultigresCluster resource.
2+
//
3+
// The MultigresCluster controller acts as the central orchestrator for the database system.
4+
// It is responsible for translating the high-level user intent into specific child resources.
5+
// Its primary responsibilities include:
6+
//
7+
// 1. Global Component Management:
8+
// It directly manages singleton resources defined at the cluster level, such as the
9+
// Global TopoServer (via a child TopoServer CR) and the MultiAdmin deployment.
10+
//
11+
// 2. Resource Fan-Out (Child CR Management):
12+
// It projects the configuration defined in the MultigresCluster spec (Cells and Databases)
13+
// into discrete child Custom Resources (Cell and TableGroup). These child resources are
14+
// then reconciled by their own respective controllers.
15+
//
16+
// 3. Template Resolution:
17+
// It leverages the 'pkg/resolver' module to fetch CoreTemplates, CellTemplates, and
18+
// ShardTemplates, merging them with user-defined overrides to produce the final
19+
// specifications for child resources.
20+
//
21+
// 4. Status Aggregation:
22+
// It continually observes the status of its child resources to produce a high-level
23+
// summary of the cluster's health (e.g., "All cells ready", "Database X has Y/Z shards ready").
24+
//
25+
// 5. Lifecycle Management:
26+
// It utilizes finalizers to ensure that all child resources are gracefully terminated
27+
// and cleaned up before the parent MultigresCluster is removed.
28+
package multigrescluster

pkg/cluster-handler/controller/multigrescluster/integration_test.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
multigresv1alpha1 "github.com/numtide/multigres-operator/api/v1alpha1"
2121
"github.com/numtide/multigres-operator/pkg/cluster-handler/controller/multigrescluster"
22+
"github.com/numtide/multigres-operator/pkg/resolver"
2223
"github.com/numtide/multigres-operator/pkg/testutil"
2324
)
2425

@@ -124,8 +125,10 @@ func TestMultigresClusterReconciliation(t *testing.T) {
124125
},
125126
Spec: multigresv1alpha1.TopoServerSpec{
126127
Etcd: &multigresv1alpha1.EtcdSpec{
127-
Image: "etcd:latest",
128-
Replicas: ptr.To(int32(3)), // Default from logic
128+
Image: "etcd:latest",
129+
Replicas: ptr.To(int32(3)), // Default from logic
130+
Storage: multigresv1alpha1.StorageSpec{Size: resolver.DefaultEtcdStorageSize},
131+
Resources: resolver.DefaultResourcesEtcd(),
129132
},
130133
},
131134
},
@@ -149,8 +152,9 @@ func TestMultigresClusterReconciliation(t *testing.T) {
149152
Spec: corev1.PodSpec{
150153
Containers: []corev1.Container{
151154
{
152-
Name: "multiadmin",
153-
Image: "admin:latest",
155+
Name: "multiadmin",
156+
Image: "admin:latest",
157+
Resources: resolver.DefaultResourcesAdmin(),
154158
},
155159
},
156160
},
@@ -278,18 +282,45 @@ func TestMultigresClusterReconciliation(t *testing.T) {
278282
t.Fatalf("Failed to create controller, %v", err)
279283
}
280284

281-
// 4. Create the Input
285+
// 4. Create Defaults (Templates)
286+
// The controller expects "default" templates to exist when TemplateDefaults are not specified.
287+
// We create empty templates so the resolution succeeds and falls back to hardcoded defaults or inline specs.
288+
emptyCore := &multigresv1alpha1.CoreTemplate{
289+
ObjectMeta: metav1.ObjectMeta{Name: "default", Namespace: namespace},
290+
Spec: multigresv1alpha1.CoreTemplateSpec{},
291+
}
292+
if err := k8sClient.Create(ctx, emptyCore); client.IgnoreAlreadyExists(err) != nil {
293+
t.Fatalf("Failed to create default core template: %v", err)
294+
}
295+
296+
emptyCell := &multigresv1alpha1.CellTemplate{
297+
ObjectMeta: metav1.ObjectMeta{Name: "default", Namespace: namespace},
298+
Spec: multigresv1alpha1.CellTemplateSpec{},
299+
}
300+
if err := k8sClient.Create(ctx, emptyCell); client.IgnoreAlreadyExists(err) != nil {
301+
t.Fatalf("Failed to create default cell template: %v", err)
302+
}
303+
304+
emptyShard := &multigresv1alpha1.ShardTemplate{
305+
ObjectMeta: metav1.ObjectMeta{Name: "default", Namespace: namespace},
306+
Spec: multigresv1alpha1.ShardTemplateSpec{},
307+
}
308+
if err := k8sClient.Create(ctx, emptyShard); client.IgnoreAlreadyExists(err) != nil {
309+
t.Fatalf("Failed to create default shard template: %v", err)
310+
}
311+
312+
// 5. Create the Input
282313
if err := k8sClient.Create(ctx, tc.cluster); err != nil {
283314
t.Fatalf("Failed to create the initial cluster, %v", err)
284315
}
285316

286-
// 5. Assert Logic: Wait for Children
317+
// 6. Assert Logic: Wait for Children
287318
// This ensures the controller has run and reconciled at least once successfully
288319
if err := watcher.WaitForMatch(tc.wantResources...); err != nil {
289320
t.Errorf("Resources mismatch:\n%v", err)
290321
}
291322

292-
// 6. Verify Parent Finalizer (Manual Check)
323+
// 7. Verify Parent Finalizer (Manual Check)
293324
// We check this manually to avoid fighting with status/spec diffs in the watcher
294325
fetchedCluster := &multigresv1alpha1.MultigresCluster{}
295326
if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(tc.cluster), fetchedCluster); err != nil {

0 commit comments

Comments
 (0)