...
1
17
18 package clusterresolver
19
20 import (
21 "bytes"
22 "encoding/json"
23 "fmt"
24
25 internalserviceconfig "google.golang.org/grpc/internal/serviceconfig"
26 "google.golang.org/grpc/internal/xds/bootstrap"
27 "google.golang.org/grpc/serviceconfig"
28 "google.golang.org/grpc/xds/internal/balancer/outlierdetection"
29 )
30
31
32 type DiscoveryMechanismType int
33
34 const (
35
36 DiscoveryMechanismTypeEDS DiscoveryMechanismType = iota
37
38 DiscoveryMechanismTypeLogicalDNS
39 )
40
41
42
43
44
45
46
47 func (t DiscoveryMechanismType) MarshalJSON() ([]byte, error) {
48 buffer := bytes.NewBufferString(`"`)
49 switch t {
50 case DiscoveryMechanismTypeEDS:
51 buffer.WriteString("EDS")
52 case DiscoveryMechanismTypeLogicalDNS:
53 buffer.WriteString("LOGICAL_DNS")
54 }
55 buffer.WriteString(`"`)
56 return buffer.Bytes(), nil
57 }
58
59
60 func (t *DiscoveryMechanismType) UnmarshalJSON(b []byte) error {
61 var s string
62 err := json.Unmarshal(b, &s)
63 if err != nil {
64 return err
65 }
66 switch s {
67 case "EDS":
68 *t = DiscoveryMechanismTypeEDS
69 case "LOGICAL_DNS":
70 *t = DiscoveryMechanismTypeLogicalDNS
71 default:
72 return fmt.Errorf("unable to unmarshal string %q to type DiscoveryMechanismType", s)
73 }
74 return nil
75 }
76
77
78
79
80
81
82
83 type DiscoveryMechanism struct {
84
85 Cluster string `json:"cluster,omitempty"`
86
87
88 LoadReportingServer *bootstrap.ServerConfig `json:"lrsLoadReportingServer,omitempty"`
89
90
91 MaxConcurrentRequests *uint32 `json:"maxConcurrentRequests,omitempty"`
92
93 Type DiscoveryMechanismType `json:"type,omitempty"`
94
95
96
97
98
99 EDSServiceName string `json:"edsServiceName,omitempty"`
100
101
102 DNSHostname string `json:"dnsHostname,omitempty"`
103
104
105 OutlierDetection json.RawMessage `json:"outlierDetection,omitempty"`
106
107 TelemetryLabels map[string]string `json:"telemetryLabels,omitempty"`
108 outlierDetection outlierdetection.LBConfig
109 }
110
111
112 func (dm DiscoveryMechanism) Equal(b DiscoveryMechanism) bool {
113 od := &dm.outlierDetection
114 switch {
115 case dm.Cluster != b.Cluster:
116 return false
117 case !equalUint32P(dm.MaxConcurrentRequests, b.MaxConcurrentRequests):
118 return false
119 case dm.Type != b.Type:
120 return false
121 case dm.EDSServiceName != b.EDSServiceName:
122 return false
123 case dm.DNSHostname != b.DNSHostname:
124 return false
125 case !od.EqualIgnoringChildPolicy(&b.outlierDetection):
126 return false
127 }
128
129 if dm.LoadReportingServer == nil && b.LoadReportingServer == nil {
130 return true
131 }
132 if (dm.LoadReportingServer != nil) != (b.LoadReportingServer != nil) {
133 return false
134 }
135 return dm.LoadReportingServer.String() == b.LoadReportingServer.String()
136 }
137
138 func equalUint32P(a, b *uint32) bool {
139 if a == nil && b == nil {
140 return true
141 }
142 if a == nil || b == nil {
143 return false
144 }
145 return *a == *b
146 }
147
148
149 type LBConfig struct {
150 serviceconfig.LoadBalancingConfig `json:"-"`
151
152
153
154
155 DiscoveryMechanisms []DiscoveryMechanism `json:"discoveryMechanisms,omitempty"`
156
157
158 XDSLBPolicy json.RawMessage `json:"xdsLbPolicy,omitempty"`
159 xdsLBPolicy internalserviceconfig.BalancerConfig
160 }
161
View as plain text