dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/client/controller/version/v3/client.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. 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 * 20 * Copyright 2020 gRPC authors. 21 * 22 */ 23 24 // Package v3 provides xDS v3 transport protocol specific functionality. 25 package v3 26 27 import ( 28 "context" 29 "fmt" 30 ) 31 32 import ( 33 dubbogoLogger "github.com/dubbogo/gost/log/logger" 34 35 v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" 36 v3discoverypb "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3" 37 38 "github.com/golang/protobuf/proto" 39 _struct "github.com/golang/protobuf/ptypes/struct" 40 41 statuspb "google.golang.org/genproto/googleapis/rpc/status" 42 43 "google.golang.org/grpc" 44 "google.golang.org/grpc/codes" 45 46 "google.golang.org/protobuf/types/known/anypb" 47 ) 48 49 import ( 50 controllerversion "dubbo.apache.org/dubbo-go/v3/xds/client/controller/version" 51 "dubbo.apache.org/dubbo-go/v3/xds/client/resource" 52 resourceversion "dubbo.apache.org/dubbo-go/v3/xds/client/resource/version" 53 "dubbo.apache.org/dubbo-go/v3/xds/utils/pretty" 54 ) 55 56 func init() { 57 controllerversion.RegisterAPIClientBuilder(resourceversion.TransportV3, newClient) 58 } 59 60 var ( 61 resourceTypeToURL = map[resource.ResourceType]string{ 62 resource.ListenerResource: resourceversion.V3ListenerURL, 63 resource.RouteConfigResource: resourceversion.V3RouteConfigURL, 64 resource.ClusterResource: resourceversion.V3ClusterURL, 65 resource.EndpointsResource: resourceversion.V3EndpointsURL, 66 } 67 ) 68 69 func newClient(opts controllerversion.BuildOptions) (controllerversion.MetadataWrappedVersionClient, error) { 70 nodeProto, ok := opts.NodeProto.(*v3corepb.Node) 71 if !ok { 72 return nil, fmt.Errorf("xds: unsupported Node proto type: %T, want %T", opts.NodeProto, v3corepb.Node{}) 73 } 74 v3c := &client{ 75 nodeProto: nodeProto, logger: dubbogoLogger.GetLogger(), 76 } 77 return v3c, nil 78 } 79 80 type adsStream v3discoverypb.AggregatedDiscoveryService_StreamAggregatedResourcesClient 81 82 // client performs the actual xDS RPCs using the xDS v3 API. It creates a 83 // single ADS stream on which the different types of xDS requests and responses 84 // are multiplexed. 85 type client struct { 86 nodeProto *v3corepb.Node 87 logger dubbogoLogger.Logger 88 } 89 90 func (v3c *client) NewStream(ctx context.Context, cc *grpc.ClientConn) (grpc.ClientStream, error) { 91 return v3discoverypb.NewAggregatedDiscoveryServiceClient(cc).StreamAggregatedResources(ctx, grpc.WaitForReady(true)) 92 } 93 94 // SetMetadata update client metadata 95 func (v3c *client) SetMetadata(p *_struct.Struct) { 96 v3c.nodeProto.Metadata = p 97 } 98 99 // SendRequest sends out a DiscoveryRequest for the given resourceNames, of type 100 // rType, on the provided stream. 101 // 102 // version is the ack version to be sent with the request 103 // - If this is the new request (not an ack/nack), version will be empty. 104 // - If this is an ack, version will be the version from the response. 105 // - If this is a nack, version will be the previous acked version (from 106 // versionMap). If there was no ack before, it will be empty. 107 func (v3c *client) SendRequest(s grpc.ClientStream, resourceNames []string, rType resource.ResourceType, version, nonce, errMsg string) error { 108 stream, ok := s.(adsStream) 109 if !ok { 110 return fmt.Errorf("xds: Attempt to send request on unsupported stream type: %T", s) 111 } 112 req := &v3discoverypb.DiscoveryRequest{ 113 Node: v3c.nodeProto, 114 TypeUrl: resourceTypeToURL[rType], 115 ResourceNames: resourceNames, 116 VersionInfo: version, 117 ResponseNonce: nonce, 118 } 119 if errMsg != "" { 120 req.ErrorDetail = &statuspb.Status{ 121 Code: int32(codes.InvalidArgument), Message: errMsg, 122 } 123 } 124 if err := stream.Send(req); err != nil { 125 return fmt.Errorf("xds: stream.Send(%+v) failed: %v", req, err) 126 } 127 v3c.logger.Debugf("ADS request sent: %v", pretty.ToJSON(req)) 128 return nil 129 } 130 131 // RecvResponse blocks on the receipt of one response message on the provided 132 // stream. 133 func (v3c *client) RecvResponse(s grpc.ClientStream) (proto.Message, error) { 134 stream, ok := s.(adsStream) 135 if !ok { 136 return nil, fmt.Errorf("xds: Attempt to receive response on unsupported stream type: %T", s) 137 } 138 139 resp, err := stream.Recv() 140 if err != nil { 141 return nil, fmt.Errorf("xds: stream.Recv() failed: %v", err) 142 } 143 v3c.logger.Infof("ADS response received, type: %v", resp.GetTypeUrl()) 144 v3c.logger.Debugf("ADS response received: %+v", pretty.ToJSON(resp)) 145 return resp, nil 146 } 147 148 func (v3c *client) ParseResponse(r proto.Message) (resource.ResourceType, []*anypb.Any, string, string, error) { 149 rType := resource.UnknownResource 150 resp, ok := r.(*v3discoverypb.DiscoveryResponse) 151 if !ok { 152 return rType, nil, "", "", fmt.Errorf("xds: unsupported message type: %T", resp) 153 } 154 155 // Note that the xDS transport protocol is versioned independently of 156 // the resource types, and it is supported to transfer older versions 157 // of resource types using new versions of the transport protocol, or 158 // vice-versa. Hence we need to handle v3 type_urls as well here. 159 var err error 160 url := resp.GetTypeUrl() 161 switch { 162 case resource.IsListenerResource(url): 163 rType = resource.ListenerResource 164 case resource.IsRouteConfigResource(url): 165 rType = resource.RouteConfigResource 166 case resource.IsClusterResource(url): 167 rType = resource.ClusterResource 168 case resource.IsEndpointsResource(url): 169 rType = resource.EndpointsResource 170 default: 171 return rType, nil, "", "", controllerversion.ErrResourceTypeUnsupported{ 172 ErrStr: fmt.Sprintf("Resource type %v unknown in response from server", resp.GetTypeUrl()), 173 } 174 } 175 return rType, resp.GetResources(), resp.GetVersionInfo(), resp.GetNonce(), err 176 }