Skip to content

Commit

Permalink
Publish resync audit events every hour (#40)
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha authored Dec 21, 2023
1 parent 1cd6c9a commit 35e1cdd
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions lib/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"
)

// Informer - informer allows you interact with the underlying informer.
type Informer interface {
// AddEventHandlerWithResyncPeriod adds an event handler to the shared informer using the
// specified resync period. Events to a single handler are delivered sequentially, but there is
// no coordination between different handlers.
AddEventHandlerWithResyncPeriod(handler cache.ResourceEventHandler, resyncPeriod time.Duration)
}

type EventCreator func(obj client.Object) (*api.Event, error)

type EventPublisher struct {
Expand Down Expand Up @@ -157,12 +165,12 @@ func (p *EventPublisher) Publish(ev *api.Event, et api.EventType) error {
}
}

func (p *EventPublisher) ForGVK(gvk schema.GroupVersionKind) cache.ResourceEventHandler {
func (p *EventPublisher) ForGVK(informer Informer, gvk schema.GroupVersionKind) {
if gvk.Version == "" || gvk.Kind == "" {
panic(fmt.Sprintf("incomplete GVK; %+v", gvk))
}

return &ResourceEventPublisher{
h := &ResourceEventPublisher{
p: p,
createEvent: func(obj client.Object) (*api.Event, error) {
r := obj.DeepCopyObject().(client.Object)
Expand All @@ -183,6 +191,7 @@ func (p *EventPublisher) ForGVK(gvk schema.GroupVersionKind) cache.ResourceEvent
return ev, nil
},
}
informer.AddEventHandlerWithResyncPeriod(h, 1*time.Hour)
}

type funcNodeLister func() ([]*core.Node, error)
Expand Down Expand Up @@ -278,7 +287,7 @@ func (p *EventPublisher) SetupWithManagerForKind(ctx context.Context, mgr manage
if err != nil {
return err
}
i.AddEventHandler(p.ForGVK(gvk))
p.ForGVK(i, gvk)
return nil
}

Expand Down Expand Up @@ -314,27 +323,12 @@ func (p *ResourceEventPublisher) OnAdd(o interface{}) {
}
}

func (p *ResourceEventPublisher) OnUpdate(oldObj, newObj interface{}) {
uOld, ok := oldObj.(client.Object)
if !ok {
return
}
func (p *ResourceEventPublisher) OnUpdate(_, newObj interface{}) {
uNew, ok := newObj.(client.Object)
if !ok {
return
}

if uOld.GetUID() == uNew.GetUID() && uOld.GetGeneration() == uNew.GetGeneration() {
if klog.V(8).Enabled() {
klog.V(8).InfoS("skipping update event",
"gvk", uNew.GetObjectKind().GroupVersionKind(),
"namespace", uNew.GetNamespace(),
"name", uNew.GetName(),
)
}
return
}

ev, err := p.createEvent(uNew)
if err != nil {
klog.V(5).InfoS("failed to create event data", "error", err)
Expand Down

0 comments on commit 35e1cdd

Please sign in to comment.