14
14
* limitations under the License.
15
15
*/
16
16
17
- // Package xds contains methods to Get/Set handshake cluster names. It is separated
18
- // out from the top level /internal package to avoid circular dependencies.
17
+ // Package xds contains functions, structs, and utilities for working with
18
+ // handshake cluster names, as well as shared components used by xds balancers
19
+ // and resolvers. It is separated from the top-level /internal package to
20
+ // avoid circular dependencies.
19
21
package xds
20
22
21
23
import (
24
+ "fmt"
25
+
22
26
"google.golang.org/grpc/attributes"
27
+ "google.golang.org/grpc/internal/xds/clients"
23
28
"google.golang.org/grpc/resolver"
24
29
)
25
30
@@ -40,3 +45,60 @@ func GetXDSHandshakeClusterName(attr *attributes.Attributes) (string, bool) {
40
45
name , ok := v .(string )
41
46
return name , ok
42
47
}
48
+
49
+ // LocalityString generates a string representation of clients.Locality in the
50
+ // format specified in gRFC A76.
51
+ func LocalityString (l clients.Locality ) string {
52
+ return fmt .Sprintf ("{region=%q, zone=%q, sub_zone=%q}" , l .Region , l .Zone , l .SubZone )
53
+ }
54
+
55
+ // IsLocalityEqual allows the values to be compared by Attributes.Equal.
56
+ func IsLocalityEqual (l clients.Locality , o any ) bool {
57
+ ol , ok := o .(clients.Locality )
58
+ if ! ok {
59
+ return false
60
+ }
61
+ return l .Region == ol .Region && l .Zone == ol .Zone && l .SubZone == ol .SubZone
62
+ }
63
+
64
+ // LocalityFromString converts a string representation of clients.locality as
65
+ // specified in gRFC A76, into a LocalityID struct.
66
+ func LocalityFromString (s string ) (ret clients.Locality , _ error ) {
67
+ _ , err := fmt .Sscanf (s , "{region=%q, zone=%q, sub_zone=%q}" , & ret .Region , & ret .Zone , & ret .SubZone )
68
+ if err != nil {
69
+ return clients.Locality {}, fmt .Errorf ("%s is not a well formatted locality ID, error: %v" , s , err )
70
+ }
71
+ return ret , nil
72
+ }
73
+
74
+ type localityKeyType string
75
+
76
+ const localityKey = localityKeyType ("grpc.xds.internal.address.locality" )
77
+
78
+ // GetLocalityID returns the locality ID of addr.
79
+ func GetLocalityID (addr resolver.Address ) clients.Locality {
80
+ path , _ := addr .BalancerAttributes .Value (localityKey ).(clients.Locality )
81
+ return path
82
+ }
83
+
84
+ // SetLocalityID sets locality ID in addr to l.
85
+ func SetLocalityID (addr resolver.Address , l clients.Locality ) resolver.Address {
86
+ addr .BalancerAttributes = addr .BalancerAttributes .WithValue (localityKey , l )
87
+ return addr
88
+ }
89
+
90
+ // SetLocalityIDInEndpoint sets locality ID in endpoint to l.
91
+ func SetLocalityIDInEndpoint (endpoint resolver.Endpoint , l clients.Locality ) resolver.Endpoint {
92
+ endpoint .Attributes = endpoint .Attributes .WithValue (localityKey , l )
93
+ return endpoint
94
+ }
95
+
96
+ // ResourceTypeMapForTesting maps TypeUrl to corresponding ResourceType.
97
+ var ResourceTypeMapForTesting map [string ]any
98
+
99
+ // UnknownCSMLabels are TelemetryLabels emitted from CDS if CSM Telemetry Label
100
+ // data is not present in the CDS Resource.
101
+ var UnknownCSMLabels = map [string ]string {
102
+ "csm.service_name" : "unknown" ,
103
+ "csm.service_namespace_name" : "unknown" ,
104
+ }
0 commit comments