google.golang.org/grpc@v1.74.2/xds/internal/internal.go (about) 1 /* 2 * 3 * Copyright 2019 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 // Package internal contains functions/structs shared by xds 19 // balancers/resolvers. 20 package internal 21 22 import ( 23 "fmt" 24 25 "google.golang.org/grpc/resolver" 26 "google.golang.org/grpc/xds/internal/clients" 27 ) 28 29 // LocalityString generates a string representation of clients.Locality in the 30 // format specified in gRFC A76. 31 func LocalityString(l clients.Locality) string { 32 return fmt.Sprintf("{region=%q, zone=%q, sub_zone=%q}", l.Region, l.Zone, l.SubZone) 33 } 34 35 // IsLocalityEqual allows the values to be compared by Attributes.Equal. 36 func IsLocalityEqual(l clients.Locality, o any) bool { 37 ol, ok := o.(clients.Locality) 38 if !ok { 39 return false 40 } 41 return l.Region == ol.Region && l.Zone == ol.Zone && l.SubZone == ol.SubZone 42 } 43 44 // LocalityFromString converts a string representation of clients.locality as 45 // specified in gRFC A76, into a LocalityID struct. 46 func LocalityFromString(s string) (ret clients.Locality, _ error) { 47 _, err := fmt.Sscanf(s, "{region=%q, zone=%q, sub_zone=%q}", &ret.Region, &ret.Zone, &ret.SubZone) 48 if err != nil { 49 return clients.Locality{}, fmt.Errorf("%s is not a well formatted locality ID, error: %v", s, err) 50 } 51 return ret, nil 52 } 53 54 type localityKeyType string 55 56 const localityKey = localityKeyType("grpc.xds.internal.address.locality") 57 58 // GetLocalityID returns the locality ID of addr. 59 func GetLocalityID(addr resolver.Address) clients.Locality { 60 path, _ := addr.BalancerAttributes.Value(localityKey).(clients.Locality) 61 return path 62 } 63 64 // SetLocalityID sets locality ID in addr to l. 65 func SetLocalityID(addr resolver.Address, l clients.Locality) resolver.Address { 66 addr.BalancerAttributes = addr.BalancerAttributes.WithValue(localityKey, l) 67 return addr 68 } 69 70 // SetLocalityIDInEndpoint sets locality ID in endpoint to l. 71 func SetLocalityIDInEndpoint(endpoint resolver.Endpoint, l clients.Locality) resolver.Endpoint { 72 endpoint.Attributes = endpoint.Attributes.WithValue(localityKey, l) 73 return endpoint 74 } 75 76 // ResourceTypeMapForTesting maps TypeUrl to corresponding ResourceType. 77 var ResourceTypeMapForTesting map[string]any 78 79 // UnknownCSMLabels are TelemetryLabels emitted from CDS if CSM Telemetry Label 80 // data is not present in the CDS Resource. 81 var UnknownCSMLabels = map[string]string{ 82 "csm.service_name": "unknown", 83 "csm.service_namespace_name": "unknown", 84 }