Skip to content

Commit

Permalink
fix: handle normal teardown procedure
Browse files Browse the repository at this point in the history
Signed-off-by: Zespre Chang <zespre.chang@suse.com>
  • Loading branch information
starbops committed Feb 5, 2024
1 parent 2292a53 commit 3b2e500
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
37 changes: 32 additions & 5 deletions cmd/agent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"

"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -33,10 +34,36 @@ func run(ctx context.Context, options *config.AgentOptions) error {
return agent.Run(egctx)
})

eg.Go(func() error {
<-egctx.Done()
return s.Stop(egctx)
})
errCh := cleanup(egctx, s)

if err := eg.Wait(); err != nil {
return err
}

// Return cleanup error message if any
if err := <-errCh; err != nil {
return err
}

logrus.Info("finished clean")

return nil
}

func cleanup(ctx context.Context, srv *server.HTTPServer) <-chan error {
errCh := make(chan error)

go func() {
defer close(errCh)
<-ctx.Done()

if errors.Is(ctx.Err(), context.Canceled) {
errCh <- srv.Stop(ctx)
return
}

errCh <- nil
}()

return eg.Wait()
return errCh
}
39 changes: 33 additions & 6 deletions cmd/controller/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"log"

"github.com/rancher/wrangler/pkg/leader"
Expand Down Expand Up @@ -71,11 +72,6 @@ func run(options *config.ControllerOptions) error {
return s.Run()
})

eg.Go(func() error {
<-egctx.Done()
return s.Stop(egctx)
})

eg.Go(func() error {
if noLeaderElection {
callback(egctx)
Expand All @@ -85,5 +81,36 @@ func run(options *config.ControllerOptions) error {
return nil
})

return eg.Wait()
errCh := cleanup(egctx, s)

if err := eg.Wait(); err != nil {
return err
}

// Return cleanup error message if any
if err := <-errCh; err != nil {
return err
}

logrus.Info("finished clean")

return nil
}

func cleanup(ctx context.Context, srv *server.HTTPServer) <-chan error {
errCh := make(chan error)

go func() {
defer close(errCh)
<-ctx.Done()

if errors.Is(ctx.Err(), context.Canceled) {
errCh <- srv.Stop(ctx)
return
}

errCh <- nil
}()

return errCh
}

0 comments on commit 3b2e500

Please sign in to comment.