forked from projectcalico/typha
/
rebalance.go
109 lines (99 loc) · 3.23 KB
/
rebalance.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
// Copyright (c) 2017 Tigera, Inc. All rights reserved.
//
// 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 k8s
import (
log "github.com/sirupsen/logrus"
"context"
"time"
"github.com/projectcalico/typha/pkg/config"
)
type MaxConnsAPI interface {
SetMaxConns(numConns int)
}
type K8sAPI interface {
GetNumTyphas(namespace, serviceName, portName string) (int, error)
GetNumNodes() (int, error)
}
func PollK8sForConnectionLimit(
cxt context.Context,
configParams *config.Config,
tickerC <-chan time.Time,
k8sAPI K8sAPI,
server MaxConnsAPI,
) {
logCxt := log.WithField("thread", "k8s-poll")
logCxt.Info("Kubernetes poll goroutine started.")
activeTarget := configParams.MaxConnectionsUpperLimit
for {
select {
case <-tickerC:
// Get the number of Typhas in the service.
numTyphas, tErr := k8sAPI.GetNumTyphas(
configParams.K8sNamespace,
configParams.K8sServiceName,
configParams.K8sPortName,
)
if tErr != nil || numTyphas <= 0 {
logCxt.WithError(tErr).WithField("numTyphas", numTyphas).Warn(
"Failed to get number of Typhas")
}
// Get the number of nodes as an estimate for the number of Felix connections we should expect.
numNodes, nErr := k8sAPI.GetNumNodes()
if nErr != nil || numNodes <= 0 {
logCxt.WithError(nErr).WithField("numNodes", numNodes).Warn(
"Failed to get number of nodes")
}
target := configParams.MaxConnectionsUpperLimit
reason := "error"
if tErr == nil && nErr == nil {
target, reason = CalculateMaxConnLimit(configParams, numTyphas, numNodes)
}
if target != activeTarget {
logCxt.WithFields(log.Fields{
"numTyphas": numTyphas,
"numNodes": numNodes,
"newLimit": target,
"reason": reason,
}).Info("Calculated new connection limit.")
server.SetMaxConns(target)
activeTarget = target
}
case <-cxt.Done():
logCxt.Info("Context finished")
return
}
}
}
func CalculateMaxConnLimit(configParams *config.Config, numTyphas, numNodes int) (target int, reason string) {
reason = "configured lower limit"
target = configParams.MaxConnectionsLowerLimit
if numTyphas <= 1 {
reason = "lone typha"
target = configParams.MaxConnectionsUpperLimit
return
}
// We subtract 1 from the number of Typhas when calculating the fraction to allow for one Typha
// dying during a rolling upgrade, for example. That does mean our load will be less even but
// it reduces the number of expensive disconnections. We add 20% to give some further headroom.
candidate := 1 + numNodes*120/(numTyphas-1)/100
if candidate > target {
reason = "fraction+20%"
target = candidate
}
if target > configParams.MaxConnectionsUpperLimit {
reason = "configured upper limit"
target = configParams.MaxConnectionsUpperLimit
}
return
}