forked from cybertec-postgresql/vip-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (92 loc) · 2.07 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"context"
"fmt"
"net/netip"
// "flag"
"log"
"net"
"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"
)
var (
// vip-manager version definition
version = "2.0.0"
)
func getMask(vip netip.Addr, mask int) net.IPMask {
if mask > 0 || mask < 33 {
return net.CIDRMask(mask, 32)
}
var ip net.IP = vip.AsSlice()
return ip.DefaultMask()
}
func getNetIface(iface string) *net.Interface {
netIface, err := net.InterfaceByName(iface)
if err != nil {
log.Fatalf("Obtaining the interface raised an error: %s", err)
}
return netIface
}
func main() {
if (len(os.Args) > 1) && (os.Args[1] == "--version") {
// log.Print("version " + version)
// return nil, nil
// }
fmt.Printf("version: %s\n", version)
return
}
conf, err := vipconfig.NewConfig()
if err != nil {
log.Fatal(err)
}
lc, err := checker.NewLeaderChecker(conf)
if err != nil {
log.Fatalf("Failed to initialize leader checker: %s", err)
}
vip := netip.MustParseAddr(conf.IP)
vipMask := getMask(vip, conf.Mask)
netIface := getNetIface(conf.Iface)
states := make(chan bool)
manager, err := ipmanager.NewIPManager(
conf.HostingType,
&ipmanager.IPConfiguration{
VIP: vip,
Netmask: vipMask,
Iface: *netIface,
RetryNum: conf.RetryNum,
RetryAfter: conf.RetryAfter,
},
states,
conf.Verbose,
)
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.Print("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.Fatalf("Leader checker returned the following error: %s", err)
}
wg.Done()
}()
wg.Add(1)
go func() {
manager.SyncStates(mainCtx, states)
wg.Done()
}()
wg.Wait()
}