-
Notifications
You must be signed in to change notification settings - Fork 2
/
graceful.go
77 lines (62 loc) · 1.97 KB
/
graceful.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package runnable
import (
"context"
"sync"
"github.com/go-logr/logr"
ctrl "sigs.k8s.io/controller-runtime"
)
var log = ctrl.Log.WithName("graceful-runnable")
// RunCall starts a component.
type RunCall func(context.Context) error
// StopCall stops a component.
type StopCall func() error
// Graceful implements the Runnable interface and handles graceful shutdown of
// the component. This is useful for components that don't stop immediately.
type Graceful struct {
// Run starts the component. It's called in a goroutine and can be
// blocking.
run RunCall
// Stop stops the running component.
stop StopCall
// RequireLeaderElection decides if the runnable requires leader election
// before running.
requireLeaderElection bool
// wg is the WaitGroup for handling graceful stop.
wg *sync.WaitGroup
// log is the logger.
log logr.Logger
}
// NewGraceful creates a new graceful shutdown runnable. logger is optional.
// Pass nil for the default logger.
func NewGraceful(run RunCall, stop StopCall, requireLeaderElection bool, wg *sync.WaitGroup, logger logr.Logger) *Graceful {
// Use the default package logger if not provided.
if logger == nil {
logger = log
}
return &Graceful{
run: run,
stop: stop,
requireLeaderElection: requireLeaderElection,
wg: wg,
log: logger,
}
}
// Start implements the Runnable interface which enables the component to be
// managed by the controller manager.
func (g *Graceful) Start(ctx context.Context) error {
// Handle stop in a goroutine.
go func() {
defer g.wg.Done()
<-ctx.Done()
g.log.Info("stopping gracefully")
if err := g.stop(); err != nil {
g.log.Error(err, "failed to stop gracefully")
}
}()
return g.run(ctx)
}
// NeedLeaderElection implements the LeaderElectionRunnable interface, which
// helps the controller manager decide when to start the component.
func (g *Graceful) NeedLeaderElection() bool {
return g.requireLeaderElection
}