/
main.go
88 lines (73 loc) · 2.01 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/sourcegraph/conc"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/leaderelection"
"github.com/lcaparelli/coord/coord"
)
func main() {
// TODO: add config to parameterize
namespace := "default"
electionID := "id"
leaderPollInterval := time.Second * 10
unixSockPath := "leader.sock"
rec := coord.EventRecorderProvider{}
resLock, err := leaderelection.NewResourceLock(controllerruntime.GetConfigOrDie(), rec, leaderelection.Options{
LeaderElection: true,
LeaderElectionNamespace: namespace,
LeaderElectionID: electionID,
})
if err != nil {
panic(fmt.Errorf("creating resource lock: %v", err))
}
errChanCoord := make(chan error)
isLeaderChan := make(chan bool)
coordinator, err := coord.NewCoordinator(resLock, errChanCoord, isLeaderChan)
if err != nil {
panic(fmt.Errorf("creating coordinator: %v", err))
}
errChanReport := make(chan error)
reporter, err := coord.NewUnixSocketReporter(leaderPollInterval, unixSockPath, errChanReport, isLeaderChan, coordinator.IsLeader)
if err != nil {
panic(fmt.Errorf("creating reporter: %v", err))
}
ctx, cancel := context.WithCancel(context.Background())
var wg conc.WaitGroup
wg.Go(func() {
coordinator.Coordinate(ctx)
close(errChanCoord)
})
wg.Go(func() {
reporter.Report(ctx)
close(errChanReport)
})
wg.Go(func() {
for err := range errChanCoord {
if err != nil {
fmt.Println("coordinator err: " + err.Error())
}
}
})
wg.Go(func() {
for err := range errChanReport {
if err != nil {
fmt.Println("reporter err: " + err.Error())
}
}
})
// Set up channel on which to send signal notifications.
// We must use a buffered channel or risk missing the signal
// if we're not ready to receive when the signal is sent.
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGABRT, syscall.SIGTERM)
// Block until any signal is received.
<-c
cancel()
wg.Wait()
}