rls.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. *
  3. * Copyright 2021 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 rls implements the RLS cluster specifier plugin.
  19. package rls
  20. import (
  21. "encoding/json"
  22. "fmt"
  23. "github.com/golang/protobuf/proto"
  24. "github.com/golang/protobuf/ptypes"
  25. "google.golang.org/grpc/balancer"
  26. "google.golang.org/grpc/internal"
  27. "google.golang.org/grpc/internal/envconfig"
  28. rlspb "google.golang.org/grpc/internal/proto/grpc_lookup_v1"
  29. "google.golang.org/grpc/xds/internal/clusterspecifier"
  30. "google.golang.org/protobuf/encoding/protojson"
  31. "google.golang.org/protobuf/types/known/anypb"
  32. )
  33. func init() {
  34. if envconfig.XDSRLS {
  35. clusterspecifier.Register(rls{})
  36. }
  37. // TODO: Remove these once the RLS env var is removed.
  38. internal.RegisterRLSClusterSpecifierPluginForTesting = func() {
  39. clusterspecifier.Register(rls{})
  40. }
  41. internal.UnregisterRLSClusterSpecifierPluginForTesting = func() {
  42. for _, typeURL := range rls.TypeURLs(rls{}) {
  43. clusterspecifier.UnregisterForTesting(typeURL)
  44. }
  45. }
  46. }
  47. type rls struct{}
  48. func (rls) TypeURLs() []string {
  49. return []string{"type.googleapis.com/grpc.lookup.v1.RouteLookupClusterSpecifier"}
  50. }
  51. // lbConfigJSON is the RLS LB Policies configuration in JSON format.
  52. // RouteLookupConfig will be a raw JSON string from the passed in proto
  53. // configuration, and the other fields will be hardcoded.
  54. type lbConfigJSON struct {
  55. RouteLookupConfig json.RawMessage `json:"routeLookupConfig"`
  56. ChildPolicy []map[string]json.RawMessage `json:"childPolicy"`
  57. ChildPolicyConfigTargetFieldName string `json:"childPolicyConfigTargetFieldName"`
  58. }
  59. func (rls) ParseClusterSpecifierConfig(cfg proto.Message) (clusterspecifier.BalancerConfig, error) {
  60. if cfg == nil {
  61. return nil, fmt.Errorf("rls_csp: nil configuration message provided")
  62. }
  63. any, ok := cfg.(*anypb.Any)
  64. if !ok {
  65. return nil, fmt.Errorf("rls_csp: error parsing config %v: unknown type %T", cfg, cfg)
  66. }
  67. rlcs := new(rlspb.RouteLookupClusterSpecifier)
  68. if err := ptypes.UnmarshalAny(any, rlcs); err != nil {
  69. return nil, fmt.Errorf("rls_csp: error parsing config %v: %v", cfg, err)
  70. }
  71. rlcJSON, err := protojson.Marshal(rlcs.GetRouteLookupConfig())
  72. if err != nil {
  73. return nil, fmt.Errorf("rls_csp: error marshaling route lookup config: %v: %v", rlcs.GetRouteLookupConfig(), err)
  74. }
  75. lbCfgJSON := &lbConfigJSON{
  76. RouteLookupConfig: rlcJSON, // "JSON form of RouteLookupClusterSpecifier.config" - RLS in xDS Design Doc
  77. ChildPolicy: []map[string]json.RawMessage{
  78. {
  79. "cds_experimental": json.RawMessage("{}"),
  80. },
  81. },
  82. ChildPolicyConfigTargetFieldName: "cluster",
  83. }
  84. rawJSON, err := json.Marshal(lbCfgJSON)
  85. if err != nil {
  86. return nil, fmt.Errorf("rls_csp: error marshaling load balancing config %v: %v", lbCfgJSON, err)
  87. }
  88. rlsBB := balancer.Get(internal.RLSLoadBalancingPolicyName)
  89. if rlsBB == nil {
  90. return nil, fmt.Errorf("RLS LB policy not registered")
  91. }
  92. if _, err = rlsBB.(balancer.ConfigParser).ParseConfig(rawJSON); err != nil {
  93. return nil, fmt.Errorf("rls_csp: validation error from rls lb policy parsing: %v", err)
  94. }
  95. return clusterspecifier.BalancerConfig{{internal.RLSLoadBalancingPolicyName: lbCfgJSON}}, nil
  96. }