-
Notifications
You must be signed in to change notification settings - Fork 506
/
frontend.go
343 lines (294 loc) · 7.78 KB
/
frontend.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package frontend
import (
"crypto/tls"
"crypto/x509"
"io"
"io/ioutil"
"log"
"math"
"net"
"sync"
"time"
"github.com/autonomy/dianemo/src/initramfs/cmd/proxyd/pkg/backend"
pkgnet "github.com/autonomy/dianemo/src/initramfs/pkg/net"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
)
// ReverseProxy represents a reverse proxy server.
type ReverseProxy struct {
ConnectTimeout int
Config *tls.Config
current *backend.Backend
backends map[string]*backend.Backend
mux *sync.Mutex
}
// NewReverseProxy initializes a ReverseProxy.
func NewReverseProxy() (r *ReverseProxy, err error) {
config, err := tlsConfig()
if err != nil {
return
}
r = &ReverseProxy{
ConnectTimeout: 100,
Config: config,
mux: &sync.Mutex{},
backends: map[string]*backend.Backend{},
}
return r, nil
}
// Listen starts the server on the specified address.
func (r *ReverseProxy) Listen(address string) (err error) {
l, err := net.Listen("tcp", address)
if err != nil {
return err
}
log.Printf("listening on %v", l.Addr())
for {
conn, err := l.Accept()
if err != nil {
if e, ok := err.(net.Error); ok {
if e.Temporary() {
continue
}
}
}
go r.proxyConnection(conn)
}
}
// AddBackend adds a backend.
func (r *ReverseProxy) AddBackend(uid, addr string) {
r.mux.Lock()
defer r.mux.Unlock()
r.backends[uid] = &backend.Backend{UID: uid, Addr: addr, Connections: 0}
r.setCurrent()
}
// DeleteBackend deletes a backend.
func (r *ReverseProxy) DeleteBackend(uid string) (deleted bool) {
r.mux.Lock()
defer r.mux.Unlock()
if _, ok := r.backends[uid]; ok {
delete(r.backends, uid)
deleted = true
}
r.setCurrent()
return deleted
}
// GetBackend gets a backend based on least connections algorithm.
func (r *ReverseProxy) GetBackend() (backend *backend.Backend) {
return r.current
}
// IncrementBackend increments the connections count of a backend. nolint: dupl
func (r *ReverseProxy) IncrementBackend(uid string) {
r.mux.Lock()
defer r.mux.Unlock()
if _, ok := r.backends[uid]; !ok {
return
}
r.backends[uid].Connections++
r.setCurrent()
}
// DecrementBackend deccrements the connections count of a backend. nolint: dupl
func (r *ReverseProxy) DecrementBackend(uid string) {
r.mux.Lock()
defer r.mux.Unlock()
if _, ok := r.backends[uid]; !ok {
return
}
// Avoid setting the connections to the max uint32 value.
if r.backends[uid].Connections == 0 {
return
}
r.backends[uid].Connections--
r.setCurrent()
}
// Watch uses the Kubernetes informer API to watch events for the API server.
func (r *ReverseProxy) Watch() (err error) {
kubeconfig := "/etc/kubernetes/admin.conf"
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return
}
ip, err := pkgnet.IP()
if err != nil {
return
}
// Update the host to the node's IP.
config.Host = ip.String() + ":6443"
// Add the node for the purposes of bootstrapping. If we don't do this, the
// kubelet won't be able reach the API server before it becomes healthy.
r.AddBackend("bootstrap", ip.String())
log.Printf("registered bootstrap backend with IP: %s", ip.String())
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return
}
restclient := clientset.CoreV1().RESTClient()
watchlist := cache.NewListWatchFromClient(restclient, "pods", "kube-system", fields.Everything())
_, controller := cache.NewInformer(
watchlist,
&v1.Pod{},
time.Minute*5,
cache.ResourceEventHandlerFuncs{
AddFunc: r.AddFunc(),
DeleteFunc: r.DeleteFunc(),
UpdateFunc: r.UpdateFunc(),
},
)
stop := make(chan struct{})
controller.Run(stop)
return nil
}
// AddFunc is a ResourceEventHandlerFunc.
func (r *ReverseProxy) AddFunc() func(obj interface{}) {
return func(obj interface{}) {
pod := obj.(*v1.Pod)
if !isAPIServer(pod) {
return
}
// We need an IP address to register.
if pod.Status.PodIP == "" {
return
}
// Return early in the case of any container not being ready.
for _, status := range pod.Status.ContainerStatuses {
if !status.Ready {
return
}
}
r.AddBackend(string(pod.UID), pod.Status.PodIP)
log.Printf("registered API server %s (UID: %q) with IP: %s", pod.Name, pod.UID, pod.Status.PodIP)
if deleted := r.DeleteBackend("bootstrap"); deleted {
log.Println("deregistered bootstrap backend")
}
}
}
// UpdateFunc is a ResourceEventHandlerFunc.
func (r *ReverseProxy) UpdateFunc() func(oldObj, newObj interface{}) {
return func(oldObj, newObj interface{}) {
old := oldObj.(*v1.Pod)
new := newObj.(*v1.Pod)
if !isAPIServer(old) {
return
}
// Remove the backend if any container is not ready.
for _, status := range new.Status.ContainerStatuses {
if !status.Ready {
if deleted := r.DeleteBackend(old.Status.PodIP); deleted {
log.Printf("deregistered unhealthy API server %s (UID: %q) with IP: %s", old.Name, old.UID, old.Status.PodIP)
}
break
}
}
// We need an IP address to register.
if old.Status.PodIP == "" && new.Status.PodIP != "" {
r.AddBackend(string(new.UID), new.Status.PodIP)
log.Printf("registered API server %s (UID: %q) with IP: %s", new.Name, new.UID, new.Status.PodIP)
if deleted := r.DeleteBackend("bootstrap"); deleted {
log.Println("deregistered bootstrap backend")
}
}
}
}
// DeleteFunc is a ResourceEventHandlerFunc.
func (r *ReverseProxy) DeleteFunc() func(obj interface{}) {
return func(obj interface{}) {
pod := obj.(*v1.Pod)
if !isAPIServer(pod) {
return
}
if deleted := r.DeleteBackend(pod.Status.PodIP); deleted {
log.Printf("deregistered API server %s (UID: %q) with IP: %s", pod.Name, pod.UID, pod.Status.PodIP)
}
}
}
func (r *ReverseProxy) setCurrent() {
least := uint32(math.MaxUint32)
for _, b := range r.backends {
switch {
case b.Connections == 0:
fallthrough
case b.Connections < least:
least = b.Connections
r.current = b
}
}
}
func (r *ReverseProxy) proxyConnection(c1 net.Conn) {
backend := r.GetBackend()
if backend == nil {
log.Printf("no available backend, closing remote connection: %s", c1.RemoteAddr().String())
// nolint: errcheck
c1.Close()
return
}
uid := backend.UID
addr := backend.Addr
c2, err := net.DialTimeout("tcp", addr+":6443", time.Duration(r.ConnectTimeout)*time.Millisecond)
if err != nil {
log.Printf("dial %v: %v", addr, err)
// nolint: errcheck
c1.Close()
return
}
// Ensure the connections are valid.
if c1 == nil || c2 == nil {
return
}
r.IncrementBackend(uid)
r.joinConnections(uid, c1, c2)
}
func (r *ReverseProxy) joinConnections(uid string, c1 net.Conn, c2 net.Conn) {
defer r.DecrementBackend(uid)
tcp1, ok := c1.(*net.TCPConn)
if !ok {
return
}
tcp2, ok := c2.(*net.TCPConn)
if !ok {
return
}
log.Printf("%s -> %s", c1.RemoteAddr(), c2.RemoteAddr())
var wg sync.WaitGroup
join := func(dst *net.TCPConn, src *net.TCPConn) {
// Close after the copy to avoid a deadlock.
// nolint: errcheck
defer dst.CloseRead()
defer wg.Done()
_, err := io.Copy(dst, src)
if err != nil {
log.Printf("%v", err)
}
}
wg.Add(2)
go join(tcp1, tcp2)
go join(tcp2, tcp1)
wg.Wait()
}
func tlsConfig() (config *tls.Config, err error) {
caCert, err := ioutil.ReadFile("/etc/kubernetes/pki/ca.crt")
if err != nil {
return
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(caCert)
config = &tls.Config{RootCAs: certPool}
return config, nil
}
func isAPIServer(pod *v1.Pod) bool {
// This is used for non-self-hosted deployments.
if component, ok := pod.Labels["component"]; ok {
if component == "kube-apiserver" {
return true
}
}
// This is used for self-hosted deployments.
if k8sApp, ok := pod.Labels["k8s-app"]; ok {
if k8sApp == "self-hosted-kube-apiserver" {
return true
}
}
return false
}