google.golang.org/grpc@v1.62.1/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  	"google.golang.org/grpc/balancer"
    26  	rlspb "google.golang.org/grpc/internal/proto/grpc_lookup_v1"
    27  	internalserviceconfig "google.golang.org/grpc/internal/serviceconfig"
    28  	"google.golang.org/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  	RouteLookupChannelServiceConfig  string
    37  	ChildPolicy                      *internalserviceconfig.BalancerConfig
    38  	ChildPolicyConfigTargetFieldName string
    39  }
    40  
    41  // ServiceConfigJSON generates service config with a load balancing config
    42  // corresponding to the RLS LB policy.
    43  func (c *RLSConfig) ServiceConfigJSON() (string, error) {
    44  	m := protojson.MarshalOptions{
    45  		Multiline:     true,
    46  		Indent:        "  ",
    47  		UseProtoNames: true,
    48  	}
    49  	routeLookupCfg, err := m.Marshal(c.RouteLookupConfig)
    50  	if err != nil {
    51  		return "", err
    52  	}
    53  	childPolicy, err := c.ChildPolicy.MarshalJSON()
    54  	if err != nil {
    55  		return "", err
    56  	}
    57  
    58  	return fmt.Sprintf(`
    59  {
    60    "loadBalancingConfig": [
    61      {
    62        "rls_experimental": {
    63          "routeLookupConfig": %s,
    64  				"routeLookupChannelServiceConfig": %s,
    65          "childPolicy": %s,
    66          "childPolicyConfigTargetFieldName": %q
    67        }
    68      }
    69    ]
    70  }`, string(routeLookupCfg), c.RouteLookupChannelServiceConfig, string(childPolicy), c.ChildPolicyConfigTargetFieldName), nil
    71  }
    72  
    73  // LoadBalancingConfig generates load balancing config which can used as part of
    74  // a ClientConnState update to the RLS LB policy.
    75  func (c *RLSConfig) LoadBalancingConfig() (serviceconfig.LoadBalancingConfig, error) {
    76  	m := protojson.MarshalOptions{
    77  		Multiline:     true,
    78  		Indent:        "  ",
    79  		UseProtoNames: true,
    80  	}
    81  	routeLookupCfg, err := m.Marshal(c.RouteLookupConfig)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	childPolicy, err := c.ChildPolicy.MarshalJSON()
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	lbConfigJSON := fmt.Sprintf(`
    90  {
    91    "routeLookupConfig": %s,
    92    "routeLookupChannelServiceConfig": %s,
    93    "childPolicy": %s,
    94    "childPolicyConfigTargetFieldName": %q
    95  }`, string(routeLookupCfg), c.RouteLookupChannelServiceConfig, string(childPolicy), c.ChildPolicyConfigTargetFieldName)
    96  
    97  	builder := balancer.Get("rls_experimental")
    98  	if builder == nil {
    99  		return nil, errors.New("balancer builder not found for RLS LB policy")
   100  	}
   101  	parser := builder.(balancer.ConfigParser)
   102  	return parser.ParseConfig([]byte(lbConfigJSON))
   103  }