-
Notifications
You must be signed in to change notification settings - Fork 335
/
lbconfigurer.go
90 lines (86 loc) · 3.03 KB
/
lbconfigurer.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
package xds
import (
envoy_cluster "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
corev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
"google.golang.org/protobuf/types/known/wrapperspb"
common_api "github.com/kumahq/kuma/api/common/v1alpha1"
api "github.com/kumahq/kuma/pkg/plugins/policies/meshloadbalancingstrategy/api/v1alpha1"
"github.com/kumahq/kuma/pkg/util/pointer"
util_proto "github.com/kumahq/kuma/pkg/util/proto"
)
type LoadBalancerConfigurer struct {
LoadBalancer api.LoadBalancer
}
func (c *LoadBalancerConfigurer) Configure(cluster *envoy_cluster.Cluster) error {
switch c.LoadBalancer.Type {
case api.RoundRobinType:
cluster.LbPolicy = envoy_cluster.Cluster_ROUND_ROBIN
case api.LeastRequestType:
cluster.LbPolicy = envoy_cluster.Cluster_LEAST_REQUEST
if c.LoadBalancer.LeastRequest == nil {
return nil
}
leastRequests := &envoy_cluster.Cluster_LeastRequestLbConfig{}
if arb := c.LoadBalancer.LeastRequest.ActiveRequestBias; arb != nil {
decimal, err := common_api.NewDecimalFromIntOrString(pointer.Deref(arb))
if err != nil {
return err
}
leastRequests.ActiveRequestBias = &corev3.RuntimeDouble{
DefaultValue: decimal.InexactFloat64(),
}
}
if cc := c.LoadBalancer.LeastRequest.ChoiceCount; cc != nil {
leastRequests.ChoiceCount = util_proto.UInt32(*cc)
}
if leastRequests.ChoiceCount != nil || leastRequests.ActiveRequestBias != nil {
cluster.LbConfig = &envoy_cluster.Cluster_LeastRequestLbConfig_{
LeastRequestLbConfig: leastRequests,
}
}
case api.RandomType:
cluster.LbPolicy = envoy_cluster.Cluster_RANDOM
case api.RingHashType:
cluster.LbPolicy = envoy_cluster.Cluster_RING_HASH
if c.LoadBalancer.RingHash == nil {
return nil
}
var minimumRingSize *wrapperspb.UInt64Value
if min := c.LoadBalancer.RingHash.MinRingSize; min != nil {
minimumRingSize = util_proto.UInt64(uint64(*min))
}
var maximumRingSize *wrapperspb.UInt64Value
if max := c.LoadBalancer.RingHash.MaxRingSize; max != nil {
maximumRingSize = util_proto.UInt64(uint64(*max))
}
var hashFunction envoy_cluster.Cluster_RingHashLbConfig_HashFunction
if hf := c.LoadBalancer.RingHash.HashFunction; hf != nil {
switch *hf {
case api.MurmurHash2Type:
hashFunction = envoy_cluster.Cluster_RingHashLbConfig_MURMUR_HASH_2
case api.XXHashType:
hashFunction = envoy_cluster.Cluster_RingHashLbConfig_XX_HASH
}
}
cluster.LbConfig = &envoy_cluster.Cluster_RingHashLbConfig_{
RingHashLbConfig: &envoy_cluster.Cluster_RingHashLbConfig{
MinimumRingSize: minimumRingSize,
MaximumRingSize: maximumRingSize,
HashFunction: hashFunction,
},
}
case api.MaglevType:
cluster.LbPolicy = envoy_cluster.Cluster_MAGLEV
if c.LoadBalancer.Maglev == nil {
return nil
}
if tableSize := c.LoadBalancer.Maglev.TableSize; tableSize != nil {
cluster.LbConfig = &envoy_cluster.Cluster_MaglevLbConfig_{
MaglevLbConfig: &envoy_cluster.Cluster_MaglevLbConfig{
TableSize: util_proto.UInt64(uint64(*tableSize)),
},
}
}
}
return nil
}