-
Notifications
You must be signed in to change notification settings - Fork 396
/
loadbalancer.go
455 lines (402 loc) · 14.5 KB
/
loadbalancer.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
Copyright 2020 The OpenYurt Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package remote
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"sync"
"time"
"k8s.io/apimachinery/pkg/runtime/schema"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/klog/v2"
"github.com/openyurtio/openyurt/pkg/yurthub/cachemanager"
"github.com/openyurtio/openyurt/pkg/yurthub/filter/manager"
"github.com/openyurtio/openyurt/pkg/yurthub/healthchecker"
"github.com/openyurtio/openyurt/pkg/yurthub/poolcoordinator"
coordinatorconstants "github.com/openyurtio/openyurt/pkg/yurthub/poolcoordinator/constants"
"github.com/openyurtio/openyurt/pkg/yurthub/proxy/util"
"github.com/openyurtio/openyurt/pkg/yurthub/transport"
hubutil "github.com/openyurtio/openyurt/pkg/yurthub/util"
)
const (
watchCheckInterval = 5 * time.Second
)
type loadBalancerAlgo interface {
PickOne() *util.RemoteProxy
Name() string
}
type rrLoadBalancerAlgo struct {
sync.Mutex
checker healthchecker.MultipleBackendsHealthChecker
backends []*util.RemoteProxy
next int
}
func (rr *rrLoadBalancerAlgo) Name() string {
return "rr algorithm"
}
func (rr *rrLoadBalancerAlgo) PickOne() *util.RemoteProxy {
if len(rr.backends) == 0 {
return nil
} else if len(rr.backends) == 1 {
if rr.checker.BackendHealthyStatus(rr.backends[0].RemoteServer()) {
return rr.backends[0]
}
return nil
} else {
// round robin
rr.Lock()
defer rr.Unlock()
hasFound := false
selected := rr.next
for i := 0; i < len(rr.backends); i++ {
selected = (rr.next + i) % len(rr.backends)
if rr.checker.BackendHealthyStatus(rr.backends[selected].RemoteServer()) {
hasFound = true
break
}
}
if hasFound {
rr.next = (selected + 1) % len(rr.backends)
return rr.backends[selected]
}
}
return nil
}
type priorityLoadBalancerAlgo struct {
sync.Mutex
checker healthchecker.MultipleBackendsHealthChecker
backends []*util.RemoteProxy
}
func (prio *priorityLoadBalancerAlgo) Name() string {
return "priority algorithm"
}
func (prio *priorityLoadBalancerAlgo) PickOne() *util.RemoteProxy {
if len(prio.backends) == 0 {
return nil
} else if len(prio.backends) == 1 {
if prio.checker.BackendHealthyStatus(prio.backends[0].RemoteServer()) {
return prio.backends[0]
}
return nil
} else {
prio.Lock()
defer prio.Unlock()
for i := 0; i < len(prio.backends); i++ {
if prio.checker.BackendHealthyStatus(prio.backends[i].RemoteServer()) {
return prio.backends[i]
}
}
return nil
}
}
// LoadBalancer is an interface for proxying http request to remote server
// based on the load balance mode(round-robin or priority)
type LoadBalancer interface {
ServeHTTP(rw http.ResponseWriter, req *http.Request)
}
type loadBalancer struct {
backends []*util.RemoteProxy
algo loadBalancerAlgo
localCacheMgr cachemanager.CacheManager
filterManager *manager.Manager
coordinatorGetter func() poolcoordinator.Coordinator
workingMode hubutil.WorkingMode
stopCh <-chan struct{}
}
// NewLoadBalancer creates a loadbalancer for specified remote servers
func NewLoadBalancer(
lbMode string,
remoteServers []*url.URL,
localCacheMgr cachemanager.CacheManager,
transportMgr transport.Interface,
coordinatorGetter func() poolcoordinator.Coordinator,
healthChecker healthchecker.MultipleBackendsHealthChecker,
filterManager *manager.Manager,
workingMode hubutil.WorkingMode,
stopCh <-chan struct{}) (LoadBalancer, error) {
lb := &loadBalancer{
localCacheMgr: localCacheMgr,
filterManager: filterManager,
coordinatorGetter: coordinatorGetter,
workingMode: workingMode,
stopCh: stopCh,
}
backends := make([]*util.RemoteProxy, 0, len(remoteServers))
for i := range remoteServers {
b, err := util.NewRemoteProxy(remoteServers[i], lb.modifyResponse, lb.errorHandler, transportMgr, stopCh)
if err != nil {
klog.Errorf("could not new proxy backend(%s), %v", remoteServers[i].String(), err)
continue
}
backends = append(backends, b)
}
if len(backends) == 0 {
return nil, fmt.Errorf("no backends can be used by lb")
}
var algo loadBalancerAlgo
switch lbMode {
case "rr":
algo = &rrLoadBalancerAlgo{backends: backends, checker: healthChecker}
case "priority":
algo = &priorityLoadBalancerAlgo{backends: backends, checker: healthChecker}
default:
algo = &rrLoadBalancerAlgo{backends: backends, checker: healthChecker}
}
lb.backends = backends
lb.algo = algo
return lb, nil
}
func (lb *loadBalancer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// pick a remote proxy based on the load balancing algorithm.
rp := lb.algo.PickOne()
if rp == nil {
// exceptional case
klog.Errorf("could not pick one healthy backends by %s for request %s", lb.algo.Name(), hubutil.ReqString(req))
http.Error(rw, "could not pick one healthy backends, try again to go through local proxy.", http.StatusInternalServerError)
return
}
klog.V(3).Infof("picked backend %s by %s for request %s", rp.Name(), lb.algo.Name(), hubutil.ReqString(req))
// If pool-scoped resource request is from leader-yurthub, it should always be sent to the cloud APIServer.
// Thus we do not need to start a check routine for it. But for other requests, we need to periodically check
// the pool-coordinator status, and switch the traffic to pool-coordinator if it is ready.
if util.IsPoolScopedResouceListWatchRequest(req) && !isRequestFromLeaderYurthub(req) {
// We get here possibly because the pool-coordinator is not ready.
// We should cancel the watch request when pool-coordinator becomes ready.
klog.Infof("pool-coordinator is not ready, we use cloud APIServer to temporarily handle the req: %s", hubutil.ReqString(req))
clientReqCtx := req.Context()
cloudServeCtx, cloudServeCancel := context.WithCancel(clientReqCtx)
go func() {
t := time.NewTicker(watchCheckInterval)
defer t.Stop()
for {
select {
case <-t.C:
coordinator := lb.coordinatorGetter()
if coordinator == nil {
continue
}
if _, isReady := coordinator.IsReady(); isReady {
klog.Infof("notified the pool coordinator is ready, cancel the req %s making it handled by pool coordinator", hubutil.ReqString(req))
util.ReListWatchReq(rw, req)
cloudServeCancel()
return
}
case <-clientReqCtx.Done():
klog.Infof("watch req %s is canceled by client, when pool coordinator is not ready", hubutil.ReqString(req))
return
}
}
}()
newReq := req.Clone(cloudServeCtx)
req = newReq
}
rp.ServeHTTP(rw, req)
}
func (lb *loadBalancer) errorHandler(rw http.ResponseWriter, req *http.Request, err error) {
klog.Errorf("remote proxy error handler: %s, %v", hubutil.ReqString(req), err)
if lb.localCacheMgr == nil || !lb.localCacheMgr.CanCacheFor(req) {
rw.WriteHeader(http.StatusBadGateway)
return
}
ctx := req.Context()
if info, ok := apirequest.RequestInfoFrom(ctx); ok {
if info.Verb == "get" || info.Verb == "list" {
if obj, err := lb.localCacheMgr.QueryCache(req); err == nil {
hubutil.WriteObject(http.StatusOK, obj, rw, req)
return
}
}
}
rw.WriteHeader(http.StatusBadGateway)
}
func (lb *loadBalancer) modifyResponse(resp *http.Response) error {
if resp == nil || resp.Request == nil {
klog.Infof("no request info in response, skip cache response")
return nil
}
req := resp.Request
ctx := req.Context()
// re-added transfer-encoding=chunked response header for watch request
info, exists := apirequest.RequestInfoFrom(ctx)
if exists {
if info.Verb == "watch" {
klog.V(5).Infof("add transfer-encoding=chunked header into response for req %s", hubutil.ReqString(req))
h := resp.Header
if hv := h.Get("Transfer-Encoding"); hv == "" {
h.Add("Transfer-Encoding", "chunked")
}
}
}
if resp.StatusCode >= http.StatusOK && resp.StatusCode <= http.StatusPartialContent {
// prepare response content type
reqContentType, _ := hubutil.ReqContentTypeFrom(ctx)
respContentType := resp.Header.Get("Content-Type")
if len(respContentType) == 0 {
respContentType = reqContentType
}
ctx = hubutil.WithRespContentType(ctx, respContentType)
req = req.WithContext(ctx)
// filter response data
if lb.filterManager != nil {
if ok, runner := lb.filterManager.FindRunner(req); ok {
wrapBody, needUncompressed := hubutil.NewGZipReaderCloser(resp.Header, resp.Body, req, "filter")
size, filterRc, err := runner.Filter(req, wrapBody, lb.stopCh)
if err != nil {
klog.Errorf("failed to filter response for %s, %v", hubutil.ReqString(req), err)
return err
}
resp.Body = filterRc
if size > 0 {
resp.ContentLength = int64(size)
resp.Header.Set("Content-Length", fmt.Sprint(size))
}
// after gunzip in filter, the header content encoding should be removed.
// because there's no need to gunzip response.body again.
if needUncompressed {
resp.Header.Del("Content-Encoding")
}
}
}
if lb.workingMode == hubutil.WorkingModeEdge {
// cache resp with storage interface
lb.cacheResponse(req, resp)
}
} else if resp.StatusCode == http.StatusNotFound && info.Verb == "list" && lb.localCacheMgr != nil {
// 404 Not Found: The CRD may have been unregistered and should be updated locally as well.
// Other types of requests may return a 404 response for other reasons (for example, getting a pod that doesn't exist).
// And the main purpose is to return 404 when list an unregistered resource locally, so here only consider the list request.
gvr := schema.GroupVersionResource{
Group: info.APIGroup,
Version: info.APIVersion,
Resource: info.Resource,
}
err := lb.localCacheMgr.DeleteKindFor(gvr)
if err != nil {
klog.Errorf("failed: %v", err)
}
}
return nil
}
func (lb *loadBalancer) cacheResponse(req *http.Request, resp *http.Response) {
if lb.localCacheMgr.CanCacheFor(req) {
ctx := req.Context()
wrapPrc, needUncompressed := hubutil.NewGZipReaderCloser(resp.Header, resp.Body, req, "cache-manager")
// after gunzip in filter, the header content encoding should be removed.
// because there's no need to gunzip response.body again.
if needUncompressed {
resp.Header.Del("Content-Encoding")
}
resp.Body = wrapPrc
var poolCacheManager cachemanager.CacheManager
var isHealthy bool
coordinator := lb.coordinatorGetter()
if coordinator == nil {
isHealthy = false
} else {
poolCacheManager, isHealthy = coordinator.IsHealthy()
}
if isHealthy && poolCacheManager != nil {
if !isLeaderHubUserAgent(ctx) {
if isRequestOfNodeAndPod(ctx) {
// Currently, for request that does not come from "leader-yurthub",
// we only cache pod and node resources to pool-coordinator.
// Note: We do not allow the non-leader yurthub to cache pool-scoped resources
// into pool-coordinator to ensure that only one yurthub can update pool-scoped
// cache to avoid inconsistency of data.
lb.cacheToLocalAndPool(req, resp, poolCacheManager)
} else {
lb.cacheToLocal(req, resp)
}
} else {
if isPoolScopedCtx(ctx) {
// Leader Yurthub will always list/watch all resources, which contain may resource this
// node does not need.
lb.cacheToPool(req, resp, poolCacheManager)
} else {
klog.Errorf("failed to cache response for request %s, leader yurthub does not cache non-poolscoped resources.", hubutil.ReqString(req))
}
}
return
}
// When pool-coordinator is not healthy or not be enabled, we can
// only cache the response at local.
lb.cacheToLocal(req, resp)
}
}
func (lb *loadBalancer) cacheToLocal(req *http.Request, resp *http.Response) {
ctx := req.Context()
req = req.WithContext(ctx)
rc, prc := hubutil.NewDualReadCloser(req, resp.Body, true)
go func(req *http.Request, prc io.ReadCloser, stopCh <-chan struct{}) {
if err := lb.localCacheMgr.CacheResponse(req, prc, stopCh); err != nil {
klog.Errorf("lb failed to cache req %s in local cache, %v", hubutil.ReqString(req), err)
}
}(req, prc, ctx.Done())
resp.Body = rc
}
func (lb *loadBalancer) cacheToPool(req *http.Request, resp *http.Response, poolCacheManager cachemanager.CacheManager) {
ctx := req.Context()
req = req.WithContext(ctx)
rc, prc := hubutil.NewDualReadCloser(req, resp.Body, true)
go func(req *http.Request, prc io.ReadCloser, stopCh <-chan struct{}) {
if err := poolCacheManager.CacheResponse(req, prc, stopCh); err != nil {
klog.Errorf("lb failed to cache req %s in pool cache, %v", hubutil.ReqString(req), err)
}
}(req, prc, ctx.Done())
resp.Body = rc
}
func (lb *loadBalancer) cacheToLocalAndPool(req *http.Request, resp *http.Response, poolCacheMgr cachemanager.CacheManager) {
ctx := req.Context()
req = req.WithContext(ctx)
rc, prc1, prc2 := hubutil.NewTripleReadCloser(req, resp.Body, true)
go func(req *http.Request, prc io.ReadCloser, stopCh <-chan struct{}) {
if err := lb.localCacheMgr.CacheResponse(req, prc, stopCh); err != nil {
klog.Errorf("lb failed to cache req %s in local cache, %v", hubutil.ReqString(req), err)
}
}(req, prc1, ctx.Done())
if poolCacheMgr != nil {
go func(req *http.Request, prc io.ReadCloser, stopCh <-chan struct{}) {
if err := poolCacheMgr.CacheResponse(req, prc, stopCh); err != nil {
klog.Errorf("lb failed to cache req %s in pool cache, %v", hubutil.ReqString(req), err)
}
}(req, prc2, ctx.Done())
}
resp.Body = rc
}
func isLeaderHubUserAgent(reqCtx context.Context) bool {
comp, hasComp := hubutil.ClientComponentFrom(reqCtx)
return hasComp && comp == coordinatorconstants.DefaultPoolScopedUserAgent
}
func isPoolScopedCtx(reqCtx context.Context) bool {
poolScoped, hasPoolScoped := hubutil.IfPoolScopedResourceFrom(reqCtx)
return hasPoolScoped && poolScoped
}
func isRequestOfNodeAndPod(reqCtx context.Context) bool {
reqInfo, ok := apirequest.RequestInfoFrom(reqCtx)
if !ok {
return false
}
return (reqInfo.Resource == "nodes" && reqInfo.APIGroup == "" && reqInfo.APIVersion == "v1") ||
(reqInfo.Resource == "pods" && reqInfo.APIGroup == "" && reqInfo.APIVersion == "v1")
}
func isRequestFromLeaderYurthub(req *http.Request) bool {
ctx := req.Context()
agent, ok := hubutil.ClientComponentFrom(ctx)
if !ok {
return false
}
return agent == coordinatorconstants.DefaultPoolScopedUserAgent
}