istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/echo/server/forwarder/xds.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package forwarder
    16  
    17  import (
    18  	"context"
    19  
    20  	"google.golang.org/grpc"
    21  	"google.golang.org/grpc/credentials"
    22  	"google.golang.org/grpc/credentials/insecure"
    23  	"google.golang.org/grpc/credentials/xds"
    24  	xdsresolver "google.golang.org/grpc/xds"
    25  
    26  	"istio.io/istio/pkg/test/echo/common"
    27  	"istio.io/istio/pkg/test/echo/proto"
    28  )
    29  
    30  var _ protocol = &grpcProtocol{}
    31  
    32  type xdsProtocol struct {
    33  	e *executor
    34  }
    35  
    36  func newXDSProtocol(e *executor) protocol {
    37  	return &xdsProtocol{e: e}
    38  }
    39  
    40  func (c *xdsProtocol) ForwardEcho(ctx context.Context, cfg *Config) (*proto.ForwardEchoResponse, error) {
    41  	var getConn grpcConnectionGetter
    42  	if cfg.newConnectionPerRequest {
    43  		// Create a new connection per request.
    44  		getConn = func() (*grpc.ClientConn, func(), error) {
    45  			conn, err := newXDSConnection(cfg)
    46  			if err != nil {
    47  				return nil, nil, err
    48  			}
    49  			return conn, func() { _ = conn.Close() }, nil
    50  		}
    51  	} else {
    52  		// Reuse the connection across all requests.
    53  		conn, err := newXDSConnection(cfg)
    54  		if err != nil {
    55  			return nil, err
    56  		}
    57  		defer func() { _ = conn.Close() }()
    58  		getConn = func() (*grpc.ClientConn, func(), error) {
    59  			return conn, func() {}, nil
    60  		}
    61  	}
    62  
    63  	call := grpcCall{
    64  		e:       c.e,
    65  		getConn: getConn,
    66  	}
    67  	return doForward(ctx, cfg, c.e, call.makeRequest)
    68  }
    69  
    70  func (c *xdsProtocol) Close() error {
    71  	return nil
    72  }
    73  
    74  func newXDSConnection(cfg *Config) (*grpc.ClientConn, error) {
    75  	var opts []grpc.DialOption
    76  
    77  	// transport security
    78  	creds, err := xds.NewClientCredentials(xds.ClientOptions{FallbackCreds: insecure.NewCredentials()})
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	security := grpc.WithTransportCredentials(creds)
    83  	if len(cfg.XDSTestBootstrap) > 0 {
    84  		r, err := xdsresolver.NewXDSResolverWithConfigForTesting(cfg.XDSTestBootstrap)
    85  		if err != nil {
    86  			return nil, err
    87  		}
    88  		opts = append(opts, grpc.WithResolvers(r))
    89  	}
    90  
    91  	if cfg.getClientCertificate != nil {
    92  		security = grpc.WithTransportCredentials(credentials.NewTLS(cfg.tlsConfig))
    93  	}
    94  
    95  	address := cfg.Request.Url
    96  
    97  	// Connect to the GRPC server.
    98  	ctx, cancel := context.WithTimeout(context.Background(), common.ConnectionTimeout)
    99  	defer cancel()
   100  	opts = append(opts, security, grpc.WithAuthority(cfg.hostHeader))
   101  	return grpc.DialContext(ctx, address, opts...)
   102  }