Skip to content

Commit

Permalink
Delay informer start after controller.
Browse files Browse the repository at this point in the history
Starting informer before controller will cause resync period set
in the informer not respected, this commit makes EnableInjectionOrDie
to return informers so that the callers can delay the start of the
informer.
  • Loading branch information
Jimmy Lin committed Sep 29, 2020
1 parent 1e373a9 commit 99f7df5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
25 changes: 11 additions & 14 deletions injection/sharedmain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func GetLeaderElectionConfig(ctx context.Context) (*leaderelection.Config, error

// EnableInjectionOrDie enables Knative Injection and starts the informers.
// Both Context and Config are optional.
func EnableInjectionOrDie(ctx context.Context, cfg *rest.Config) context.Context {
func EnableInjectionOrDie(ctx context.Context, cfg *rest.Config) (context.Context, []controller.Informer) {
if ctx == nil {
ctx = signals.NewContext()
}
Expand All @@ -146,18 +146,7 @@ func EnableInjectionOrDie(ctx context.Context, cfg *rest.Config) context.Context
}
ctx = injection.WithConfig(ctx, cfg)

ctx, informers := injection.Default.SetupInformers(ctx, cfg)

// Start the injection clients and informers.
logging.FromContext(ctx).Info("Starting informers...")
go func(ctx context.Context) {
if err := controller.StartInformers(ctx.Done(), informers...); err != nil {
logging.FromContext(ctx).Fatalw("Failed to start informers", zap.Error(err))
}
<-ctx.Done()
}(ctx)

return ctx
return injection.Default.SetupInformers(ctx, cfg)
}

// Main runs the generic main flow with a new context.
Expand Down Expand Up @@ -225,7 +214,7 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto
cfg.Burst = len(ctors) * rest.DefaultBurst
}

ctx = EnableInjectionOrDie(ctx, cfg)
ctx, informers := EnableInjectionOrDie(ctx, cfg)

logger, atomicLevel := SetupLoggerOrDie(ctx, component)
defer flush(logger)
Expand Down Expand Up @@ -279,6 +268,14 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto
})
}

// Start the injection clients and informers.
logging.FromContext(ctx).Info("Starting informers...")
go func(ctx context.Context) {
if err := controller.StartInformers(ctx.Done(), informers...); err != nil {
logging.FromContext(ctx).Fatalw("Failed to start informers", zap.Error(err))
}
<-ctx.Done()
}(ctx)
// Wait for webhook informers to sync.
if wh != nil {
wh.InformersHaveSynced()
Expand Down
7 changes: 5 additions & 2 deletions leaderelection/chaosduck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
kubeclient "knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/controller"
"knative.dev/pkg/injection/sharedmain"
"knative.dev/pkg/kflag"
"knative.dev/pkg/signals"
Expand Down Expand Up @@ -117,8 +118,10 @@ func quack(ctx context.Context, kc kubernetes.Interface, component string, leade

func main() {
ctx := signals.NewContext()
ctx = sharedmain.EnableInjectionOrDie(ctx, nil)

ctx, informers := sharedmain.EnableInjectionOrDie(ctx, nil)
if err := controller.StartInformers(ctx.Done(), informers...); err != nil {
log.Fatalf("Failed to start informers %v", err)
}
kc := kubeclient.Get(ctx)

// Until we are shutdown, build up an index of components and kill
Expand Down

0 comments on commit 99f7df5

Please sign in to comment.