Skip to content

Commit 03768af

Browse files
committed
update
1 parent cc9b155 commit 03768af

File tree

5 files changed

+79
-14
lines changed

5 files changed

+79
-14
lines changed

pkg/controller/kubebuilderx/plan_builder.go

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,29 @@ func buildOrderedVertices(transCtx *transformContext, currentTree *ObjectTree, d
165165
workloadVertices = append(workloadVertices, vertex)
166166
}
167167
}
168+
graphOptions := func(options ObjectOptions) []model.GraphOption {
169+
opts := []model.GraphOption{
170+
inDataContext4G(),
171+
}
172+
for i := range options.Hooks {
173+
opts = append(opts, model.WithHook(options.Hooks[i]))
174+
}
175+
return opts
176+
}
168177
createNewObjects := func() {
169178
for name := range createSet {
170-
if desiredTree.childrenOptions[name].SkipToReconcile {
179+
options := desiredTree.childrenOptions[name]
180+
if options.SkipToReconcile {
171181
continue
172182
}
173-
v := model.NewObjectVertex(nil, assign(ctx, newSnapshot[name]), model.ActionCreatePtr(), inDataContext4G())
183+
v := model.NewObjectVertex(nil, assign(ctx, newSnapshot[name]), model.ActionCreatePtr(), graphOptions(options)...)
174184
findAndAppend(v)
175185
}
176186
}
177187
updateObjects := func() {
178188
for name := range updateSet {
179-
if desiredTree.childrenOptions[name].SkipToReconcile {
189+
options := desiredTree.childrenOptions[name]
190+
if options.SkipToReconcile {
180191
continue
181192
}
182193
oldObj := oldSnapshot[name]
@@ -190,21 +201,23 @@ func buildOrderedVertices(transCtx *transformContext, currentTree *ObjectTree, d
190201
var v *model.ObjectVertex
191202
subResource := desiredTree.childrenOptions[*name].SubResource
192203
if subResource != "" {
193-
v = model.NewObjectVertex(oldObj, newObj, model.ActionUpdatePtr(), inDataContext4G(), model.WithSubResource(subResource))
204+
opts := append(graphOptions(options), model.WithSubResource(subResource))
205+
v = model.NewObjectVertex(oldObj, newObj, model.ActionUpdatePtr(), opts...)
194206
} else {
195-
v = model.NewObjectVertex(oldObj, newObj, model.ActionUpdatePtr(), inDataContext4G())
207+
v = model.NewObjectVertex(oldObj, newObj, model.ActionUpdatePtr(), graphOptions(options)...)
196208
}
197209
findAndAppend(v)
198210
}
199211
}
200212
}
201-
deleteOrphanObjects := func() {
213+
deleteObjects := func() {
202214
for name := range deleteSet {
203-
if desiredTree.childrenOptions[name].SkipToReconcile {
215+
options := desiredTree.childrenOptions[name]
216+
if options.SkipToReconcile {
204217
continue
205218
}
206219
object := oldSnapshot[name]
207-
v := model.NewObjectVertex(nil, object, model.ActionDeletePtr(), inDataContext4G())
220+
v := model.NewObjectVertex(nil, object, model.ActionDeletePtr(), graphOptions(options)...)
208221
findAndAppend(v)
209222
}
210223
}
@@ -218,7 +231,7 @@ func buildOrderedVertices(transCtx *transformContext, currentTree *ObjectTree, d
218231
// objects to be updated
219232
updateObjects()
220233
// objects to be deleted
221-
deleteOrphanObjects()
234+
deleteObjects()
222235
// handle object dependencies
223236
handleDependencies()
224237
return vertices
@@ -246,6 +259,9 @@ func (b *PlanBuilder) defaultWalkFunc(v graph.Vertex) error {
246259
if vertex.Action == nil {
247260
return errors.New("vertex action can't be nil")
248261
}
262+
if err := b.callHooks(vertex); err != nil {
263+
return fmt.Errorf("vertex call hooks failed: %v, obj: %s, action: %v", err, vertex.Obj.GetName(), *vertex.Action)
264+
}
249265
ctx := b.transCtx.ctx
250266
switch *vertex.Action {
251267
case model.CREATE:
@@ -262,6 +278,15 @@ func (b *PlanBuilder) defaultWalkFunc(v graph.Vertex) error {
262278
return nil
263279
}
264280

281+
func (b *PlanBuilder) callHooks(vertex *model.ObjectVertex) error {
282+
for i := range vertex.Hooks {
283+
if err := vertex.Hooks[i](vertex.Obj); err != nil {
284+
return err
285+
}
286+
}
287+
return nil
288+
}
289+
265290
func (b *PlanBuilder) createObject(ctx context.Context, vertex *model.ObjectVertex) error {
266291
err := b.cli.Create(ctx, vertex.Obj, clientOption(vertex))
267292
if err != nil && !apierrors.IsAlreadyExists(err) {

pkg/controller/kubebuilderx/reconciler.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ type ObjectOptions struct {
4343

4444
// if true, the object should not be reconciled
4545
SkipToReconcile bool
46+
47+
// hooks are called before the object is manipulated
48+
Hooks []func(client.Object) error
4649
}
4750

4851
type WithSubResource string
@@ -57,6 +60,12 @@ func (o SkipToReconcile) ApplyToObject(opts *ObjectOptions) {
5760
opts.SkipToReconcile = bool(o)
5861
}
5962

63+
type WithHook func(client.Object) error
64+
65+
func (o WithHook) ApplyToObject(opts *ObjectOptions) {
66+
opts.Hooks = append(opts.Hooks, o)
67+
}
68+
6069
type ObjectTree struct {
6170
// TODO(free6om): should find a better place to hold these two params?
6271
context.Context
@@ -252,8 +261,20 @@ func (t *ObjectTree) Delete(objects ...client.Object) error {
252261
return nil
253262
}
254263

255-
func (t *ObjectTree) DeleteSecondaryObjects() {
256-
t.children = make(model.ObjectSnapshot)
264+
func (t *ObjectTree) DeleteWithOption(object client.Object, options ...ObjectOption) error {
265+
name, err := model.GetGVKName(object)
266+
if err != nil {
267+
return err
268+
}
269+
delete(t.children, *name)
270+
if len(options) > 0 {
271+
option := ObjectOptions{}
272+
for _, opt := range options {
273+
opt.ApplyToObject(&option)
274+
}
275+
t.childrenOptions[*name] = option
276+
}
277+
return nil
257278
}
258279

259280
func (t *ObjectTree) SetFinalizer(finalizer string) {

pkg/controller/kubebuilderx/reconciler_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ var _ = Describe("reconciler test", func() {
6767
Expect(tree.Update(obj0Update)).Should(Succeed())
6868
Expect(tree.List(&corev1.Pod{})[0]).Should(Equal(obj0Update))
6969
Expect(tree.GetSecondaryObjects()).Should(HaveLen(1))
70-
tree.DeleteSecondaryObjects()
71-
Expect(tree.GetSecondaryObjects()).Should(HaveLen(0))
7270

7371
By("DeepCopy")
7472
tree.SetRoot(root)

pkg/controller/model/graph_options.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1919

2020
package model
2121

22-
import "sigs.k8s.io/controller-runtime/pkg/client"
22+
import (
23+
"sigs.k8s.io/controller-runtime/pkg/client"
24+
)
2325

2426
type GraphOptions struct {
2527
replaceIfExisting bool
2628
haveDifferentTypeWith bool
2729
clientOpt any
2830
propagationPolicy client.PropagationPolicy
2931
subResource string
32+
hooks []func(client.Object) error
3033
}
3134

3235
type GraphOption interface {
@@ -99,3 +102,19 @@ func WithSubResource(subResource string) GraphOption {
99102
subResource: subResource,
100103
}
101104
}
105+
106+
type hookOption struct {
107+
hook func(object client.Object) error
108+
}
109+
110+
var _ GraphOption = &hookOption{}
111+
112+
func (o *hookOption) ApplyTo(opts *GraphOptions) {
113+
opts.hooks = append(opts.hooks, o.hook)
114+
}
115+
116+
func WithHook(hook func(client.Object) error) GraphOption {
117+
return &hookOption{
118+
hook: hook,
119+
}
120+
}

pkg/controller/model/transform_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type ObjectVertex struct {
7272
SubResource string
7373
ClientOpt any
7474
PropagationPolicy client.PropagationPolicy
75+
Hooks []func(client.Object) error
7576
}
7677

7778
func (v *ObjectVertex) String() string {
@@ -92,6 +93,7 @@ func NewObjectVertex(oldObj, newObj client.Object, action *Action, opts ...Graph
9293
Action: action,
9394
SubResource: graphOpts.subResource,
9495
ClientOpt: graphOpts.clientOpt,
96+
Hooks: graphOpts.hooks,
9597
}
9698
}
9799

0 commit comments

Comments
 (0)