-
Notifications
You must be signed in to change notification settings - Fork 43
/
main.go
79 lines (65 loc) · 1.55 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"sync"
"github.com/cybertec-postgresql/vip-manager/checker"
"github.com/cybertec-postgresql/vip-manager/ipmanager"
"github.com/cybertec-postgresql/vip-manager/vipconfig"
"go.uber.org/zap"
)
var (
// vip-manager version definition
version = "master"
commit = "none"
date = "unknown"
)
var log *zap.SugaredLogger = zap.L().Sugar()
func main() {
if (len(os.Args) > 1) && (os.Args[1] == "--version") {
fmt.Printf("version: %s\n", version)
fmt.Printf("commit: %s\n", commit)
fmt.Printf("date: %s\n", date)
return
}
conf, err := vipconfig.NewConfig()
if err != nil {
log.Fatal(err)
}
log = conf.Logger.Sugar()
defer func() { _ = conf.Logger.Sync() }()
lc, err := checker.NewLeaderChecker(conf)
if err != nil {
log.Fatalf("Failed to initialize leader checker: %s", err)
}
states := make(chan bool)
manager, err := ipmanager.NewIPManager(conf, states)
if err != nil {
log.Fatalf("Problems with generating the virtual ip manager: %s", err)
}
mainCtx, cancel := context.WithCancel(context.Background())
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
log.Info("Received exit signal")
cancel()
}()
var wg sync.WaitGroup
wg.Add(1)
go func() {
err := lc.GetChangeNotificationStream(mainCtx, states)
if err != nil && err != context.Canceled {
log.Fatal("Leader checker returned the following error: %s", zap.Error(err))
}
wg.Done()
}()
wg.Add(1)
go func() {
manager.SyncStates(mainCtx, states)
wg.Done()
}()
wg.Wait()
}