-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupstream.go
234 lines (205 loc) · 9.38 KB
/
upstream.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
// Copyright 2022 API7.ai, Inc
//
// 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 cloud
import (
"fmt"
)
const (
// HealthCheckTypeTCP indicates a TCP-type health check.
HealthCheckTypeTCP = "tcp"
// HealthCheckTypeHTTP indicates an HTTP-type health check.
HealthCheckTypeHTTP = "http"
// HealthCheckTypeHTTPS indicates an HTTPS-type health check.
HealthCheckTypeHTTPS = "https"
// UpstreamSchemeHTTP indicates the http scheme for communicating with upstream.
UpstreamSchemeHTTP = "http"
// UpstreamSchemeHTTPS indicates the https scheme for communicating with upstream.
UpstreamSchemeHTTPS = "https"
// LoadBalanceRoundRobin indicates the weighted round robin load balancing algorithm.
LoadBalanceRoundRobin = "roundrobin"
// LoadBalanceConsistentHash indicates the consistent hash load balancing algorithm.
LoadBalanceConsistentHash = "consistent_hash"
// LoadBalanceEWMA indicates the ewma load balancing algorithm.
LoadBalanceEWMA = "ewma"
// LoadBalanceLeastConn indicates the least connection load balancing algorithm.
LoadBalanceLeastConn = "least_conn"
)
// UpstreamAndVersion contains both the upstream definition and the version information.
type UpstreamAndVersion struct {
Upstream Upstream `json:"upstream"`
// Version information about this upstream
Version string `json:"version"`
// ClientCertID settings the client cert for communicating with the upstream
// Deprecated: use Upstream.ClientCertID.
ClientCertID ID `json:"client_cert_id,omitempty"`
}
// Upstream is the definition of the upstream on Application.
type Upstream struct {
// The scheme to communicate with the upstream
Scheme string `json:"scheme"`
// LBType is the load balancing strategy of the upstream
LBType string `json:"lb_type,omitempty"`
// HashKey is the hash key used to balance the upstream
HashKey string `json:"hash_key,omitempty"`
// ServiceDiscovery is the service discovery of the upstream
ServiceDiscovery *UpstreamServiceDiscovery `json:"service_discovery,omitempty"`
// The upstream endpoints
Targets []UpstreamTarget `json:"targets,omitempty"`
// Retries is sets the number of retries while passing the request to Upstream using the underlying Nginx mechanism.
Retries *int `json:"retries,omitempty"`
// Timeout is sets the timeout for connecting to, and sending and receiving messages to and from the Upstream
Timeout *UpstreamTimeout `json:"timeout,omitempty"`
// UpstreamHostMode configures the host header when the request is forwarded to the upstream
UpstreamHostMode string `json:"upstream_host_mode,omitempty"`
// UpstreamHost specifies the host of the Upstream request, this is only valid if the upstream_host_mode is set to rewrite
UpstreamHost string `json:"upstream_host,omitempty"`
// ClientCertID settings the client cert for communicating with the upstream
ClientCertID ID `json:"client_cert_id,omitempty"`
//Checks the data of health check
Checks *Checks `json:"checks,omitempty"`
}
// Checks the data of health check
type Checks struct {
Active *ActiveHealthCheck `json:"active,omitempty"`
Passive *PassiveHealthCheck `json:"passive,omitempty"`
}
// ActiveHealthCheck the data of active health check
type ActiveHealthCheck struct {
Type string `json:"type"`
HTTP *HTTPActiveHealthCheck `json:"http"`
HTTPS *HTTPSActiveHealthCheck `json:"https"`
TCP *TCPActiveCheckPredicates `json:"tcp"`
}
// HTTPActiveHealthCheck is the configuration of HTTP active health check
type HTTPActiveHealthCheck struct {
ProbeTimeout int64 `json:"probe_timeout,omitempty"`
ConcurrentProbes int64 `json:"concurrent_probes,omitempty"`
HTTPProbePath string `json:"http_probe_path,omitempty"`
HTTPProbeHost string `json:"http_probe_host,omitempty"`
ProbeTargetPort int64 `json:"probe_target_port,omitempty"`
HTTPProbeHeaders ProbeHeader `json:"http_probe_headers,omitempty"`
Healthy HTTPHealthyPredicates `json:"healthy,omitempty"`
UnHealthy HTTPUnhealthyPredicates `json:"unhealthy,omitempty"`
}
// ProbeHeader indicates headers that will be taken in probe requests.
type ProbeHeader map[string]string
func (header ProbeHeader) ToStringArray() []string {
var res []string
if header == nil {
return res
}
for k, v := range header {
s := fmt.Sprintf("%v: %v", k, v)
res = append(res, s)
}
return res
}
// HTTPSActiveHealthCheck the data of active health check for https
type HTTPSActiveHealthCheck struct {
HTTPActiveHealthCheck
VerifyTargetTlsCertificate bool `json:"verify_target_tls_certificate"`
}
// HTTPHealthyPredicates healthy predicates.
type HTTPHealthyPredicates struct {
TargetsCheckInterval int64 `json:"targets_check_interval,omitempty"`
HTTPStatusCodes []int `json:"http_status_codes,omitempty"`
Successes int64 `json:"successes,omitempty"`
}
// HTTPUnhealthyPredicates unhealthy predicates.
type HTTPUnhealthyPredicates struct {
TargetsCheckInterval int64 `json:"targets_check_interval,omitempty"`
HTTPStatusCodes []int `json:"http_status_codes,omitempty"`
HTTPFailures int64 `json:"http_failures,omitempty"`
Timeouts int64 `json:"timeouts,omitempty"`
}
// TCPActiveCheckPredicates predicates for the TCP probe active health check
type TCPActiveCheckPredicates struct {
ProbeTimeout int64 `json:"probe_timeout,omitempty"`
ConcurrentProbes int64 `json:"concurrent_probes,omitempty"`
ProbeTargetPort int64 `json:"probe_target_port,omitempty"`
Healthy *TCPHealthyPredicates `json:"healthy,omitempty"`
UnHealthy *TCPUnhealthyPredicates `json:"unhealthy,omitempty"`
}
// TCPHealthyPredicates the healthy case data of tcp health check.
type TCPHealthyPredicates struct {
TargetsCheckInterval int64 `json:"targets_check_interval,omitempty"`
Successes int64 `json:"successes,omitempty"`
}
// TCPUnhealthyPredicates the unhealthy case data of tcp health check.
type TCPUnhealthyPredicates struct {
TargetsCheckInterval int64 `json:"targets_check_interval,omitempty"`
TcpFailures int64 `json:"tcp_failures,omitempty"`
Timeouts int64 `json:"timeouts,omitempty"`
}
// PassiveHealthCheck the data of passive health check
type PassiveHealthCheck struct {
Type string `json:"type"`
HTTP *HTTPPassiveHealthCheck `json:"http"`
HTTPS *HTTPPassiveHealthCheck `json:"https"`
TCP *TCPPassiveCheckPredicates `json:"tcp"`
}
// HTTPPassiveHealthCheck is the configuration of HTTP passive health check
type HTTPPassiveHealthCheck struct {
Healthy HTTPHealthyPredicatesForPassive `json:"healthy,omitempty"`
UnHealthy HTTPUnhealthyPredicatesForPassive `json:"unhealthy,omitempty"`
}
// HTTPHealthyPredicatesForPassive healthy predicates for passive health check.
type HTTPHealthyPredicatesForPassive struct {
HTTPStatusCodes []int `json:"http_status_codes,omitempty"`
}
// HTTPUnhealthyPredicatesForPassive unhealthy predicates for passive health check.
type HTTPUnhealthyPredicatesForPassive struct {
HTTPStatusCodes []int `json:"http_status_codes,omitempty"`
HTTPFailures int64 `json:"http_failures,omitempty"`
Timeouts int64 `json:"timeouts,omitempty"`
}
// TCPPassiveCheckPredicates predicates for the TCP probe passive health check
type TCPPassiveCheckPredicates struct {
UnHealthy *TCPUnhealthyPredicatesForPassive `json:"unhealthy,omitempty"`
}
// TCPUnhealthyPredicatesForPassive the unhealthy case data of passive tcp health check.
type TCPUnhealthyPredicatesForPassive struct {
TcpFailures int64 `json:"tcp_failures,omitempty"`
Timeouts int64 `json:"timeouts,omitempty"`
}
// UpstreamServiceDiscovery is the service discovery of the upstream.
type UpstreamServiceDiscovery struct {
// ServiceRegistry is the type of service registry
ServiceRegistry ServiceRegistryType `json:"service_registry"`
// ServiceRegistryID is the id of service registry
ServiceRegistryID ID `json:"service_registry_id"`
// KubernetesService is the kubernetes service discovery of the upstream
KubernetesService KubernetesUpstreamServiceDiscovery `json:"kubernetes_service"`
}
// KubernetesUpstreamServiceDiscovery is the kubernetes service discovery of the upstream.
type KubernetesUpstreamServiceDiscovery struct {
// Namespace is the namespace of the kubernetes endpoint
Namespace string `json:"namespace"`
// Name is the name of the kubernetes endpoint
Name string `json:"name"`
// Port is the target port of the kubernetes endpoint
Port string `json:"port"`
}
// UpstreamTarget is the definition for an upstream endpoint.
type UpstreamTarget struct {
Host string `json:"host"`
Port int `json:"port"`
Weight int `json:"weight"`
}
// UpstreamTimeout is the timeout for connecting to, and sending and receiving messages to and from the Upstream, value in seconds.
type UpstreamTimeout struct {
Connect int `json:"connect,omitempty"`
Send int `json:"send,omitempty"`
Read int `json:"read,omitempty"`
}