-
Notifications
You must be signed in to change notification settings - Fork 0
/
peermanager.go
169 lines (141 loc) · 3.51 KB
/
peermanager.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package tunnelserver
import (
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
"github.com/pkg/errors"
"github.com/rancher/norman/types/set"
"github.com/rancher/rancher/pkg/remotedialer"
"github.com/rancher/rancher/pkg/settings"
"github.com/rancher/types/config"
"github.com/rancher/types/peermanager"
"github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/net"
)
func NewPeerManager(context *config.ScaledContext, dialer *remotedialer.Server) (peermanager.PeerManager, error) {
return startPeerManager(context, dialer)
}
type peerManager struct {
sync.Mutex
leader bool
ready bool
token string
urlFormat string
server *remotedialer.Server
peers map[string]bool
listeners map[chan<- peermanager.Peers]bool
}
func startPeerManager(context *config.ScaledContext, server *remotedialer.Server) (peermanager.PeerManager, error) {
tokenBytes, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token")
if os.IsNotExist(err) {
logrus.Infof("Running in single server mode, will not peer connections")
return nil, nil
} else if err != nil {
return nil, err
}
ip, err := net.ChooseHostInterface()
if err != nil {
return nil, errors.Wrap(err, "choosing interface IP")
}
logrus.Infof("Running in clustered mode with ID %s, monitoring endpoint %s/%s", ip, settings.Namespace.Get(), settings.PeerServices.Get())
server.PeerID = ip.String()
server.PeerToken = string(tokenBytes)
pm := &peerManager{
token: server.PeerToken,
urlFormat: "wss://%s/v3/connect",
server: server,
peers: map[string]bool{},
listeners: map[chan<- peermanager.Peers]bool{},
}
context.Core.Endpoints(settings.Namespace.Get()).AddHandler("peer-manager-controller", pm.syncService)
return pm, nil
}
func (p *peerManager) syncService(key string, endpoint *v1.Endpoints) error {
if endpoint == nil {
return nil
}
parts := strings.SplitN(key, "/", 2)
if len(parts) != 2 {
return nil
}
ns, name := parts[0], parts[1]
if ns != settings.Namespace.Get() {
return nil
}
for _, svc := range strings.Split(settings.PeerServices.Get(), ",") {
if name == strings.TrimSpace(svc) {
p.addRemovePeers(endpoint)
break
}
}
return nil
}
func (p *peerManager) addRemovePeers(endpoints *v1.Endpoints) {
p.Lock()
defer p.Unlock()
newSet := map[string]bool{}
ready := false
for _, subset := range endpoints.Subsets {
for _, addr := range subset.Addresses {
if addr.IP == p.server.PeerID {
ready = true
} else {
newSet[addr.IP] = true
}
}
}
toCreate, toDelete, _ := set.Diff(newSet, p.peers)
for _, ip := range toCreate {
p.server.AddPeer(fmt.Sprintf(p.urlFormat, ip), ip, p.token)
}
for _, ip := range toDelete {
p.server.RemovePeer(ip)
}
p.peers = newSet
p.ready = ready
p.notify()
}
func (p *peerManager) notify() {
peers := peermanager.Peers{
Leader: p.leader,
Ready: p.ready,
SelfID: p.server.PeerID,
}
for id := range p.peers {
peers.IDs = append(peers.IDs, id)
}
for c := range p.listeners {
c <- peers
}
}
func (p *peerManager) AddListener(c chan<- peermanager.Peers) {
p.Lock()
defer p.Unlock()
p.listeners[c] = true
}
func (p *peerManager) RemoveListener(c chan<- peermanager.Peers) {
p.Lock()
defer p.Unlock()
delete(p.listeners, c)
c <- peermanager.Peers{
SelfID: p.server.PeerID,
Leader: p.leader,
}
}
func (p *peerManager) IsLeader() bool {
p.Lock()
defer p.Unlock()
return p.leader
}
func (p *peerManager) Leader() {
p.Lock()
defer p.Unlock()
if p.leader {
return
}
p.leader = true
p.notify()
}