Skip to content

Commit

Permalink
Move webhook setup before manager start
Browse files Browse the repository at this point in the history
According to
kubernetes-sigs/controller-runtime#1148,
there's a race condition between manager start and webhook register.
This commit moved webhook setup before manager start.

Tested on GKE cluster. The nil pointer error was not detected for 20
times.
  • Loading branch information
yiqigao217 committed Sep 9, 2020
1 parent a5b2c06 commit 619d6e0
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions incubator/hnc/cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,32 +153,9 @@ func main() {
os.Exit(1)
}

go startControllers(mgr, setupFinished)

setupLog.Info("Starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}

func startControllers(mgr ctrl.Manager, setupFinished chan struct{}) {
setupLog.Info("Waiting for certificate generation to complete")
// Block until the setup finishes.
<-setupFinished

if testLog {
stats.StartLoggingActivity()
}

// Create all reconciling controllers
// Register webhooks before manager start to avoid potential race conditions.
// See https://github.com/kubernetes-sigs/controller-runtime/issues/1148.
f := forest.NewForest()
setupLog.Info("Creating controllers", "maxReconciles", maxReconciles)
removeOldCRDVersion := true
if err := reconcilers.Create(mgr, f, maxReconciles, removeOldCRDVersion); err != nil {
setupLog.Error(err, "cannot create controllers")
os.Exit(1)
}

// Create all validating admission controllers.
if !novalidation {
Expand All @@ -202,5 +179,31 @@ func startControllers(mgr ctrl.Manager, setupFinished chan struct{}) {
os.Exit(1)
}

go startControllers(mgr, f, setupFinished)

setupLog.Info("Starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}

func startControllers(mgr ctrl.Manager, f *forest.Forest, setupFinished chan struct{}) {
setupLog.Info("Waiting for certificate generation to complete")
// Block until the setup finishes.
<-setupFinished

if testLog {
stats.StartLoggingActivity()
}

// Create all reconciling controllers
setupLog.Info("Creating controllers", "maxReconciles", maxReconciles)
removeOldCRDVersion := true
if err := reconcilers.Create(mgr, f, maxReconciles, removeOldCRDVersion); err != nil {
setupLog.Error(err, "cannot create controllers")
os.Exit(1)
}

setupLog.Info("All controllers started; setup complete")
}

0 comments on commit 619d6e0

Please sign in to comment.