-
Notifications
You must be signed in to change notification settings - Fork 6
/
types.go
679 lines (570 loc) · 22.2 KB
/
types.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
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
package types
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
)
type HasLabels interface {
GetLabels() Labels
SetLabel(k, v string)
}
var (
_ HasLabels = (*Route)(nil)
_ HasLabels = (*Service)(nil)
_ HasLabels = (*Consumer)(nil)
_ HasLabels = (*SSL)(nil)
_ HasLabels = (*PluginConfig)(nil)
_ HasLabels = (*ConsumerGroup)(nil)
_ HasLabels = (*StreamRoute)(nil)
_ HasLabels = (*Upstream)(nil)
)
func FilterResources[T HasLabels](filters Labels, resources []T) []T {
if len(filters) == 0 {
return resources
}
var filtered []T
for _, res := range resources {
labels := res.GetLabels()
if len(labels) > 0 {
matchAll := true
for k, v := range filters {
if label, ok := labels[k]; !ok || label != v {
matchAll = false
break
}
}
if matchAll {
filtered = append(filtered, res)
}
}
}
return filtered
}
// Configuration is the configuration of services
type Configuration struct {
Name string `yaml:"name" json:"name"`
Version string `yaml:"version" json:"version"`
Meta *ConfigurationMeta `yaml:"meta,omitempty" json:"meta,omitempty"`
Services []*Service `yaml:"services,omitempty" json:"services,omitempty"`
Routes []*Route `yaml:"routes,omitempty" json:"routes,omitempty"`
Consumers []*Consumer `yaml:"consumers,omitempty" json:"consumers,omitempty"`
SSLs []*SSL `yaml:"ssls,omitempty" json:"ssls,omitempty"`
GlobalRules []*GlobalRule `yaml:"global_rules,omitempty" json:"global_rules,omitempty"`
PluginConfigs []*PluginConfig `yaml:"plugin_configs,omitempty" json:"plugin_configs,omitempty"`
ConsumerGroups []*ConsumerGroup `yaml:"consumer_groups,omitempty" json:"consumer_groups,omitempty"`
PluginMetadatas []*PluginMetadata `yaml:"plugin_metadatas,omitempty" json:"plugin_metadatas,omitempty"`
StreamRoutes []*StreamRoute `yaml:"stream_routes,omitempty" json:"stream_routes,omitempty"`
Upstreams []*Upstream `yaml:"upstreams,omitempty" json:"upstreams,omitempty"`
}
type ConfigurationMode string
var (
ModeFull ConfigurationMode = "full"
ModePartial ConfigurationMode = "partial"
)
type ConfigurationMeta struct {
Mode ConfigurationMode `json:"mode,omitempty" yaml:"mode,omitempty"`
Labels Labels `json:"labels,omitempty" yaml:"labels,omitempty"`
}
// Labels is the APISIX resource labels
type Labels map[string]string
// Vars represents the route match expressions of APISIX.
type Vars [][]StringOrSlice
// Route apisix route object
type Route struct {
ID string `json:"id" yaml:"id"`
Name string `json:"name" yaml:"name"`
Labels Labels `json:"labels,omitempty" yaml:"labels,omitempty"`
Description string `json:"desc,omitempty" yaml:"desc,omitempty"`
Host string `json:"host,omitempty" yaml:"host,omitempty"`
Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
Uri string `json:"uri,omitempty" yaml:"uri,omitempty"`
Uris []string `json:"uris,omitempty" yaml:"uris,omitempty"`
Priority *int `json:"priority,omitempty" yaml:"priority,omitempty"`
Timeout *UpstreamTimeout `json:"timeout,omitempty" yaml:"timeout,omitempty"`
Vars Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
Methods []string `json:"methods,omitempty" yaml:"methods,omitempty"`
EnableWebsocket bool `json:"enable_websocket,omitempty" yaml:"enable_websocket,omitempty"`
RemoteAddr string `json:"remote_addr,omitempty" yaml:"remote_addr,omitempty"`
RemoteAddrs []string `json:"remote_addrs,omitempty" yaml:"remote_addrs,omitempty"`
Upstream *Upstream `json:"upstream,omitempty" yaml:"upstream,omitempty"`
UpstreamID string `json:"upstream_id,omitempty" yaml:"upstream_id,omitempty"`
ServiceID string `json:"service_id,omitempty" yaml:"service_id,omitempty"`
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
PluginConfigID string `json:"plugin_config_id,omitempty" yaml:"plugin_config_id,omitempty"`
FilterFunc string `json:"filter_func,omitempty" yaml:"filter_func,omitempty"`
Script string `json:"script,omitempty" yaml:"script,omitempty"`
ScriptID string `json:"script_id,omitempty" yaml:"script_id,omitempty"`
Status *int `json:"status,omitempty" yaml:"status,omitempty"`
// api7
StripPathPrefix bool `json:"strip_path_prefix,omitempty" yaml:"strip_path_prefix,omitempty"`
}
func (r *Route) GetLabels() Labels {
return r.Labels
}
func (r *Route) SetLabel(k, v string) {
if r.Labels == nil {
r.Labels = map[string]string{}
}
r.Labels[k] = v
}
func (r *Route) UnmarshalJSON(cont []byte) error {
type unmarshalerRoute Route
var route unmarshalerRoute
err := json.Unmarshal(cont, &route)
if err != nil {
return err
}
*r = Route(route)
SetRouteDefaultValues(r)
return nil
}
// Service is the abstraction of a backend service on API gateway.
type Service struct {
ID string `json:"id" yaml:"id"`
Name string `json:"name" yaml:"name"`
Description string `json:"desc,omitempty" yaml:"desc,omitempty"`
// Labels are used for resource classification and indexing
Labels Labels `json:"labels,omitempty" yaml:"labels,omitempty"`
// HTTP hosts for this service.
Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
// Plugin settings on Service level
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
// Upstream settings for the Service.
Upstream *Upstream `json:"upstream,omitempty" yaml:"upstream,omitempty"`
// UpstreamID settings for the Service.
UpstreamID string `json:"upstream_id,omitempty" yaml:"upstream_id,omitempty"`
// Enables a websocket. Set to false by default.
EnableWebsocket bool `json:"enable_websocket,omitempty" yaml:"enable_websocket,omitempty"`
Script string `json:"script,omitempty" yaml:"script,omitempty"`
// api7
PathPrefix string `json:"path_prefix,omitempty" yaml:"path_prefix,omitempty"`
Status int `json:"status,omitempty" yaml:"status,omitempty"`
}
func (s *Service) GetLabels() Labels {
return s.Labels
}
func (s *Service) SetLabel(k, v string) {
if s.Labels == nil {
s.Labels = map[string]string{}
}
s.Labels[k] = v
}
func (s *Service) UnmarshalJSON(cont []byte) error {
type unmarshalerService Service
var route unmarshalerService
err := json.Unmarshal(cont, &route)
if err != nil {
return err
}
*s = Service(route)
SetServiceDefaultValues(s)
return nil
}
// Upstream is the definition of the upstream on Service.
type Upstream struct {
// ID is the upstream name. It should be unique among all upstreams
// in the same service.
ID string `json:"id" yaml:"id"`
Name string `json:"name" yaml:"name"`
Labels Labels `json:"labels,omitempty" yaml:"labels,omitempty"`
Type string `json:"type,omitempty" yaml:"type,omitempty"`
HashOn string `json:"hash_on,omitempty" yaml:"hash_on,omitempty"`
Key string `json:"key,omitempty" yaml:"key,omitempty"`
Checks *UpstreamHealthCheck `json:"checks,omitempty" yaml:"checks,omitempty"`
Nodes UpstreamNodes `json:"nodes" yaml:"nodes"`
Scheme string `json:"scheme,omitempty" yaml:"scheme,omitempty"`
Retries int `json:"retries,omitempty" yaml:"retries,omitempty"`
RetryTimeout int `json:"retry_timeout,omitempty" yaml:"retry_timeout,omitempty"`
Timeout *UpstreamTimeout `json:"timeout,omitempty" yaml:"timeout,omitempty"`
TLS *ClientTLS `json:"tls,omitempty" yaml:"tls,omitempty"`
KeepalivePool *KeepalivePool `json:"keepalive_pool,omitempty" yaml:"keepalive_pool,omitempty"`
PassHost string `json:"pass_host,omitempty" yaml:"pass_host,omitempty"`
UpstreamHost string `json:"upstream_host,omitempty" yaml:"upstream_host,omitempty"`
// for Service Discovery
ServiceName string `json:"service_name,omitempty" yaml:"service_name,omitempty"`
DiscoveryType string `json:"discovery_type,omitempty" yaml:"discovery_type,omitempty"`
DiscoveryArgs map[string]string `json:"discovery_args,omitempty" yaml:"discovery_args,omitempty"`
}
func (u *Upstream) GetLabels() Labels {
return u.Labels
}
func (u *Upstream) SetLabel(k, v string) {
if u.Labels == nil {
u.Labels = map[string]string{}
}
u.Labels[k] = v
}
func (u *Upstream) UnmarshalJSON(cont []byte) error {
type unmarshalerUpstream Upstream
var route unmarshalerUpstream
err := json.Unmarshal(cont, &route)
if err != nil {
return err
}
*u = Upstream(route)
SetUpstreamDefaultValues(u)
return nil
}
type KeepalivePool struct {
Size *int `json:"size,omitempty" yaml:"size,omitempty"`
IdleTimeout *int `json:"idle_timeout,omitempty" yaml:"idle_timeout,omitempty"`
Requests *int `json:"requests,omitempty" yaml:"requests,omitempty"`
}
// 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,omitempty" yaml:"weight,omitempty"`
Priority *int `json:"priority,omitempty" yaml:"priority,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty" yaml:"metadata,omitempty"`
}
// UpstreamNodes is the upstream node list.
type UpstreamNodes []UpstreamNode
// 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
}
// UpstreamHealthCheck defines the active and/or passive health check for an Upstream,
// with the upstream health check feature, pods can be kicked out or joined in quickly,
// if the feedback of Kubernetes liveness/readiness probe is long.
type UpstreamHealthCheck struct {
Active *UpstreamActiveHealthCheck `json:"active" yaml:"active"`
Passive *UpstreamPassiveHealthCheck `json:"passive,omitempty" yaml:"passive,omitempty"`
}
// UpstreamActiveHealthCheck defines the active kind of upstream health check.
type UpstreamActiveHealthCheck struct {
Type string `json:"type,omitempty" yaml:"type,omitempty"`
Timeout *int `json:"timeout,omitempty" yaml:"timeout,omitempty"`
Concurrency *int `json:"concurrency,omitempty" yaml:"concurrency,omitempty"`
Host string `json:"host,omitempty" yaml:"host,omitempty"`
Port int32 `json:"port,omitempty" yaml:"port,omitempty"`
HTTPPath string `json:"http_path,omitempty" yaml:"http_path,omitempty"`
HTTPSVerifyCert *bool `json:"https_verify_certificate,omitempty" yaml:"https_verify_certificate,omitempty"`
HTTPRequestHeaders []string `json:"req_headers,omitempty" yaml:"req_headers,omitempty"`
Healthy UpstreamActiveHealthCheckHealthy `json:"healthy,omitempty" yaml:"healthy,omitempty"`
Unhealthy UpstreamActiveHealthCheckUnhealthy `json:"unhealthy,omitempty" yaml:"unhealthy,omitempty"`
}
// UpstreamPassiveHealthCheck defines the passive kind of upstream health check.
type UpstreamPassiveHealthCheck struct {
Type string `json:"type,omitempty" yaml:"type,omitempty"`
Healthy UpstreamPassiveHealthCheckHealthy `json:"healthy,omitempty" yaml:"healthy,omitempty"`
Unhealthy UpstreamPassiveHealthCheckUnhealthy `json:"unhealthy,omitempty" yaml:"unhealthy,omitempty"`
}
// UpstreamActiveHealthCheckHealthy defines the conditions to judge whether
// an upstream node is healthy with the active manner.
type UpstreamActiveHealthCheckHealthy struct {
UpstreamPassiveHealthCheckHealthy `json:",inline" yaml:",inline"`
Interval *int `json:"interval,omitempty" yaml:"interval,omitempty"`
}
// UpstreamPassiveHealthCheckHealthy defines the conditions to judge whether
// an upstream node is healthy with the passive manner.
type UpstreamPassiveHealthCheckHealthy struct {
HTTPStatuses []int `json:"http_statuses,omitempty" yaml:"http_statuses,omitempty"`
Successes *int `json:"successes,omitempty" yaml:"successes,omitempty"`
}
// UpstreamActiveHealthCheckUnhealthy defines the conditions to judge whether
// an upstream node is unhealthy with the active manager.
type UpstreamActiveHealthCheckUnhealthy struct {
UpstreamPassiveHealthCheckUnhealthy `json:",inline" yaml:",inline"`
Interval *int `json:"interval,omitempty" yaml:"interval,omitempty"`
}
// UpstreamPassiveHealthCheckUnhealthy defines the conditions to judge whether
// an upstream node is unhealthy with the passive manager.
type UpstreamPassiveHealthCheckUnhealthy struct {
HTTPStatuses []int `json:"http_statuses,omitempty" yaml:"http_statuses,omitempty"`
HTTPFailures *int `json:"http_failures,omitempty" yaml:"http_failures,omitempty"`
TCPFailures *int `json:"tcp_failures,omitempty" yaml:"tcp_failures,omitempty"`
Timeouts *int `json:"timeouts,omitempty" yaml:"timeouts,omitempty"`
}
// ClientTLS is tls cert and key use in mTLS
type ClientTLS struct {
Cert string `json:"client_cert,omitempty" yaml:"client_cert,omitempty"`
Key string `json:"client_key,omitempty" yaml:"client_key,omitempty"`
// ClientCertID is the referenced SSL id, can't be used with client_cert and client_key
ClientCertID string `json:"client_cert_id,omitempty"`
// Verify Turn on server certificate verification, currently only kafka upstream is supported
Verify *bool `json:"verify,omitempty" yaml:"verify,omitempty"`
}
// UpstreamTimeout represents the timeout settings on Upstream.
type UpstreamTimeout struct {
// Connect is the connect timeout
Connect int `json:"connect" yaml:"connect"`
// Send is the send timeout
Send int `json:"send" yaml:"send"`
// Read is the read timeout
Read int `json:"read" yaml:"read"`
}
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
}
type Plugin map[string]interface{}
type Plugins map[string]Plugin
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 := new(Plugins)
p.DeepCopyInto(out)
return out
}
func (p *Plugins) UnmarshalJSON(cont []byte) error {
var plugins map[string]Plugin
err := json.Unmarshal(cont, &plugins)
if err != nil {
return err
}
if p == nil || *p == nil {
*p = make(Plugins)
}
for name, config := range plugins {
(*p)[name] = GetPluginDefaultValues(name, config)
}
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 []string `json:"-"`
}
func (s *StringOrSlice) MarshalJSON() ([]byte, error) {
var (
p []byte
err error
)
if s.SliceVal != nil {
p, err = json.Marshal(s.SliceVal)
} else {
p, err = json.Marshal(s.StrVal)
}
return p, err
}
func (s *StringOrSlice) UnmarshalJSON(p []byte) error {
var err error
if len(p) == 0 {
return errors.New("empty object")
}
if p[0] == '[' {
err = json.Unmarshal(p, &s.SliceVal)
} else {
err = json.Unmarshal(p, &s.StrVal)
}
return err
}
// Consumer represents the consumer object in APISIX.
type Consumer struct {
Username string `json:"username" yaml:"username"`
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
Labels Labels `json:"labels,omitempty" yaml:"labels,omitempty"`
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
GroupID string `json:"group_id,omitempty" yaml:"group_id,omitempty"`
}
func (c *Consumer) GetLabels() Labels {
return c.Labels
}
func (c *Consumer) SetLabel(k, v string) {
if c.Labels == nil {
c.Labels = map[string]string{}
}
c.Labels[k] = v
}
// SSL represents the ssl object in APISIX.
type SSL struct {
ID string `json:"id" yaml:"id"`
Labels Labels `json:"labels,omitempty" yaml:"labels,omitempty"`
Type string `json:"type,omitempty" yaml:"type,omitempty"`
SNI string `json:"sni" yaml:"sni"`
SNIs []string `json:"snis" yaml:"snis"`
Cert string `json:"cert,omitempty" yaml:"cert,omitempty"`
Key string `json:"key,omitempty" yaml:"key,omitempty"`
Certs []string `json:"certs,omitempty" yaml:"certs,omitempty"`
Keys []string `json:"keys,omitempty" yaml:"keys,omitempty"`
Client *MutualTLSClientConfig `json:"client,omitempty" yaml:"client,omitempty"`
ExpTime int `json:"exptime,omitempty" yaml:"exptime,omitempty"`
Status *int `json:"status,omitempty" yaml:"status,omitempty"`
SSLProtocols []string `json:"ssl_protocols,omitempty" yaml:"ssl_protocols,omitempty"`
ValidityStart int `json:"validity_start,omitempty" yaml:"validity_start,omitempty"`
ValidityEnd int `json:"validity_end,omitempty" yaml:"validity_end,omitempty"`
}
func (ssl *SSL) GetLabels() Labels {
return ssl.Labels
}
func (ssl *SSL) SetLabel(k, v string) {
if ssl.Labels == nil {
ssl.Labels = map[string]string{}
}
ssl.Labels[k] = v
}
func (ssl *SSL) UnmarshalJSON(cont []byte) error {
type unmarshalerSSL SSL
var route unmarshalerSSL
err := json.Unmarshal(cont, &route)
if err != nil {
return err
}
*ssl = SSL(route)
SetSSLDefaultValues(ssl)
return nil
}
// MutualTLSClientConfig apisix SSL client field
type MutualTLSClientConfig struct {
CA string `json:"ca,omitempty" yaml:"ca,omitempty"`
Depth *int `json:"depth,omitempty" yaml:"depth,omitempty"`
SkipMtlsUriRegex []string `json:"skip_mtls_uri_regex,omitempty" yaml:"skip_mtls_uri_regex,omitempty"`
}
// GlobalRule represents the global_rule object in APISIX.
type GlobalRule struct {
ID string `json:"id" yaml:"id"`
Plugins Plugins `json:"plugins" yaml:"plugins"`
}
// PluginConfig apisix plugin object
type PluginConfig struct {
ID string `json:"id,omitempty" yaml:"id,omitempty"`
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
Labels Labels `json:"labels,omitempty" yaml:"labels,omitempty"`
Plugins Plugins `json:"plugins" yaml:"plugins"`
}
func (p *PluginConfig) GetLabels() Labels {
return p.Labels
}
func (p *PluginConfig) SetLabel(k, v string) {
if p.Labels == nil {
p.Labels = map[string]string{}
}
p.Labels[k] = v
}
// ConsumerGroup apisix consumer group object
type ConsumerGroup struct {
ID string `json:"id,omitempty" yaml:"id,omitempty"`
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
Labels Labels `json:"labels,omitempty" yaml:"labels,omitempty"`
Plugins Plugins `json:"plugins" yaml:"plugins"`
}
func (c *ConsumerGroup) GetLabels() Labels {
return c.Labels
}
func (c *ConsumerGroup) SetLabel(k, v string) {
if c.Labels == nil {
c.Labels = map[string]string{}
}
c.Labels[k] = v
}
const (
UpstreamPassHostPass = "pass"
)
type PluginMetadata struct {
ID string `json:"id,omitempty" yaml:"id,omitempty"`
Config map[string]interface{} `json:",inline" yaml:",inline"`
}
func (s *PluginMetadata) MarshalJSON() ([]byte, error) {
if s.Config == nil {
s.Config = make(map[string]interface{})
}
s.Config["id"] = s.ID
p, err := json.Marshal(s.Config)
delete(s.Config, "id")
return p, err
}
func (s *PluginMetadata) UnmarshalJSON(p []byte) error {
var (
config map[string]interface{}
err error
)
err = json.Unmarshal(p, &config)
if err != nil {
return err
}
id, ok := config["id"]
if ok {
if reflect.TypeOf(id).Kind() != reflect.String {
return errors.New("plugin metadata id is not a string, input: " + string(p))
}
s.ID = fmt.Sprintf("%v", id)
delete(config, "id")
}
s.Config = config
return nil
}
// StreamRoute represents the stream_route object in APISIX.
type StreamRoute struct {
ID string `json:"id,omitempty" yaml:"id,omitempty"`
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
Labels Labels `json:"labels,omitempty" yaml:"labels,omitempty"`
RemoteAddr string `json:"remote_addr,omitempty" yaml:"remote_addr,omitempty"`
ServerAddr string `json:"server_addr,omitempty" yaml:"server_addr,omitempty"`
ServerPort int `json:"server_port,omitempty" yaml:"server_port,omitempty"`
SNI string `json:"sni,omitempty" yaml:"sni,omitempty"`
Upstream *Upstream `json:"upstream,omitempty" yaml:"upstream,omitempty"`
UpstreamID string `json:"upstream_id,omitempty" yaml:"upstream_id,omitempty"`
ServiceID string `json:"service_id,omitempty" yaml:"service_id,omitempty"`
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
// Protocol
}
func (s *StreamRoute) GetLabels() Labels {
return s.Labels
}
func (s *StreamRoute) SetLabel(k, v string) {
if s.Labels == nil {
s.Labels = map[string]string{}
}
s.Labels[k] = v
}
func (s *StreamRoute) UnmarshalJSON(cont []byte) error {
type unmarshalerStreamRoute StreamRoute
var route unmarshalerStreamRoute
err := json.Unmarshal(cont, &route)
if err != nil {
return err
}
*s = StreamRoute(route)
SetStreamRouteDefaultValues(s)
return nil
}