Skip to content

Commit bde8a75

Browse files
committed
feat: adding lifecycle builder
1 parent cb59786 commit bde8a75

File tree

7 files changed

+199
-14
lines changed

7 files changed

+199
-14
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package builder
2+
3+
import (
4+
"sigs.k8s.io/controller-runtime/pkg/client"
5+
mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager"
6+
7+
"github.com/platform-mesh/golang-commons/controller/lifecycle/controllerruntime"
8+
"github.com/platform-mesh/golang-commons/controller/lifecycle/multicluster"
9+
"github.com/platform-mesh/golang-commons/controller/lifecycle/subroutine"
10+
"github.com/platform-mesh/golang-commons/logger"
11+
)
12+
13+
type Builder struct {
14+
operatorName string
15+
controllerName string
16+
withConditionManagement bool
17+
withSpreadingReconciles bool
18+
withReadOnly bool
19+
subroutines []subroutine.Subroutine
20+
log *logger.Logger
21+
}
22+
23+
func NewBuilder(operatorName, controllerName string, subroutines []subroutine.Subroutine, log *logger.Logger) *Builder {
24+
return &Builder{
25+
operatorName: operatorName,
26+
controllerName: controllerName,
27+
log: log,
28+
withConditionManagement: false,
29+
subroutines: subroutines,
30+
}
31+
}
32+
33+
func (b *Builder) WithConditionManagement() *Builder {
34+
b.withConditionManagement = true
35+
return b
36+
}
37+
38+
func (b *Builder) WithSpreadingReconciles() *Builder {
39+
b.withSpreadingReconciles = true
40+
return b
41+
}
42+
43+
func (b *Builder) WithReadOnly() *Builder {
44+
b.withReadOnly = true
45+
return b
46+
}
47+
48+
func (b *Builder) BuildControllerRuntime(cl client.Client) *controllerruntime.LifecycleManager {
49+
lm := controllerruntime.NewLifecycleManager(b.subroutines, b.operatorName, b.controllerName, cl, b.log)
50+
if b.withConditionManagement {
51+
lm.WithConditionManagement()
52+
}
53+
if b.withSpreadingReconciles {
54+
lm.WithSpreadingReconciles()
55+
}
56+
if b.withReadOnly {
57+
lm.WithReadOnly()
58+
}
59+
return lm
60+
}
61+
62+
func (b *Builder) BuildMultiCluster(mgr mcmanager.Manager) *multicluster.LifecycleManager {
63+
lm := multicluster.NewLifecycleManager(b.subroutines, b.operatorName, b.controllerName, mgr, b.log)
64+
if b.withConditionManagement {
65+
lm.WithConditionManagement()
66+
}
67+
if b.withSpreadingReconciles {
68+
lm.WithSpreadingReconciles()
69+
}
70+
if b.withReadOnly {
71+
lm.WithReadOnly()
72+
}
73+
return lm
74+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package builder
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"k8s.io/client-go/rest"
8+
mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager"
9+
10+
pmtesting "github.com/platform-mesh/golang-commons/controller/testSupport"
11+
"github.com/platform-mesh/golang-commons/logger"
12+
)
13+
14+
func TestNewBuilder_Defaults(t *testing.T) {
15+
log := &logger.Logger{}
16+
b := NewBuilder("op", "ctrl", nil, log)
17+
if b.operatorName != "op" {
18+
t.Errorf("expected operatorName 'op', got %s", b.operatorName)
19+
}
20+
if b.controllerName != "ctrl" {
21+
t.Errorf("expected controllerName 'ctrl', got %s", b.controllerName)
22+
}
23+
if b.withConditionManagement {
24+
t.Error("expected withConditionManagement to be false")
25+
}
26+
if b.withSpreadingReconciles {
27+
t.Error("expected withSpreadingReconciles to be false")
28+
}
29+
if b.withReadOnly {
30+
t.Error("expected withReadOnly to be false")
31+
}
32+
if b.log != log {
33+
t.Error("expected log to be set")
34+
}
35+
}
36+
37+
func TestBuilder_WithConditionManagement(t *testing.T) {
38+
b := NewBuilder("op", "ctrl", nil, &logger.Logger{})
39+
b.WithConditionManagement()
40+
if !b.withConditionManagement {
41+
t.Error("WithConditionManagement should set withConditionManagement to true")
42+
}
43+
}
44+
45+
func TestBuilder_WithSpreadingReconciles(t *testing.T) {
46+
b := NewBuilder("op", "ctrl", nil, &logger.Logger{})
47+
b.WithSpreadingReconciles()
48+
if !b.withSpreadingReconciles {
49+
t.Error("WithSpreadingReconciles should set withSpreadingReconciles to true")
50+
}
51+
}
52+
53+
func TestBuilder_WithReadOnly(t *testing.T) {
54+
b := NewBuilder("op", "ctrl", nil, &logger.Logger{})
55+
b.WithReadOnly()
56+
if !b.withReadOnly {
57+
t.Error("WithReadOnly should set withReadOnly to true")
58+
}
59+
}
60+
61+
func TestControllerRuntimeBuilder(t *testing.T) {
62+
t.Run("Minimal setup", func(t *testing.T) {
63+
b := NewBuilder("op", "ctrl", nil, &logger.Logger{})
64+
fakeClient := pmtesting.CreateFakeClient(t, &pmtesting.TestApiObject{})
65+
lm := b.BuildControllerRuntime(fakeClient)
66+
assert.NotNil(t, lm)
67+
})
68+
t.Run("All Options", func(t *testing.T) {
69+
b := NewBuilder("op", "ctrl", nil, &logger.Logger{}).WithConditionManagement().WithSpreadingReconciles()
70+
fakeClient := pmtesting.CreateFakeClient(t, &pmtesting.TestApiObject{})
71+
lm := b.BuildControllerRuntime(fakeClient)
72+
assert.NotNil(t, lm)
73+
})
74+
t.Run("ReadOnly", func(t *testing.T) {
75+
b := NewBuilder("op", "ctrl", nil, &logger.Logger{}).WithReadOnly()
76+
fakeClient := pmtesting.CreateFakeClient(t, &pmtesting.TestApiObject{})
77+
lm := b.BuildControllerRuntime(fakeClient)
78+
assert.NotNil(t, lm)
79+
})
80+
}
81+
82+
func TestMulticontrollerRuntimeBuilder(t *testing.T) {
83+
t.Run("Minimal setup", func(t *testing.T) {
84+
b := NewBuilder("op", "ctrl", nil, &logger.Logger{})
85+
cfg := &rest.Config{}
86+
provider := pmtesting.NewFakeProvider(cfg)
87+
mgr, err := mcmanager.New(cfg, provider, mcmanager.Options{})
88+
assert.NoError(t, err)
89+
lm := b.BuildMultiCluster(mgr)
90+
assert.NotNil(t, lm)
91+
})
92+
t.Run("All Options", func(t *testing.T) {
93+
b := NewBuilder("op", "ctrl", nil, &logger.Logger{}).WithConditionManagement().WithSpreadingReconciles()
94+
cfg := &rest.Config{}
95+
provider := pmtesting.NewFakeProvider(cfg)
96+
mgr, err := mcmanager.New(cfg, provider, mcmanager.Options{})
97+
assert.NoError(t, err)
98+
lm := b.BuildMultiCluster(mgr)
99+
assert.NotNil(t, lm)
100+
})
101+
t.Run("ReadOnly", func(t *testing.T) {
102+
b := NewBuilder("op", "ctrl", nil, &logger.Logger{}).WithReadOnly()
103+
cfg := &rest.Config{}
104+
provider := pmtesting.NewFakeProvider(cfg)
105+
mgr, err := mcmanager.New(cfg, provider, mcmanager.Options{})
106+
assert.NoError(t, err)
107+
lm := b.BuildMultiCluster(mgr)
108+
assert.NotNil(t, lm)
109+
})
110+
}

controller/lifecycle/controllerruntime/lifecycle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type LifecycleManager struct {
3131
prepareContextFunc api.PrepareContextFunc
3232
}
3333

34-
func NewLifecycleManager(log *logger.Logger, operatorName string, controllerName string, client client.Client, subroutines []subroutine.Subroutine) *LifecycleManager {
34+
func NewLifecycleManager(subroutines []subroutine.Subroutine, operatorName string, controllerName string, client client.Client, log *logger.Logger) *LifecycleManager {
3535
log = log.MustChildLoggerWithAttributes("operator", operatorName, "controller", controllerName)
3636
return &LifecycleManager{
3737
log: log,

controller/lifecycle/controllerruntime/lifecycle_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ func TestLifecycle(t *testing.T) {
3737
m, err := manager.New(&rest.Config{}, manager.Options{Scheme: fakeClient.Scheme()})
3838
assert.NoError(t, err)
3939

40-
lm, log := createLifecycleManager([]subroutine.Subroutine{}, fakeClient)
41-
lm = lm.WithSpreadingReconciles()
40+
log := testlogger.New()
41+
lm := NewLifecycleManager([]subroutine.Subroutine{}, "test-operator", "test-controller", fakeClient, log.Logger)
42+
lm.WithSpreadingReconciles()
4243
tr := &testReconciler{lifecycleManager: lm}
4344

4445
// Act
@@ -56,7 +57,7 @@ func TestLifecycle(t *testing.T) {
5657
assert.NoError(t, err)
5758

5859
lm, log := createLifecycleManager([]subroutine.Subroutine{}, fakeClient)
59-
lm = lm.WithSpreadingReconciles()
60+
lm.WithSpreadingReconciles()
6061
tr := &testReconciler{lifecycleManager: lm}
6162

6263
// Act
@@ -74,7 +75,8 @@ func TestLifecycle(t *testing.T) {
7475
assert.NoError(t, err)
7576

7677
lm, log := createLifecycleManager([]subroutine.Subroutine{}, fakeClient)
77-
lm.WithSpreadingReconciles().WithReadOnly()
78+
lm.WithSpreadingReconciles()
79+
lm.WithReadOnly()
7880
tr := &testReconciler{lifecycleManager: lm}
7981

8082
// Act
@@ -133,7 +135,7 @@ func TestLifecycle(t *testing.T) {
133135
_, log := createLifecycleManager([]subroutine.Subroutine{}, fakeClient)
134136

135137
// When
136-
l := NewLifecycleManager(log.Logger, "test-operator", "test-controller", fakeClient, []subroutine.Subroutine{}).WithConditionManagement()
138+
l := NewLifecycleManager([]subroutine.Subroutine{}, "test-operator", "test-controller", fakeClient, log.Logger).WithConditionManagement()
137139

138140
// Then
139141
assert.True(t, true, l.ConditionsManager() != nil)
@@ -144,7 +146,7 @@ func TestLifecycle(t *testing.T) {
144146
_, log := createLifecycleManager([]subroutine.Subroutine{}, fakeClient)
145147

146148
// When
147-
l := NewLifecycleManager(log.Logger, "test-operator", "test-controller", fakeClient, []subroutine.Subroutine{}).WithReadOnly()
149+
l := NewLifecycleManager([]subroutine.Subroutine{}, "test-operator", "test-controller", fakeClient, log.Logger).WithReadOnly()
148150

149151
// Then
150152
assert.True(t, true, l.ConditionsManager() != nil)
@@ -162,6 +164,6 @@ func (r *testReconciler) Reconcile(ctx context.Context, req controllerruntime.Re
162164

163165
func createLifecycleManager(subroutines []subroutine.Subroutine, c client.Client) (*LifecycleManager, *testlogger.TestLogger) {
164166
log := testlogger.New()
165-
mgr := NewLifecycleManager(log.Logger, "test-operator", "test-controller", c, subroutines)
167+
mgr := NewLifecycleManager(subroutines, "test-operator", "test-controller", c, log.Logger)
166168
return mgr, log
167169
}

controller/lifecycle/multicluster/lifecycle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type LifecycleManager struct {
3737
prepareContextFunc api.PrepareContextFunc
3838
}
3939

40-
func NewLifecycleManager(log *logger.Logger, operatorName string, controllerName string, mgr ClusterGetter, subroutines []subroutine.Subroutine) *LifecycleManager {
40+
func NewLifecycleManager(subroutines []subroutine.Subroutine, operatorName string, controllerName string, mgr ClusterGetter, log *logger.Logger) *LifecycleManager {
4141
log = log.MustChildLoggerWithAttributes("operator", operatorName, "controller", controllerName)
4242
return &LifecycleManager{
4343
log: log,

controller/lifecycle/multicluster/lifecycle_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func TestLifecycleManager_WithConditionManagement(t *testing.T) {
185185
_, log := createLifecycleManager([]subroutine.Subroutine{}, fakeClient)
186186

187187
// When
188-
l := NewLifecycleManager(log.Logger, "test-operator", "test-controller", clusterGetter, []subroutine.Subroutine{}).WithConditionManagement()
188+
l := NewLifecycleManager([]subroutine.Subroutine{}, "test-operator", "test-controller", clusterGetter, log.Logger).WithConditionManagement()
189189

190190
// Then
191191
assert.True(t, true, l.ConditionsManager() != nil)
@@ -202,6 +202,6 @@ func (r *testReconciler) Reconcile(ctx context.Context, req mcreconcile.Request)
202202
func createLifecycleManager(subroutines []subroutine.Subroutine, client client.Client) (*LifecycleManager, *testlogger.TestLogger) {
203203
log := testlogger.New()
204204
clusterGetter := &pmtesting.FakeManager{Client: client}
205-
m := NewLifecycleManager(log.Logger, "test-operator", "test-controller", clusterGetter, subroutines)
205+
m := NewLifecycleManager(subroutines, "test-operator", "test-controller", clusterGetter, log.Logger)
206206
return m, log
207207
}

controller/testSupport/lifecycle.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ func (l *TestLifecycleManager) PrepareContextFunc() api.PrepareContextFunc {
3737
return l.prepareContextFunc
3838
}
3939
func (l *TestLifecycleManager) Subroutines() []subroutine.Subroutine { return l.SubroutinesArr }
40-
func (l *TestLifecycleManager) WithSpreadingReconciles() *TestLifecycleManager {
40+
func (l *TestLifecycleManager) WithSpreadingReconciles() api.Lifecycle {
4141
l.spreader = &TestSpreader{ShouldReconcile: l.ShouldReconcile}
4242
return l
4343
}
44-
func (l *TestLifecycleManager) WithConditionManagement() *TestLifecycleManager {
44+
func (l *TestLifecycleManager) WithConditionManagement() api.Lifecycle {
4545
l.conditionsManager = &TestConditionManager{}
4646
return l
4747
}
48-
4948
func (l *TestLifecycleManager) WithPrepareContextFunc(prepareFunction api.PrepareContextFunc) *TestLifecycleManager {
5049
l.prepareContextFunc = prepareFunction
5150
return l

0 commit comments

Comments
 (0)