Skip to content

Commit b9f518f

Browse files
committed
refactor: move internal.go and internal_test.go into xds.go/xds_test.go
Per code review feedback, consolidated the contents of internal/xds/internal.go into internal/xds/xds.go and internal/xds/internal_test.go into internal/xds/xds_test.go to keep related functionality in a single file and improve code organization.
1 parent c503412 commit b9f518f

File tree

3 files changed

+64
-86
lines changed

3 files changed

+64
-86
lines changed

internal/xds/internal.go

Lines changed: 0 additions & 84 deletions
This file was deleted.

internal/xds/xds.go

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

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.
1921
package xds
2022

2123
import (
24+
"fmt"
25+
2226
"google.golang.org/grpc/attributes"
27+
"google.golang.org/grpc/internal/xds/clients"
2328
"google.golang.org/grpc/resolver"
2429
)
2530

@@ -40,3 +45,60 @@ func GetXDSHandshakeClusterName(attr *attributes.Attributes) (string, bool) {
4045
name, ok := v.(string)
4146
return name, ok
4247
}
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+
}
File renamed without changes.

0 commit comments

Comments
 (0)