forked from go-chassis/go-chassis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pilotv2.go
executable file
·114 lines (95 loc) · 3.22 KB
/
pilotv2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package client
import (
"context"
"fmt"
"time"
envoy_api "github.com/envoyproxy/go-control-plane/envoy/api/v2"
envoy_api_core "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
xds "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v2"
"github.com/go-chassis/go-chassis/pkg/istio/util"
"google.golang.org/grpc"
)
// PilotClient is a interface for the client to communicate to pilot
type PilotClient interface {
RDS
// TODO: add all xDS interface
EDS
CDS
LDS
}
// RDS defines route discovery service interface
type RDS interface {
GetAllRouteConfigurations() (*envoy_api.RouteConfiguration, error)
GetRouteConfigurationsByPort(string) (*envoy_api.RouteConfiguration, error)
}
// EDS defines endpoint discovery service interface
type EDS interface{}
// CDS defines cluster discovery service interface
type CDS interface{}
// LDS defines listener discovery service interface
type LDS interface{}
type pilotClient struct {
rawConn *grpc.ClientConn
adsConn xds.AggregatedDiscoveryServiceClient
edsConn envoy_api.EndpointDiscoveryServiceClient
}
// NewGRPCPilotClient returns new PilotClient from options
func NewGRPCPilotClient(cfg *PilotOptions) (PilotClient, error) {
// TODO: credentials need to be added here
// set dial options from config
conn, err := grpc.Dial(cfg.Endpoints[0], grpc.WithInsecure())
if err != nil {
return nil, fmt.Errorf("new grpc pilot client error: %v", err)
}
ads := xds.NewAggregatedDiscoveryServiceClient(conn)
eds := envoy_api.NewEndpointDiscoveryServiceClient(conn)
return &pilotClient{rawConn: conn,
adsConn: ads, edsConn: eds,
}, nil
}
func (c *pilotClient) GetAllRouteConfigurations() (*envoy_api.RouteConfiguration, error) {
// TODO: this RDS stream can be reuse in all RDS request?
rds, err := c.adsConn.StreamAggregatedResources(context.Background())
if err != nil {
return nil, fmt.Errorf("[RDS] stream error: %v", err)
}
nodeID := util.BuildNodeID()
err = rds.Send(&envoy_api.DiscoveryRequest{
ResponseNonce: time.Now().String(),
Node: &envoy_api_core.Node{
Id: nodeID,
},
ResourceNames: []string{util.RDSHttpProxy},
TypeUrl: util.RouteType})
if err != nil {
return nil, fmt.Errorf("[RDS] send req error for %s(%s): %v", util.RDSHttpProxy, nodeID, err)
}
res, err := rds.Recv()
if err != nil {
return nil, fmt.Errorf("[RDS] recv error for %s(%s): %v", util.RDSHttpProxy, nodeID, err)
}
return GetRouteConfiguration(res)
}
func (c *pilotClient) GetRouteConfigurationsByPort(port string) (*envoy_api.RouteConfiguration, error) {
// TODO: this RDS stream can be reuse in all RDS request?
rds, err := c.adsConn.StreamAggregatedResources(context.Background())
if err != nil {
return nil, fmt.Errorf("[RDS] stream error: %v", err)
}
nodeID := util.BuildNodeID()
err = rds.Send(&envoy_api.DiscoveryRequest{
ResponseNonce: time.Now().String(),
Node: &envoy_api_core.Node{
Id: nodeID,
},
ResourceNames: []string{port},
TypeUrl: util.RouteType})
if err != nil {
return nil, fmt.Errorf("[RDS] send req error for %s(%s): %v", util.RDSHttpProxy, nodeID, err)
}
res, err := rds.Recv()
if err != nil {
return nil, fmt.Errorf("[RDS] recv error for %s(%s): %v", util.RDSHttpProxy, nodeID, err)
}
return GetRouteConfiguration(res)
}