Skip to content
Open
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
11 changes: 9 additions & 2 deletions operator/informer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
ResourceActionCreate = ResourceAction("CREATE")
ResourceActionUpdate = ResourceAction("UPDATE")
ResourceActionDelete = ResourceAction("DELETE")
ResourceActionResync = ResourceAction("RESYNC")
)

// ErrNilObject indicates that a provided resource.Object is nil, and cannot be processed
Expand Down Expand Up @@ -483,12 +484,18 @@ func (c *InformerController) informerUpdateFunc(resourceKind string) func(contex
// Generate the unique key for this object
retryKey := c.keyForReconcilerEvent(resourceKind, index, newObj)

// Convert to a resync action if the old and new RV are the same
action := ResourceActionUpdate
if oldObj != nil && oldObj.GetResourceVersion() == newObj.GetResourceVersion() {
action = ResourceActionResync
}

// Dequeue retries according to the RetryDequeuePolicy
c.dequeueIfRequired(retryKey, newObj, ResourceActionUpdate)
c.dequeueIfRequired(retryKey, newObj, action)

// Do the reconciler's update, check for error or a response with a specified RetryAfter
req := ReconcileRequest{
Action: ReconcileActionUpdated,
Action: ReconcileActionFromResourceAction(action),
Object: newObj,
}
c.doReconcile(ctx, reconciler, req, retryKey)
Expand Down
70 changes: 70 additions & 0 deletions operator/informer_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,76 @@ func TestInformerController_Run_WithWatcherAndReconciler(t *testing.T) {
})
}

func TestInformerController_Run_HandleEvents(t *testing.T) {
t.Run("reconciler update", func(t *testing.T) {
kind := "foo"
inf := &testInformer{}
c := NewInformerController(InformerControllerConfig{})
c.RetryPolicy = func(err error, attempt int) (bool, time.Duration) {
if attempt > 1 {
return false, 0
}
return true, time.Millisecond * 50
}
c.retryTickerInterval = time.Millisecond * 50
wg := sync.WaitGroup{}
wg.Add(1)
reconcileAction := ReconcileActionUnknown
c.AddReconciler(&SimpleReconciler{
ReconcileFunc: func(ctx context.Context, request ReconcileRequest) (ReconcileResult, error) {
reconcileAction = request.Action
wg.Done()
return ReconcileResult{}, nil
},
}, kind)
c.AddInformer(inf, kind)

// Run
ctx, cancel := context.WithCancel(context.Background())
go c.Run(ctx)
oldObj := emptyObject.Copy()
newObj := emptyObject.Copy()
newObj.SetResourceVersion("321") // Different RV
inf.FireUpdate(context.Background(), oldObj, newObj)
wg.Wait()
cancel()
assert.Equal(t, ReconcileActionUpdated, reconcileAction)
})
t.Run("reconciler cache resync", func(t *testing.T) {
kind := "foo"
inf := &testInformer{}
c := NewInformerController(InformerControllerConfig{})
c.RetryPolicy = func(err error, attempt int) (bool, time.Duration) {
if attempt > 1 {
return false, 0
}
return true, time.Millisecond * 50
}
c.retryTickerInterval = time.Millisecond * 50
wg := sync.WaitGroup{}
wg.Add(1)
reconcileAction := ReconcileActionUnknown
c.AddReconciler(&SimpleReconciler{
ReconcileFunc: func(ctx context.Context, request ReconcileRequest) (ReconcileResult, error) {
reconcileAction = request.Action
wg.Done()
return ReconcileResult{}, nil
},
}, kind)
c.AddInformer(inf, kind)

// Run
ctx, cancel := context.WithCancel(context.Background())
go c.Run(ctx)
oldObj := emptyObject.Copy()
newObj := emptyObject.Copy()
inf.FireUpdate(context.Background(), oldObj, newObj)
wg.Wait()
cancel()
assert.Equal(t, ReconcileActionResynced, reconcileAction)
})
}

func TestInformerController_Run_BackoffRetry(t *testing.T) {
// The backoff retry test needs to take at least 16 seconds to run properly, so it's isolated to its own function
// to avoid the often-used default of a 30-second-timeout on tests affecting other retry tests which take a few seconds each to run
Expand Down
4 changes: 4 additions & 0 deletions operator/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func ReconcileActionFromResourceAction(action ResourceAction) ReconcileAction {
return ReconcileActionUpdated
case ResourceActionDelete:
return ReconcileActionDeleted
case ResourceActionResync:
return ReconcileActionResynced
default:
return ReconcileActionUnknown
}
Expand All @@ -112,6 +114,8 @@ func ResourceActionFromReconcileAction(action ReconcileAction) ResourceAction {
return ResourceActionUpdate
case ReconcileActionDeleted:
return ResourceActionDelete
case ReconcileActionResynced:
return ResourceActionResync
default:
return ResourceAction("")
}
Expand Down
Loading