Skip to content

Commit 6f8ab68

Browse files
authored
chore: refactor kubebuilderx Do to a single reconciler (#10153)
1 parent 769e4ce commit 6f8ab68

2 files changed

Lines changed: 31 additions & 24 deletions

File tree

pkg/controller/kubebuilderx/controller.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type Controller interface {
5050
Prepare(TreeLoader) Controller
5151

5252
// Do is the computation phase. It contains the business logic and modifies the object tree.
53-
Do(...Reconciler) Controller
53+
Do(Reconciler) Controller
5454

5555
// Commit is the plan execution phase. It computes the difference between
5656
// the old and new object trees, and applies the difference to Kubernetes.
@@ -88,7 +88,7 @@ func (c *controller) Prepare(reader TreeLoader) Controller {
8888
return c
8989
}
9090

91-
func (c *controller) Do(reconcilers ...Reconciler) Controller {
91+
func (c *controller) Do(reconciler Reconciler) Controller {
9292
if c.err != nil {
9393
return c
9494
}
@@ -99,11 +99,6 @@ func (c *controller) Do(reconcilers ...Reconciler) Controller {
9999
if c.res.Next != cntn {
100100
return c
101101
}
102-
if len(reconcilers) == 0 {
103-
return c
104-
}
105-
106-
reconciler := reconcilers[0]
107102
switch result := reconciler.PreCondition(c.tree); {
108103
case result.Err != nil:
109104
c.err = result.Err
@@ -112,8 +107,7 @@ func (c *controller) Do(reconcilers ...Reconciler) Controller {
112107
return c
113108
}
114109
c.res, c.err = reconciler.Reconcile(c.tree)
115-
116-
return c.Do(reconcilers[1:]...)
110+
return c
117111
}
118112

119113
func (c *controller) Commit() (ctrl.Result, error) {

pkg/controller/kubebuilderx/controller_test.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,64 +49,77 @@ var _ = Describe("controller test", func() {
4949
cli := fake.NewFakeClient()
5050
req := ctrl.Request{}
5151
logger := log.FromContext(ctx).WithValues("InstanceSet", "test")
52-
var controller Controller
52+
var ctrlr Controller
5353
tree := NewObjectTree()
5454

5555
By("Load tree and reconcile it with no error")
56-
controller = NewController(ctx, cli, req, nil, logger)
57-
res, err := controller.Prepare(&dummyLoader{tree: tree}).Do(&dummyReconciler{res: Commit}).Commit()
56+
ctrlr = NewController(ctx, cli, req, nil, logger)
57+
res, err := ctrlr.Prepare(&dummyLoader{tree: tree}).Do(&dummyReconciler{res: Commit}).Commit()
5858
Expect(err).Should(BeNil())
5959
Expect(res.Requeue).Should(BeFalse())
6060

6161
By("Load tree with error")
62-
controller = NewController(ctx, cli, req, nil, logger)
62+
ctrlr = NewController(ctx, cli, req, nil, logger)
6363
loadErr := fmt.Errorf("load tree failed")
64-
res, err = controller.Prepare(&dummyLoader{err: loadErr}).Do(&dummyReconciler{res: Commit}).Commit()
64+
res, err = ctrlr.Prepare(&dummyLoader{err: loadErr}).Do(&dummyReconciler{res: Commit}).Commit()
6565
Expect(err).Should(Equal(loadErr))
6666
Expect(res.Requeue).Should(BeFalse())
6767

6868
By("Reconcile with pre-condition error")
69-
controller = NewController(ctx, cli, req, nil, logger)
69+
ctrlr = NewController(ctx, cli, req, nil, logger)
7070
reconcileCondErr := fmt.Errorf("reconcile pre-condition failed")
71-
res, err = controller.Prepare(&dummyLoader{tree: tree}).Do(&dummyReconciler{preErr: reconcileCondErr, res: Commit}).Commit()
71+
res, err = ctrlr.Prepare(&dummyLoader{tree: tree}).Do(&dummyReconciler{preErr: reconcileCondErr, res: Commit}).Commit()
7272
Expect(err).Should(Equal(reconcileCondErr))
7373
Expect(res.Requeue).Should(BeFalse())
7474

7575
By("Reconcile with pre-condition unsatisfied")
76-
controller = NewController(ctx, cli, req, nil, logger)
76+
ctrlr = NewController(ctx, cli, req, nil, logger)
7777
root := builder.NewPodBuilder(namespace, name).GetObject()
7878
tree.SetRoot(root)
7979
Expect(cli.Create(ctx, root)).Should(Succeed())
8080
newTree := NewObjectTree()
8181
newTree.SetRoot(root)
82-
res, err = controller.Prepare(&dummyLoader{tree: newTree}).Do(&dummyReconciler{unsatisfied: true, res: Commit}).Commit()
82+
res, err = ctrlr.Prepare(&dummyLoader{tree: newTree}).Do(&dummyReconciler{unsatisfied: true, res: Commit}).Commit()
8383
Expect(err).Should(BeNil())
8484
Expect(res.Requeue).Should(BeFalse())
8585
Expect(newTree).Should(Equal(tree))
8686

87+
By("Skip current Do but continue later chained Do calls")
88+
ctrlr = NewController(ctx, cli, req, nil, logger)
89+
newTree = NewObjectTree()
90+
Expect(cli.Get(ctx, client.ObjectKeyFromObject(root), root)).Should(Succeed())
91+
newTree.SetRoot(root)
92+
ctrlr = ctrlr.Prepare(&dummyLoader{tree: newTree}).
93+
Do(&dummyReconciler{unsatisfied: true, res: Commit}).
94+
Do(&dummyReconciler{res: Continue})
95+
ctl, ok := ctrlr.(*controller)
96+
Expect(ok).Should(BeTrue())
97+
Expect(ctl.err).Should(BeNil())
98+
Expect(ctl.tree.GetSecondaryObjects()).Should(HaveLen(1))
99+
87100
By("Reconcile with error")
88-
controller = NewController(ctx, cli, req, nil, logger)
101+
ctrlr = NewController(ctx, cli, req, nil, logger)
89102
reconcileErr := fmt.Errorf("reconcile with error")
90-
res, err = controller.Prepare(&dummyLoader{tree: tree}).Do(&dummyReconciler{err: reconcileErr, res: Commit}).Commit()
103+
res, err = ctrlr.Prepare(&dummyLoader{tree: tree}).Do(&dummyReconciler{err: reconcileErr, res: Commit}).Commit()
91104
Expect(err).Should(Equal(reconcileErr))
92105
Expect(res.Requeue).Should(BeFalse())
93106

94107
By("Reconcile with Commit method")
95-
controller = NewController(ctx, cli, req, nil, logger)
108+
ctrlr = NewController(ctx, cli, req, nil, logger)
96109
newTree = NewObjectTree()
97110
Expect(cli.Get(ctx, client.ObjectKeyFromObject(root), root)).Should(Succeed())
98111
newTree.SetRoot(root)
99-
res, err = controller.Prepare(&dummyLoader{tree: newTree}).Do(&dummyReconciler{res: Commit}).Commit()
112+
res, err = ctrlr.Prepare(&dummyLoader{tree: newTree}).Do(&dummyReconciler{res: Commit}).Commit()
100113
Expect(err).Should(BeNil())
101114
Expect(res.Requeue).Should(BeFalse())
102115
Expect(newTree).Should(Equal(tree))
103116

104117
By("Reconcile with Retry method")
105-
controller = NewController(ctx, cli, req, nil, logger)
118+
ctrlr = NewController(ctx, cli, req, nil, logger)
106119
newTree = NewObjectTree()
107120
Expect(cli.Get(ctx, client.ObjectKeyFromObject(root), root)).Should(Succeed())
108121
newTree.SetRoot(root)
109-
res, err = controller.Prepare(&dummyLoader{tree: newTree}).Do(&dummyReconciler{res: RetryAfter(time.Second)}).Commit()
122+
res, err = ctrlr.Prepare(&dummyLoader{tree: newTree}).Do(&dummyReconciler{res: RetryAfter(time.Second)}).Commit()
110123
Expect(err).Should(BeNil())
111124
Expect(res.Requeue).Should(BeTrue())
112125
Expect(res.RequeueAfter).Should(Equal(time.Second))

0 commit comments

Comments
 (0)