github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/balancer/rls/internal/test/e2e/rls_lb_config.go (about) 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 19 package e2e 20 21 import ( 22 "errors" 23 "fmt" 24 25 "github.com/hxx258456/ccgo/grpc/balancer" 26 rlspb "github.com/hxx258456/ccgo/grpc/internal/proto/grpc_lookup_v1" 27 internalserviceconfig "github.com/hxx258456/ccgo/grpc/internal/serviceconfig" 28 "github.com/hxx258456/ccgo/grpc/serviceconfig" 29 30 "google.golang.org/protobuf/encoding/protojson" 31 ) 32 33 // RLSConfig is a utility type to build service config for the RLS LB policy. 34 type RLSConfig struct { 35 RouteLookupConfig *rlspb.RouteLookupConfig 36 ChildPolicy *internalserviceconfig.BalancerConfig 37 ChildPolicyConfigTargetFieldName string 38 } 39 40 // ServiceConfigJSON generates service config with a load balancing config 41 // corresponding to the RLS LB policy. 42 func (c *RLSConfig) ServiceConfigJSON() (string, error) { 43 m := protojson.MarshalOptions{ 44 Multiline: true, 45 Indent: " ", 46 UseProtoNames: true, 47 } 48 routeLookupCfg, err := m.Marshal(c.RouteLookupConfig) 49 if err != nil { 50 return "", err 51 } 52 childPolicy, err := c.ChildPolicy.MarshalJSON() 53 if err != nil { 54 return "", err 55 } 56 57 return fmt.Sprintf(` 58 { 59 "loadBalancingConfig": [ 60 { 61 "rls_experimental": { 62 "routeLookupConfig": %s, 63 "childPolicy": %s, 64 "childPolicyConfigTargetFieldName": %q 65 } 66 } 67 ] 68 }`, string(routeLookupCfg), string(childPolicy), c.ChildPolicyConfigTargetFieldName), nil 69 } 70 71 // LoadBalancingConfig generates load balancing config which can used as part of 72 // a ClientConnState update to the RLS LB policy. 73 func (c *RLSConfig) LoadBalancingConfig() (serviceconfig.LoadBalancingConfig, error) { 74 m := protojson.MarshalOptions{ 75 Multiline: true, 76 Indent: " ", 77 UseProtoNames: true, 78 } 79 routeLookupCfg, err := m.Marshal(c.RouteLookupConfig) 80 if err != nil { 81 return nil, err 82 } 83 childPolicy, err := c.ChildPolicy.MarshalJSON() 84 if err != nil { 85 return nil, err 86 } 87 lbConfigJSON := fmt.Sprintf(` 88 { 89 "routeLookupConfig": %s, 90 "childPolicy": %s, 91 "childPolicyConfigTargetFieldName": %q 92 }`, string(routeLookupCfg), string(childPolicy), c.ChildPolicyConfigTargetFieldName) 93 94 builder := balancer.Get("rls_experimental") 95 if builder == nil { 96 return nil, errors.New("balancer builder not found for RLS LB policy") 97 } 98 parser := builder.(balancer.ConfigParser) 99 return parser.ParseConfig([]byte(lbConfigJSON)) 100 }