internal.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // Package internal contains functions/structs shared by xds
  18. // balancers/resolvers.
  19. package internal
  20. import (
  21. "encoding/json"
  22. "fmt"
  23. "google.golang.org/grpc/resolver"
  24. )
  25. // LocalityID is xds.Locality without XXX fields, so it can be used as map
  26. // keys.
  27. //
  28. // xds.Locality cannot be map keys because one of the XXX fields is a slice.
  29. type LocalityID struct {
  30. Region string `json:"region,omitempty"`
  31. Zone string `json:"zone,omitempty"`
  32. SubZone string `json:"subZone,omitempty"`
  33. }
  34. // ToString generates a string representation of LocalityID by marshalling it into
  35. // json. Not calling it String() so printf won't call it.
  36. func (l LocalityID) ToString() (string, error) {
  37. b, err := json.Marshal(l)
  38. if err != nil {
  39. return "", err
  40. }
  41. return string(b), nil
  42. }
  43. // Equal allows the values to be compared by Attributes.Equal.
  44. func (l LocalityID) Equal(o interface{}) bool {
  45. ol, ok := o.(LocalityID)
  46. if !ok {
  47. return false
  48. }
  49. return l.Region == ol.Region && l.Zone == ol.Zone && l.SubZone == ol.SubZone
  50. }
  51. // LocalityIDFromString converts a json representation of locality, into a
  52. // LocalityID struct.
  53. func LocalityIDFromString(s string) (ret LocalityID, _ error) {
  54. err := json.Unmarshal([]byte(s), &ret)
  55. if err != nil {
  56. return LocalityID{}, fmt.Errorf("%s is not a well formatted locality ID, error: %v", s, err)
  57. }
  58. return ret, nil
  59. }
  60. type localityKeyType string
  61. const localityKey = localityKeyType("grpc.xds.internal.address.locality")
  62. // GetLocalityID returns the locality ID of addr.
  63. func GetLocalityID(addr resolver.Address) LocalityID {
  64. path, _ := addr.BalancerAttributes.Value(localityKey).(LocalityID)
  65. return path
  66. }
  67. // SetLocalityID sets locality ID in addr to l.
  68. func SetLocalityID(addr resolver.Address, l LocalityID) resolver.Address {
  69. addr.BalancerAttributes = addr.BalancerAttributes.WithValue(localityKey, l)
  70. return addr
  71. }
  72. // ResourceTypeMapForTesting maps TypeUrl to corresponding ResourceType.
  73. var ResourceTypeMapForTesting map[string]interface{}