forked from secretflow/kuscia
/
cluster_config.go
279 lines (241 loc) · 8.3 KB
/
cluster_config.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
// Copyright 2023 Ant Group Co., Ltd.
//
// 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 xds
import (
"fmt"
"strings"
"time"
envoycluster "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
tls "github.com/envoyproxy/go-control-plane/envoy/extensions/transport_sockets/tls/v3"
envoyhttp "github.com/envoyproxy/go-control-plane/envoy/extensions/upstreams/http/v3"
v3 "github.com/envoyproxy/go-control-plane/envoy/type/v3"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/wrapperspb"
"github.com/secretflow/kuscia/pkg/utils/nlog"
)
const (
ProtocolHTTP = "HTTP"
ProtocolHTTPS = "HTTPS"
ProtocolGRPC = "GRPC"
ProtocolGRPCS = "GRPCS"
)
func GenerateProtocol(isTLS, isGRPC bool) string {
if isTLS {
if isGRPC {
return ProtocolGRPCS
}
return ProtocolHTTPS
}
if isGRPC {
return ProtocolGRPC
}
return ProtocolHTTP
}
func DecorateCluster(cluster *envoycluster.Cluster) {
cluster.ConnectTimeout = durationpb.New(10 * time.Second)
// dns lookup
cluster.ClusterDiscoveryType = &envoycluster.Cluster_Type{
Type: envoycluster.Cluster_STRICT_DNS,
}
cluster.DnsLookupFamily = envoycluster.Cluster_V4_ONLY
// load balance policy
cluster.LbPolicy = envoycluster.Cluster_RING_HASH
cluster.LbConfig = &envoycluster.Cluster_RingHashLbConfig_{
RingHashLbConfig: &envoycluster.Cluster_RingHashLbConfig{
HashFunction: envoycluster.Cluster_RingHashLbConfig_MURMUR_HASH_2,
},
}
cluster.CommonLbConfig = &envoycluster.Cluster_CommonLbConfig{
HealthyPanicThreshold: &v3.Percent{
Value: 5,
},
}
cluster.OutlierDetection = &envoycluster.OutlierDetection{
EnforcingConsecutive_5Xx: wrapperspb.UInt32(0), // disable this, we only count local origin failures
SplitExternalLocalOriginErrors: true,
ConsecutiveLocalOriginFailure: wrapperspb.UInt32(1),
BaseEjectionTime: durationpb.New(30 * time.Second),
MaxEjectionTime: durationpb.New(30 * time.Second),
}
}
func DecorateRemoteUpstreamCluster(cluster *envoycluster.Cluster, protocol string) error {
DecorateCluster(cluster)
// enable tcp keep alive as a client
cluster.UpstreamConnectionOptions = &envoycluster.UpstreamConnectionOptions{
TcpKeepalive: &core.TcpKeepalive{},
}
// set HTTPOptions
if err := GenerateUpstreamHTTPOptions(cluster, protocol); err != nil {
return err
}
return DecorateClusterTransport(cluster, protocol)
}
func DecorateLocalUpstreamCluster(cluster *envoycluster.Cluster, protocol string) error {
DecorateCluster(cluster)
// set HTTPOptions
if err := GenerateUpstreamHTTPOptions(cluster, protocol); err != nil {
return err
}
// set transport
return DecorateClusterTransport(cluster, protocol)
}
func DecorateClusterTransport(cluster *envoycluster.Cluster, protocol string) error {
if cluster.TransportSocket != nil {
return nil
}
protocol = strings.ToUpper(protocol)
if protocol != ProtocolGRPCS && protocol != ProtocolHTTPS {
return nil
}
tlsContext := &tls.UpstreamTlsContext{}
conf, err := anypb.New(tlsContext)
if err != nil {
return fmt.Errorf("MarshalL UpstreamTlsContext failed with %s", err.Error())
}
nlog.Infof("Generate tls config for %s", cluster.Name)
cluster.TransportSocket = &core.TransportSocket{
Name: "envoy.transport_sockets.tls",
ConfigType: &core.TransportSocket_TypedConfig{
TypedConfig: conf,
},
}
return nil
}
func GenerateUpstreamHTTPOptions(cluster *envoycluster.Cluster, protocol string) error {
if cluster.TypedExtensionProtocolOptions == nil {
cluster.TypedExtensionProtocolOptions = make(map[string]*anypb.Any)
}
HTTPOptions := "envoy.extensions.upstreams.http.v3.HttpProtocolOptions"
if _, ok := cluster.TypedExtensionProtocolOptions[HTTPOptions]; ok {
return nil
}
protocol = strings.ToUpper(protocol)
var err error
var opts *anypb.Any
// set HTTPOptions
if protocol == ProtocolGRPC || protocol == ProtocolGRPCS {
opts, err = anypb.New(GenerateHTTP2UpstreamHTTPOptions(false))
} else {
opts, err = anypb.New(GenerateSimpleUpstreamHTTPOptions(false))
}
if err != nil {
return err
}
cluster.TypedExtensionProtocolOptions[HTTPOptions] = opts
return nil
}
func GenerateSimpleUpstreamHTTPOptions(isRemoteCluster bool) *envoyhttp.HttpProtocolOptions {
protocolOptions := &envoyhttp.HttpProtocolOptions{
UpstreamProtocolOptions: &envoyhttp.HttpProtocolOptions_UseDownstreamProtocolConfig{
UseDownstreamProtocolConfig: &envoyhttp.HttpProtocolOptions_UseDownstreamHttpConfig{},
},
}
if isRemoteCluster {
SetCommonHTTPProtocolOptions(protocolOptions)
}
return protocolOptions
}
func GenerateHTTP2UpstreamHTTPOptions(isRemoteCluster bool) *envoyhttp.HttpProtocolOptions {
protocolOptions := &envoyhttp.HttpProtocolOptions{
UpstreamProtocolOptions: &envoyhttp.HttpProtocolOptions_ExplicitHttpConfig_{
ExplicitHttpConfig: &envoyhttp.HttpProtocolOptions_ExplicitHttpConfig{
ProtocolConfig: &envoyhttp.HttpProtocolOptions_ExplicitHttpConfig_Http2ProtocolOptions{},
},
},
}
if isRemoteCluster {
SetCommonHTTPProtocolOptions(protocolOptions)
}
return protocolOptions
}
func SetCommonHTTPProtocolOptions(options *envoyhttp.HttpProtocolOptions) {
// set connections idle timeout
options.CommonHttpProtocolOptions = &core.HttpProtocolOptions{
IdleTimeout: &durationpb.Duration{Seconds: int64(900)},
}
}
func AddTCPHealthCheck(c *envoycluster.Cluster) *envoycluster.Cluster {
c.HealthChecks = []*core.HealthCheck{
{
Timeout: durationpb.New(time.Second),
Interval: durationpb.New(15 * time.Second),
UnhealthyInterval: durationpb.New(3 * time.Second),
UnhealthyThreshold: wrapperspb.UInt32(1),
HealthyThreshold: wrapperspb.UInt32(1),
HealthChecker: &core.HealthCheck_TcpHealthCheck_{
TcpHealthCheck: &core.HealthCheck_TcpHealthCheck{},
},
},
}
return c
}
func GetClusterHTTPProtocolOptions(clusterName string) (*envoyhttp.HttpProtocolOptions, *envoycluster.Cluster, error) {
cluster, err := QueryCluster(clusterName)
if err != nil {
return nil, nil, err
}
option, ok := cluster.TypedExtensionProtocolOptions["envoy.extensions.upstreams.http.v3.HttpProtocolOptions"]
if !ok {
return nil, cluster, err
}
var protocolOptions envoyhttp.HttpProtocolOptions
if err := proto.Unmarshal(option.Value, &protocolOptions); err != nil {
return nil, cluster, err
}
return &protocolOptions, cluster, nil
}
func SetKeepAliveForDstCluster(c *envoycluster.Cluster, enable bool) error {
optionName := "envoy.extensions.upstreams.http.v3.HttpProtocolOptions"
option, ok := c.TypedExtensionProtocolOptions[optionName]
if !ok {
return nil
}
var protocolOptions envoyhttp.HttpProtocolOptions
if err := proto.Unmarshal(option.Value, &protocolOptions); err != nil {
return err
}
var action string
if enable {
if protocolOptions.CommonHttpProtocolOptions == nil || protocolOptions.CommonHttpProtocolOptions.
MaxRequestsPerConnection == nil {
return nil
}
protocolOptions.CommonHttpProtocolOptions.MaxRequestsPerConnection = nil
action = "enable"
} else {
if protocolOptions.CommonHttpProtocolOptions == nil {
SetCommonHTTPProtocolOptions(&protocolOptions)
}
if protocolOptions.CommonHttpProtocolOptions.MaxRequestsPerConnection != nil && protocolOptions.
CommonHttpProtocolOptions.MaxRequestsPerConnection.Value == uint32(1) {
return nil
}
protocolOptions.CommonHttpProtocolOptions.MaxRequestsPerConnection = &wrapperspb.UInt32Value{
Value: uint32(1),
}
action = "disable"
}
b, err := proto.Marshal(&protocolOptions)
if err != nil {
nlog.Errorf("Marshal protocolOptions failed with %s", err.Error())
return err
}
nlog.Infof("%s keep-alive for cluster:%s ", action, c.Name)
option.Value = b
c.TypedExtensionProtocolOptions[optionName] = option
return nil
}