Skip to content
This repository has been archived by the owner on Mar 17, 2023. It is now read-only.

Update predicate #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ func NewGeneric(config *Config, queue workqueue.RateLimitingInterface, workers i
return nil, errors.Errorf("controller for GVK %s should have registered an informer for that GVK", descr.Gvk)
}
inf.AddEventHandler(&GenericHandler{
Logger: controllerLogger,
WorkQueue: queueGvk,
ZapNameField: logz.ControllerName,
Logger: controllerLogger,
WorkQueue: queueGvk,
IsInterestingUpdate: descr.IsInterestingUpdate,
ZapNameField: logz.ControllerName,
})

controllers[descr.Gvk] = constructed.Interface
Expand Down
12 changes: 9 additions & 3 deletions generic_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@ import (
"github.com/atlassian/ctrl/logz"
"go.uber.org/zap"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
)

// This handler assumes that the Logger already has the obj_gk/ctrl_gk field set.
type GenericHandler struct {
Logger *zap.Logger
WorkQueue WorkQueueProducer
ZapNameField ZapNameField
Logger *zap.Logger
WorkQueue WorkQueueProducer
// IsInterestingUpdate is an optional predicate that is consulted before enqueuing an object on update event.
IsInterestingUpdate func(oldObj, newObj runtime.Object) bool
ZapNameField ZapNameField
}

func (g *GenericHandler) OnAdd(obj interface{}) {
g.add(obj.(meta_v1.Object), "added")
}

func (g *GenericHandler) OnUpdate(oldObj, newObj interface{}) {
if g.IsInterestingUpdate != nil && !g.IsInterestingUpdate(oldObj.(runtime.Object), newObj.(runtime.Object)) {
return
}
g.add(newObj.(meta_v1.Object), "updated")
}

Expand Down
2 changes: 2 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type ZapNameField func(name string) zap.Field
type Descriptor struct {
// Group Version Kind of objects a controller can process.
Gvk schema.GroupVersionKind
// IsInterestingUpdate is an optional predicate that is consulted before enqueuing an object on update event.
IsInterestingUpdate func(oldObj, newObj runtime.Object) bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Interesting" is fun, but I wonder if maybe something like ShouldUpdate() is more descriptive?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was going to comment the exact same thing before I saw your comment on another line 👍

}

type Server interface {
Expand Down