Skip to content

Commit a527f23

Browse files
authored
Merge pull request #174 from hasbro17/haseeb/return-delete-events
informer: save deleted object and pass delete event to handler
2 parents dcd89a8 + 22e16cc commit a527f23

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

example/memcached-operator/handler.go.tmpl

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ func (h *Handler) Handle(ctx types.Context, event types.Event) error {
2929
case *v1alpha1.Memcached:
3030
memcached := o
3131

32+
// Ignore the delete event since the garbage collector will clean up all secondary resources for the CR
33+
// All secondary resources must have the CR set as their OwnerReference for this to be the case
34+
if event.Deleted {
35+
return nil
36+
}
37+
3238
// Create the deployment if it doesn't exist
3339
dep := deploymentForMemcached(memcached)
3440
err := action.Create(dep)

pkg/sdk/informer/informer.go

+7
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ type informer struct {
3939
queue workqueue.RateLimitingInterface
4040
namespace string
4141
context context.Context
42+
deletedObjects map[string]interface{}
4243
}
4344

4445
func New(resourcePluralName, namespace string, resourceClient dynamic.ResourceInterface, resyncPeriod int) Informer {
4546
i := &informer{
4647
resourcePluralName: resourcePluralName,
4748
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), resourcePluralName),
4849
namespace: namespace,
50+
deletedObjects: map[string]interface{}{},
4951
}
5052

5153
resyncDuration := time.Duration(resyncPeriod) * time.Second
@@ -104,6 +106,11 @@ func (i *informer) handleDeleteResourceEvent(obj interface{}) {
104106
if err != nil {
105107
panic(err)
106108
}
109+
110+
// TODO: Revisit the need for passing delete events to the handler
111+
// Save the last known state for the deleted object
112+
i.deletedObjects[key] = obj.(*unstructured.Unstructured).DeepCopy()
113+
107114
i.queue.Add(key)
108115
}
109116

pkg/sdk/informer/sync.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,21 @@ func (i *informer) processNextItem() bool {
5757
return true
5858
}
5959

60-
// sync creates the event for the object, sends it to the handler, and processes the resulting actions
60+
// sync creates the event for the object and sends it to the handler
6161
func (i *informer) sync(key string) error {
6262
obj, exists, err := i.sharedIndexInformer.GetIndexer().GetByKey(key)
6363
if err != nil {
6464
return err
6565
}
6666
if !exists {
6767
logrus.Infof("Object (%s) is deleted", key)
68-
return nil
68+
// Lookup the last saved state for the deleted object
69+
_, ok := i.deletedObjects[key]
70+
if !ok {
71+
logrus.Errorf("No last known state found for deleted object (%s)", key)
72+
return nil
73+
}
74+
obj = i.deletedObjects[key]
6975
}
7076

7177
unstructObj := obj.(*unstructured.Unstructured).DeepCopy()
@@ -78,7 +84,11 @@ func (i *informer) sync(key string) error {
7884

7985
sdkCtx := sdkTypes.Context{Context: i.context}
8086
// TODO: Add option to prevent multiple informers from invoking Handle() concurrently?
81-
return sdkHandler.RegisteredHandler.Handle(sdkCtx, event)
87+
err = sdkHandler.RegisteredHandler.Handle(sdkCtx, event)
88+
if !exists && err == nil {
89+
delete(i.deletedObjects, key)
90+
}
91+
return err
8292
}
8393

8494
// handleErr checks if an error happened and makes sure we will retry later.

0 commit comments

Comments
 (0)