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