gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/xds/internal/testutils/protos.go (about)

     1  /*
     2   *
     3   * Copyright 2020 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 testutils
    19  
    20  import (
    21  	"net"
    22  	"strconv"
    23  
    24  	v2xdspb "gitee.com/ks-custle/core-gm/go-control-plane/envoy/api/v2"
    25  	v2corepb "gitee.com/ks-custle/core-gm/go-control-plane/envoy/api/v2/core"
    26  	v2endpointpb "gitee.com/ks-custle/core-gm/go-control-plane/envoy/api/v2/endpoint"
    27  	v3corepb "gitee.com/ks-custle/core-gm/go-control-plane/envoy/config/core/v3"
    28  	v2typepb "gitee.com/ks-custle/core-gm/go-control-plane/envoy/type"
    29  	"gitee.com/ks-custle/core-gm/grpc/xds/internal"
    30  	wrapperspb "github.com/golang/protobuf/ptypes/wrappers"
    31  )
    32  
    33  // EmptyNodeProtoV2 is a v2 Node proto with no fields set.
    34  var EmptyNodeProtoV2 = &v2corepb.Node{}
    35  
    36  // EmptyNodeProtoV3 is a v3 Node proto with no fields set.
    37  var EmptyNodeProtoV3 = &v3corepb.Node{}
    38  
    39  // LocalityIDToProto converts a LocalityID to its proto representation.
    40  func LocalityIDToProto(l internal.LocalityID) *v2corepb.Locality {
    41  	return &v2corepb.Locality{
    42  		Region:  l.Region,
    43  		Zone:    l.Zone,
    44  		SubZone: l.SubZone,
    45  	}
    46  }
    47  
    48  // The helper structs/functions related to EDS protos are used in EDS balancer
    49  // tests now, to generate test inputs. Eventually, EDS balancer tests should
    50  // generate EndpointsUpdate directly, instead of generating and parsing the
    51  // proto message.
    52  // TODO: Once EDS balancer tests don't use these, these can be moved to v2 client code.
    53  
    54  // ClusterLoadAssignmentBuilder builds a ClusterLoadAssignment, aka EDS
    55  // response.
    56  type ClusterLoadAssignmentBuilder struct {
    57  	v *v2xdspb.ClusterLoadAssignment
    58  }
    59  
    60  // NewClusterLoadAssignmentBuilder creates a ClusterLoadAssignmentBuilder.
    61  func NewClusterLoadAssignmentBuilder(clusterName string, dropPercents map[string]uint32) *ClusterLoadAssignmentBuilder {
    62  	drops := make([]*v2xdspb.ClusterLoadAssignment_Policy_DropOverload, 0, len(dropPercents))
    63  	for n, d := range dropPercents {
    64  		drops = append(drops, &v2xdspb.ClusterLoadAssignment_Policy_DropOverload{
    65  			Category: n,
    66  			DropPercentage: &v2typepb.FractionalPercent{
    67  				Numerator:   d,
    68  				Denominator: v2typepb.FractionalPercent_HUNDRED,
    69  			},
    70  		})
    71  	}
    72  
    73  	return &ClusterLoadAssignmentBuilder{
    74  		v: &v2xdspb.ClusterLoadAssignment{
    75  			ClusterName: clusterName,
    76  			Policy: &v2xdspb.ClusterLoadAssignment_Policy{
    77  				DropOverloads: drops,
    78  			},
    79  		},
    80  	}
    81  }
    82  
    83  // AddLocalityOptions contains options when adding locality to the builder.
    84  type AddLocalityOptions struct {
    85  	Health []v2corepb.HealthStatus
    86  	Weight []uint32
    87  }
    88  
    89  // AddLocality adds a locality to the builder.
    90  func (clab *ClusterLoadAssignmentBuilder) AddLocality(subzone string, weight uint32, priority uint32, addrsWithPort []string, opts *AddLocalityOptions) {
    91  	lbEndPoints := make([]*v2endpointpb.LbEndpoint, 0, len(addrsWithPort))
    92  	for i, a := range addrsWithPort {
    93  		host, portStr, err := net.SplitHostPort(a)
    94  		if err != nil {
    95  			panic("failed to split " + a)
    96  		}
    97  		port, err := strconv.Atoi(portStr)
    98  		if err != nil {
    99  			panic("failed to atoi " + portStr)
   100  		}
   101  
   102  		lbe := &v2endpointpb.LbEndpoint{
   103  			HostIdentifier: &v2endpointpb.LbEndpoint_Endpoint{
   104  				Endpoint: &v2endpointpb.Endpoint{
   105  					Address: &v2corepb.Address{
   106  						Address: &v2corepb.Address_SocketAddress{
   107  							SocketAddress: &v2corepb.SocketAddress{
   108  								Protocol: v2corepb.SocketAddress_TCP,
   109  								Address:  host,
   110  								PortSpecifier: &v2corepb.SocketAddress_PortValue{
   111  									PortValue: uint32(port)}}}}}},
   112  		}
   113  		if opts != nil {
   114  			if i < len(opts.Health) {
   115  				lbe.HealthStatus = opts.Health[i]
   116  			}
   117  			if i < len(opts.Weight) {
   118  				lbe.LoadBalancingWeight = &wrapperspb.UInt32Value{Value: opts.Weight[i]}
   119  			}
   120  		}
   121  		lbEndPoints = append(lbEndPoints, lbe)
   122  	}
   123  
   124  	var localityID *v2corepb.Locality
   125  	if subzone != "" {
   126  		localityID = &v2corepb.Locality{
   127  			Region:  "",
   128  			Zone:    "",
   129  			SubZone: subzone,
   130  		}
   131  	}
   132  
   133  	clab.v.Endpoints = append(clab.v.Endpoints, &v2endpointpb.LocalityLbEndpoints{
   134  		Locality:            localityID,
   135  		LbEndpoints:         lbEndPoints,
   136  		LoadBalancingWeight: &wrapperspb.UInt32Value{Value: weight},
   137  		Priority:            priority,
   138  	})
   139  }
   140  
   141  // Build builds ClusterLoadAssignment.
   142  func (clab *ClusterLoadAssignmentBuilder) Build() *v2xdspb.ClusterLoadAssignment {
   143  	return clab.v
   144  }