Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(informer-manager): wait for cache sync before registering event handlers #163

Merged
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
23 changes: 15 additions & 8 deletions pkg/util/informermanager/informermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"context"
"fmt"
"sync"
"time"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -134,7 +135,7 @@
return
}

err, needReenqueue := m.processFTC(ctx, ftc)
err, needReenqueue, delay := m.processFTC(ctx, ftc)
if err != nil {
if needReenqueue {
logger.Error(err, "Failed to process FederatedTypeConfig, will retry")
Expand All @@ -148,14 +149,14 @@

m.queue.Forget(key)
if needReenqueue {
m.queue.Add(key)
m.queue.AddAfter(key, delay)
}
}

func (m *informerManager) processFTC(
ctx context.Context,
ftc *fedcorev1a1.FederatedTypeConfig,
) (err error, needReenqueue bool) {
) (err error, needReenqueue bool, reenqueueDelay time.Duration) {

Check failure on line 159 in pkg/util/informermanager/informermanager.go

View workflow job for this annotation

GitHub Actions / lint

ST1008: error should be returned as the last argument (stylecheck)
m.lock.Lock()
defer m.lock.Unlock()

Expand All @@ -176,14 +177,14 @@
// time and we missed processing the deletion. We simply process the ftc deletion and reenqueue. Note:
// updating of ftc source types, however, is still not a supported use case.
err := m.processFTCDeletionUnlocked(ctx, ftcName)
return err, true
return err, true, 0
}

informer = m.informers[ftcName]
} else {
if err := m.gvkMapping.Add(ftcName, gvk); err != nil {
// There must be another ftc with the same source type GVK.
return fmt.Errorf("source type is already referenced by another FederatedTypeConfig: %w", err), false
return fmt.Errorf("source type is already referenced by another FederatedTypeConfig: %w", err), false, 0
}

logger.V(2).Info("Starting new informer for FederatedTypeConfig")
Expand All @@ -206,6 +207,11 @@
m.lastAppliedFTCsCache[ftcName] = map[*EventHandlerGenerator]*fedcorev1a1.FederatedTypeConfig{}
}

if !informer.Informer().HasSynced() {
logger.V(3).Info("Informer for FederatedTypeConfig not synced, will not register event handlers yet")
return nil, true, 100 * time.Millisecond
}

registrations := m.eventHandlerRegistrations[ftcName]
lastAppliedFTCs := m.lastAppliedFTCsCache[ftcName]

Expand All @@ -218,7 +224,7 @@

if oldRegistration := registrations[generator]; oldRegistration != nil {
if err := informer.Informer().RemoveEventHandler(oldRegistration); err != nil {
return fmt.Errorf("failed to unregister event handler: %w", err), true
return fmt.Errorf("failed to unregister event handler: %w", err), true, 0
}
delete(registrations, generator)
}
Expand All @@ -227,15 +233,15 @@
if handler := generator.Generator(ftc); handler != nil {
newRegistration, err := informer.Informer().AddEventHandler(handler)
if err != nil {
return fmt.Errorf("failed to register event handler: %w", err), true
return fmt.Errorf("failed to register event handler: %w", err), true, 0
}
registrations[generator] = newRegistration
}

lastAppliedFTCs[generator] = ftc
}

return nil, false
return nil, false, 0
}

func (m *informerManager) processFTCDeletion(ctx context.Context, ftcName string) error {
Expand Down Expand Up @@ -342,6 +348,7 @@
}

go wait.UntilWithContext(ctx, m.worker, 0)

go func() {
<-ctx.Done()

Expand Down
Loading