Skip to content

Commit

Permalink
Fix webhook error & exit handling
Browse files Browse the repository at this point in the history
Signed-off-by: Jaime Caamaño Ruiz <jcaamano@redhat.com>
  • Loading branch information
jcaamano committed Oct 6, 2023
1 parent f3a4030 commit 65c5719
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions go-controller/cmd/ovnkube-identity/ovnkubeidentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,31 +147,35 @@ func main() {
return err
}

startWg := &sync.WaitGroup{}
runWg := &sync.WaitGroup{}

var errorList []error
ctx, cancel := context.WithCancel(c.Context)
var errWebhook, errApprover error
if !cliCfg.disableWebhook {
startWg.Add(1)
runWg.Add(1)
go func() {
defer startWg.Done()
if err := runWebhook(c, restCfg); err != nil {
errorList = append(errorList, err)
defer runWg.Done()
if err := runWebhook(ctx, restCfg); err != nil {
errWebhook = fmt.Errorf("Error running webhook: %v", err)
cancel()
}
}()
}

if !cliCfg.disableApprover {
startWg.Add(1)
runWg.Add(1)
go func() {
defer startWg.Done()
if err := runCSRApproverManager(c.Context, c.App.Name, restCfg); err != nil {
errorList = append(errorList, err)
defer runWg.Done()
if err := runCSRApproverManager(ctx, c.App.Name, restCfg); err != nil {
errApprover = fmt.Errorf("Error running approver: %v", err)
cancel()
}
}()
}

startWg.Wait()
return errors.NewAggregate(errorList)
runWg.Wait()
cancel()
return errors.NewAggregate([]error{errWebhook, errApprover})
}

c.Flags = []cli.Flag{
Expand Down Expand Up @@ -304,7 +308,7 @@ func main() {
}
}

func runWebhook(c *cli.Context, restCfg *rest.Config) error {
func runWebhook(ctx context.Context, restCfg *rest.Config) error {
// We cannot use the default implementation of the webhook server because we need to enable SO_REUSEPORT
// on the socket to allow for two instances running at the same time (required during upgrades).
// The webhook server is set up and started in a very similar way to the default one:
Expand Down Expand Up @@ -344,7 +348,7 @@ func runWebhook(c *cli.Context, restCfg *rest.Config) error {
nodeInformer := informerFactory.Core().V1().Nodes().Informer()
informerFactory.Start(stopCh)
klog.Infof("Waiting for caches to sync")
cache.WaitForCacheSync(c.Context.Done(), nodeInformer.HasSynced)
cache.WaitForCacheSync(ctx.Done(), nodeInformer.HasSynced)

nodeLister := listers.NewNodeLister(nodeInformer.GetIndexer())
podWebhook := admission.WithCustomValidator(
Expand Down Expand Up @@ -379,7 +383,7 @@ func runWebhook(c *cli.Context, restCfg *rest.Config) error {
cfg.GetCertificate = certWatcher.GetCertificate

go func() {
if err := certWatcher.Start(c.Context); err != nil {
if err := certWatcher.Start(ctx); err != nil {
klog.Fatalf("Certificate watcher failed to start: %v", err)
}
}()
Expand All @@ -401,20 +405,19 @@ func runWebhook(c *cli.Context, restCfg *rest.Config) error {
},
}

idleWebhookConnectionsClosed := make(chan struct{})
defer func() {
klog.Infof("Waiting for the webhook server to gracefully close")
<-idleWebhookConnectionsClosed
}()
innerListener, err := l.Listen(c.Context, "tcp", net.JoinHostPort(cliCfg.host, strconv.Itoa(cliCfg.port)))
innerListener, err := l.Listen(ctx, "tcp", net.JoinHostPort(cliCfg.host, strconv.Itoa(cliCfg.port)))
if err != nil {
return fmt.Errorf("failed to create the listener: %v", err)
}
listener := tls.NewListener(innerListener, cfg)

klog.Infof("Starting the webhook server")
idleWebhookConnectionsClosed := make(chan struct{})
defer func() {
klog.Infof("Waiting for the webhook server to gracefully close")
<-idleWebhookConnectionsClosed
}()
go func() {
<-c.Context.Done()
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
Expand All @@ -423,6 +426,7 @@ func runWebhook(c *cli.Context, restCfg *rest.Config) error {
close(idleWebhookConnectionsClosed)
}()

klog.Infof("Starting the webhook server")
return srv.Serve(listener)
}

Expand Down

0 comments on commit 65c5719

Please sign in to comment.