-
Notifications
You must be signed in to change notification settings - Fork 685
/
comm.go
157 lines (126 loc) · 3.08 KB
/
comm.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package agent
import (
"context"
"crypto/tls"
"io"
"net/url"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
"github.com/datawire/ambassador/v2/pkg/api/agent"
"github.com/datawire/dlib/dlog"
)
const APIKeyMetadataKey = "x-ambassador-api-key"
type RPCComm struct {
conn io.Closer
client agent.DirectorClient
rptWake chan struct{}
retCancel context.CancelFunc
agentID *agent.Identity
directives chan *agent.Directive
}
const (
defaultHostname = "app.getambassador.io"
defaultPort = "443"
)
type ConnInfo struct {
hostname string
port string
secure bool
}
func connInfoFromAddress(address string) (*ConnInfo, error) {
endpoint, err := url.Parse(address)
if err != nil {
return nil, err
}
hostname := endpoint.Hostname()
if hostname == "" {
hostname = defaultHostname
}
port := endpoint.Port()
if port == "" {
port = defaultPort
}
secure := true
if endpoint.Scheme == "http" {
secure = false
}
return &ConnInfo{hostname, port, secure}, nil
}
func NewComm(ctx context.Context, connInfo *ConnInfo, agentID *agent.Identity, apiKey string) (*RPCComm, error) {
ctx = dlog.WithField(ctx, "agent", "comm")
opts := make([]grpc.DialOption, 0, 1)
address := connInfo.hostname + ":" + connInfo.port
if connInfo.secure {
config := &tls.Config{ServerName: connInfo.hostname}
creds := credentials.NewTLS(config)
opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
opts = append(opts, grpc.WithInsecure())
}
dlog.Debugf(ctx, "Dialing server at %s (secure=%t)", address, connInfo.secure)
conn, err := grpc.Dial(address, opts...)
if err != nil {
return nil, err
}
client := agent.NewDirectorClient(conn)
retCtx, retCancel := context.WithCancel(ctx)
c := &RPCComm{
conn: conn,
client: client,
retCancel: retCancel,
agentID: agentID,
directives: make(chan *agent.Directive, 1),
rptWake: make(chan struct{}, 1),
}
retCtx = metadata.AppendToOutgoingContext(retCtx, APIKeyMetadataKey, apiKey)
go c.retrieveLoop(retCtx)
return c, nil
}
func (c *RPCComm) retrieveLoop(ctx context.Context) {
ctx = dlog.WithField(ctx, "agent", "retriever")
for {
if err := c.retrieve(ctx); err != nil {
dlog.Debugf(ctx, "exited: %+v", err)
}
select {
case <-c.rptWake:
dlog.Debug(ctx, "restarting")
case <-ctx.Done():
return
}
}
}
func (c *RPCComm) retrieve(ctx context.Context) error {
stream, err := c.client.Retrieve(ctx, c.agentID)
if err != nil {
return err
}
for {
directive, err := stream.Recv()
if err != nil {
return err
}
select {
case c.directives <- directive:
case <-ctx.Done():
return nil
}
}
}
func (c *RPCComm) Close() error {
c.retCancel()
return c.conn.Close()
}
func (c *RPCComm) Report(ctx context.Context, report *agent.Snapshot, apiKey string) error {
select {
case c.rptWake <- struct{}{}:
default:
}
ctx = metadata.AppendToOutgoingContext(ctx, APIKeyMetadataKey, apiKey)
_, err := c.client.Report(ctx, report)
return err
}
func (c *RPCComm) Directives() <-chan *agent.Directive {
return c.directives
}