-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtypes.go
More file actions
548 lines (469 loc) · 16.7 KB
/
types.go
File metadata and controls
548 lines (469 loc) · 16.7 KB
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
package adc
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/incubator4/go-resty-expr/expr"
)
const (
// HashOnVars means the hash scope is variable.
HashOnVars = "vars"
// HashOnVarsCombination means the hash scope is the
// variable combination.
HashOnVarsCombination = "vars_combinations"
// HashOnHeader means the hash scope is HTTP request
// headers.
HashOnHeader = "header"
// HashOnCookie means the hash scope is HTTP Cookie.
HashOnCookie = "cookie"
// HashOnConsumer means the hash scope is APISIX consumer.
HashOnConsumer = "consumer"
// LbRoundRobin is the round robin load balancer.
LBRoundRobin = "roundrobin"
// LbConsistentHash is the consistent hash load balancer.
LbConsistentHash = "chash"
// LbEwma is the ewma load balancer.
LbEwma = "ewma"
// LbLeaseConn is the least connection load balancer.
LbLeastConn = "least_conn"
// SchemeHTTP represents the HTTP protocol.
SchemeHTTP = "http"
// SchemeGRPC represents the GRPC protocol.
SchemeGRPC = "grpc"
// SchemeHTTPS represents the HTTPS protocol.
SchemeHTTPS = "https"
// SchemeGRPCS represents the GRPCS protocol.
SchemeGRPCS = "grpcs"
// SchemeTCP represents the TCP protocol.
SchemeTCP = "tcp"
// SchemeUDP represents the UDP protocol.
SchemeUDP = "udp"
// DefaultUpstreamTimeout represents the default connect,
// read and send timeout (in seconds) with upstreams.
DefaultUpstreamTimeout = 60
// PassHostPass represents pass option for pass_host Upstream settings.
PassHostPass = "pass"
// PassHostPass represents node option for pass_host Upstream settings.
PassHostNode = "node"
// PassHostPass represents rewrite option for pass_host Upstream settings.
PassHostRewrite = "rewrite"
)
type Metadata struct {
ID string `json:"id,omitempty" yaml:"id,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Desc string `json:"description,omitempty" yaml:"description,omitempty"`
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
}
type Resources struct {
ConsumerGroups []*ConsumerGroup `json:"consumer_groups,omitempty" yaml:"consumer_groups,omitempty"`
Consumers []*Consumer `json:"consumers,omitempty" yaml:"consumers,omitempty"`
GlobalRules Plugins `json:"global_rules,omitempty" yaml:"global_rules,omitempty"`
PluginMetadata Plugins `json:"plugin_metadata,omitempty" yaml:"plugin_metadata,omitempty"`
Services []*Service `json:"services,omitempty" yaml:"services,omitempty"`
SSLs []*SSL `json:"ssls,omitempty" yaml:"ssls,omitempty"`
}
type ConsumerGroup struct {
Metadata `json:",inline" yaml:",inline"`
Consumers []Consumer `json:"consumers,omitempty" yaml:"consumers,omitempty"`
Name string `json:"name" yaml:"name"`
Plugins Plugins `json:"plugins" yaml:"plugins"`
}
type Consumer struct {
Credentials []Credential `json:"credentials,omitempty" yaml:"credentials,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
Username string `json:"username" yaml:"username"`
}
type Credential struct {
Metadata `json:",inline" yaml:",inline"`
Config map[string]any `json:"config" yaml:"config"`
Type string `json:"type" yaml:"type"`
}
type Service struct {
Metadata `json:",inline" yaml:",inline"`
Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
PathPrefix string `json:"path_prefix,omitempty" yaml:"path_prefix,omitempty"`
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
Routes []*Route `json:"routes,omitempty" yaml:"routes,omitempty"`
StreamRoutes []*StreamRoute `json:"stream_routes,omitempty" yaml:"stream_routes,omitempty"`
StripPathPrefix *bool `json:"strip_path_prefix,omitempty" yaml:"strip_path_prefix,omitempty"`
Upstream *Upstream `json:"upstream,omitempty" yaml:"upstream,omitempty"`
}
type Route struct {
Metadata `json:",inline" yaml:",inline"`
EnableWebsocket *bool `json:"enable_websocket,omitempty" yaml:"enable_websocket,omitempty"`
FilterFunc string `json:"filter_func,omitempty" yaml:"filter_func,omitempty"`
Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
Methods []string `json:"methods,omitempty" yaml:"methods,omitempty"`
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
RemoteAddrs []string `json:"remote_addrs,omitempty" yaml:"remote_addrs,omitempty"`
Timeout *Timeout `json:"timeout,omitempty" yaml:"timeout,omitempty"`
Uris []string `json:"uris" yaml:"uris"`
Vars Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
}
type Timeout struct {
Connect float64 `json:"connect"`
Read float64 `json:"read"`
Send float64 `json:"send"`
}
type StreamRoute struct {
Description string `json:"description,omitempty"`
ID string `json:"id,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Name string `json:"name"`
Plugins Plugins `json:"plugins,omitempty"`
RemoteAddr string `json:"remote_addr,omitempty"`
ServerAddr string `json:"server_addr,omitempty"`
ServerPort *int64 `json:"server_port,omitempty"`
Sni string `json:"sni,omitempty"`
}
type Upstream struct {
Metadata `json:",inline" yaml:",inline"`
HashOn string `json:"hash_on,omitempty" yaml:"hash_on,omitempty"`
Key string `json:"key,omitempty" yaml:"key,omitempty"`
Nodes UpstreamNodes `json:"nodes" yaml:"nodes"`
PassHost *PassHost `json:"pass_host,omitempty" yaml:"pass_host,omitempty"`
Retries *int64 `json:"retries,omitempty" yaml:"retries,omitempty"`
RetryTimeout *float64 `json:"retry_timeout,omitempty" yaml:"retry_timeout,omitempty"`
Scheme string `json:"scheme,omitempty" yaml:"scheme,omitempty"`
ServiceName string `json:"service_name,omitempty" yaml:"service_name,omitempty"`
Timeout *Timeout `json:"timeout,omitempty" yaml:"timeout,omitempty"`
Type UpstreamType `json:"type,omitempty" yaml:"type,omitempty"`
UpstreamHost string `json:"upstream_host,omitempty" yaml:"upstream_host,omitempty"`
}
type TLSClass struct {
ClientCERT string `json:"client_cert,omitempty"`
ClientCERTID string `json:"client_cert_id,omitempty"`
ClientKey string `json:"client_key,omitempty"`
Verify *bool `json:"verify,omitempty"`
}
type SSL struct {
Metadata `json:",inline" yaml:",inline"`
Certificates []Certificate `json:"certificates" yaml:"certificates"`
Client *ClientClass `json:"client,omitempty" yaml:"client,omitempty"`
Snis []string `json:"snis" yaml:"snis"`
SSLProtocols []SSLProtocol `json:"ssl_protocols,omitempty" yaml:"ssl_protocols,omitempty"`
Type *SSLType `json:"type,omitempty" yaml:"type,omitempty"`
}
type Certificate struct {
Certificate string `json:"certificate" yaml:"certificate"`
Key string `json:"key" yaml:"key"`
}
type ClientClass struct {
CA string `json:"ca" yaml:"ca"`
Depth *int64 `json:"depth,omitempty" yaml:"depth,omitempty"`
SkipMtlsURIRegex []string `json:"skip_mtls_uri_regex,omitempty" yaml:"skip_mtls_uri_regex,omitempty"`
}
type Method string
const (
Connect Method = "CONNECT"
Delete Method = "DELETE"
Get Method = "GET"
Head Method = "HEAD"
Options Method = "OPTIONS"
Patch Method = "PATCH"
Post Method = "POST"
Purge Method = "PURGE"
Put Method = "PUT"
Trace Method = "TRACE"
)
type PassHost string
const (
Node PassHost = "node"
Pass PassHost = "pass"
Rewrite PassHost = "rewrite"
)
type Scheme string
const (
Grpc Scheme = "grpc"
Grpcs Scheme = "grpcs"
Kafka Scheme = "kafka"
TLS Scheme = "tls"
UDP Scheme = "udp"
)
type UpstreamType string
const (
Chash UpstreamType = "chash"
Ewma UpstreamType = "ewma"
LeastConn UpstreamType = "least_conn"
Roundrobin UpstreamType = "roundrobin"
)
type SSLProtocol string
const (
TLSv11 SSLProtocol = "TLSv1.1"
TLSv12 SSLProtocol = "TLSv1.2"
TLSv13 SSLProtocol = "TLSv1.3"
)
type SSLType string
const (
Client SSLType = "client"
Server SSLType = "server"
)
type Plugins map[string]any
func (p *Plugins) DeepCopyInto(out *Plugins) {
b, _ := json.Marshal(&p)
_ = json.Unmarshal(b, out)
}
func (p Plugins) DeepCopy() Plugins {
if p == nil {
return nil
}
out := make(Plugins)
p.DeepCopyInto(&out)
return out
}
// UpstreamNode is the node in upstream
type UpstreamNode struct {
Host string `json:"host" yaml:"host"`
Port int `json:"port" yaml:"port"`
Weight int `json:"weight" yaml:"weight"`
Priority int `json:"priority,omitempty" yaml:"priority,omitempty"`
}
// UpstreamNodes is the upstream node list.
type UpstreamNodes []UpstreamNode
func mapKV2Node(key string, val float64) (*UpstreamNode, error) {
hp := strings.Split(key, ":")
host := hp[0]
// according to APISIX upstream nodes policy, port is required
port := "80"
if len(hp) > 2 {
return nil, errors.New("invalid upstream node")
} else if len(hp) == 2 {
port = hp[1]
}
portInt, err := strconv.Atoi(port)
if err != nil {
return nil, fmt.Errorf("parse port to int fail: %s", err.Error())
}
node := &UpstreamNode{
Host: host,
Port: portInt,
Weight: int(val),
}
return node, nil
}
// UnmarshalJSON implements json.Unmarshaler interface.
// lua-cjson doesn't distinguish empty array and table,
// and by default empty array will be encoded as '{}'.
// We have to maintain the compatibility.
func (n *UpstreamNodes) UnmarshalJSON(p []byte) error {
var data []UpstreamNode
if p[0] == '{' {
value := map[string]float64{}
if err := json.Unmarshal(p, &value); err != nil {
return err
}
for k, v := range value {
node, err := mapKV2Node(k, v)
if err != nil {
return err
}
data = append(data, *node)
}
*n = data
return nil
}
if err := json.Unmarshal(p, &data); err != nil {
return err
}
*n = data
return nil
}
// ComposeRouteName uses namespace, name and rule name to compose
// the route name.
func ComposeRouteName(namespace, name string, rule string) string {
// FIXME Use sync.Pool to reuse this buffer if the upstream
// name composing code path is hot.
p := make([]byte, 0, len(namespace)+len(name)+len(rule)+2)
buf := bytes.NewBuffer(p)
buf.WriteString(namespace)
buf.WriteByte('_')
buf.WriteString(name)
buf.WriteByte('_')
buf.WriteString(rule)
return buf.String()
}
func ComposeServiceNameWithRule(namespace, name string, rule string) string {
// FIXME Use sync.Pool to reuse this buffer if the upstream
// name composing code path is hot.
var p []byte
plen := len(namespace) + len(name) + 2
p = make([]byte, 0, plen)
buf := bytes.NewBuffer(p)
buf.WriteString(namespace)
buf.WriteByte('_')
buf.WriteString(name)
buf.WriteByte('_')
buf.WriteString(rule)
return buf.String()
}
func ComposeConsumerName(namespace, name string) string {
// FIXME Use sync.Pool to reuse this buffer if the upstream
// name composing code path is hot.
p := make([]byte, 0, len(namespace)+len(name)+1)
buf := bytes.NewBuffer(p)
buf.WriteString(namespace)
buf.WriteByte('_')
buf.WriteString(name)
return buf.String()
}
// NewDefaultUpstream returns an empty Upstream with default values.
func NewDefaultService() *Service {
return &Service{
Metadata: Metadata{
Labels: map[string]string{
"managed-by": "api7-ingress-controller",
},
},
Plugins: make(Plugins),
}
}
func NewDefaultUpstream() *Upstream {
return &Upstream{
Type: Roundrobin,
Nodes: make(UpstreamNodes, 0),
Scheme: SchemeHTTP,
Metadata: Metadata{
Desc: "Created by apisix-ingress-controller, DO NOT modify it manually",
Labels: map[string]string{
"managed-by": "apisix-ingress-controller",
},
},
}
}
// NewDefaultRoute returns an empty Route with default values.
func NewDefaultRoute() *Route {
return &Route{
Metadata: Metadata{
Desc: "Created by api7-ingress-controller, DO NOT modify it manually",
Labels: map[string]string{
"managed-by": "api7-ingress-controller",
},
},
}
}
const (
PluginProxyRewrite string = "proxy-rewrite"
PluginRedirect string = "redirect"
PluginResponseRewrite string = "response-rewrite"
PluginProxyMirror string = "proxy-mirror"
)
// RewriteConfig is the rule config for proxy-rewrite plugin.
type RewriteConfig struct {
RewriteTarget string `json:"uri,omitempty" yaml:"uri,omitempty"`
RewriteTargetRegex []string `json:"regex_uri,omitempty" yaml:"regex_uri,omitempty"`
Headers *Headers `json:"headers,omitempty" yaml:"headers,omitempty"`
Host string `json:"host,omitempty" yaml:"host,omitempty"`
}
type Headers struct {
Set map[string]string `json:"set" yaml:"set"`
Add map[string]string `json:"add" yaml:"add"`
Remove []string `json:"remove" yaml:"remove"`
}
// ResponseRewriteConfig is the rule config for response-rewrite plugin.
type ResponseRewriteConfig struct {
StatusCode int `json:"status_code,omitempty" yaml:"status_code,omitempty"`
Body string `json:"body,omitempty" yaml:"body,omitempty"`
BodyBase64 bool `json:"body_base64,omitempty" yaml:"body_base64,omitempty"`
Headers *ResponseHeaders `json:"headers,omitempty" yaml:"headers,omitempty"`
LuaRestyExpr []expr.Expr `json:"vars,omitempty" yaml:"vars,omitempty"`
Filters []map[string]string `json:"filters,omitempty" yaml:"filters,omitempty"`
}
type ResponseHeaders struct {
Set map[string]string `json:"set" yaml:"set"`
Add []string `json:"add" yaml:"add"`
Remove []string `json:"remove" yaml:"remove"`
}
// RequestMirror is the rule config for proxy-mirror plugin.
type RequestMirror struct {
Host string `json:"host" yaml:"host"`
}
// RedirectConfig is the rule config for redirect plugin.
type RedirectConfig struct {
HttpToHttps bool `json:"http_to_https,omitempty" yaml:"http_to_https,omitempty"`
URI string `json:"uri,omitempty" yaml:"uri,omitempty"`
RetCode int `json:"ret_code,omitempty" yaml:"ret_code,omitempty"`
}
const (
StatusSuccess = "success"
StatusFailed = "failed"
StatusPartialFailed = "partial_failed"
)
type SyncResult struct {
Status string `json:"status"`
TotalResources int `json:"total_resources"`
SuccessCount int `json:"success_count"`
FailedCount int `json:"failed_count"`
Success []SyncStatus `json:"success"`
Failed []SyncStatus `json:"failed"`
}
type SyncStatus struct {
Event StatusEvent `json:"event"`
FailedAt time.Time `json:"failed_at,omitempty"`
SyncedAt time.Time `json:"synced_at,omitempty"`
Reason string `json:"reason,omitempty"`
Response ResponseDetails `json:"response,omitempty"`
}
type StatusEvent struct {
ResourceType string `json:"resourceType"`
Type string `json:"type"`
ResourceID string `json:"resourceId"`
ResourceName string `json:"resourceName"`
ParentID string `json:"parentId,omitempty"`
}
type ResponseDetails struct {
Status int `json:"status"`
Headers map[string]string `json:"headers"`
Data ResponseData `json:"data"`
}
type ResponseData struct {
Value map[string]any `json:"value"`
ErrorMsg string `json:"error_msg"`
}
// Vars represents the route match expressions of APISIX.
type Vars [][]StringOrSlice
// UnmarshalJSON implements json.Unmarshaler interface.
// lua-cjson doesn't distinguish empty array and table,
// and by default empty array will be encoded as '{}'.
// We have to maintain the compatibility.
func (vars *Vars) UnmarshalJSON(p []byte) error {
if p[0] == '{' {
if len(p) != 2 {
return errors.New("unexpected non-empty object")
}
return nil
}
var data [][]StringOrSlice
if err := json.Unmarshal(p, &data); err != nil {
return err
}
*vars = data
return nil
}
// StringOrSlice represents a string or a string slice.
// TODO Do not use interface{} to avoid the reflection overheads.
type StringOrSlice struct {
StrVal string `json:"-"`
SliceVal []StringOrSlice `json:"-"`
}
func (s *StringOrSlice) MarshalJSON() ([]byte, error) {
if s.SliceVal != nil {
return json.Marshal(s.SliceVal)
}
return json.Marshal(s.StrVal)
}
func (s *StringOrSlice) UnmarshalJSON(p []byte) error {
if len(p) == 0 {
return errors.New("empty object")
}
if p[0] == '[' {
return json.Unmarshal(p, &s.SliceVal)
}
return json.Unmarshal(p, &s.StrVal)
}