istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/networking/grpcgen/rds.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 grpcgen
    16  
    17  import (
    18  	route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
    19  	discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
    20  
    21  	"istio.io/istio/pilot/pkg/model"
    22  	"istio.io/istio/pilot/pkg/networking/core"
    23  	"istio.io/istio/pilot/pkg/util/protoconv"
    24  )
    25  
    26  // BuildHTTPRoutes supports per-VIP routes, as used by GRPC.
    27  // This mode is indicated by using names containing full host:port instead of just port.
    28  // Returns true of the request is of this type.
    29  func (g *GrpcConfigGenerator) BuildHTTPRoutes(node *model.Proxy, push *model.PushContext, routeNames []string) model.Resources {
    30  	resp := model.Resources{}
    31  	for _, routeName := range routeNames {
    32  		if rc := buildHTTPRoute(node, push, routeName); rc != nil {
    33  			resp = append(resp, &discovery.Resource{
    34  				Name:     routeName,
    35  				Resource: protoconv.MessageToAny(rc),
    36  			})
    37  		}
    38  	}
    39  	return resp
    40  }
    41  
    42  func buildHTTPRoute(node *model.Proxy, push *model.PushContext, routeName string) *route.RouteConfiguration {
    43  	// TODO use route-style naming instead of cluster naming
    44  	_, _, hostname, port := model.ParseSubsetKey(routeName)
    45  	if hostname == "" || port == 0 {
    46  		log.Warnf("failed to parse %v", routeName)
    47  		return nil
    48  	}
    49  
    50  	virtualHosts, _, _ := core.BuildSidecarOutboundVirtualHosts(node, push, routeName, port, nil, &model.DisabledCache{})
    51  
    52  	// Only generate the required route for grpc. Will need to generate more
    53  	// as GRPC adds more features.
    54  	return &route.RouteConfiguration{
    55  		Name:         routeName,
    56  		VirtualHosts: virtualHosts,
    57  	}
    58  }