-
Notifications
You must be signed in to change notification settings - Fork 18
/
upstream.go
244 lines (187 loc) · 6.07 KB
/
upstream.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package upstream
import (
"context"
"crypto/tls"
"errors"
"fmt"
"github.com/cirruslabs/cirrus-ci-agent/api"
"github.com/cirruslabs/cirrus-ci-agent/pkg/grpchelper"
"github.com/cirruslabs/cirrus-cli/internal/executor/endpoint"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"time"
)
var (
ErrFailed = errors.New("upstream failed")
)
const (
DefaultRPCEndpoint = "https://grpc.cirrus-ci.com:443"
defaultPollIntervalSeconds = 10
// Ridiculously large per-call timout in case some upstream hangs
// which might happen, but we've never experienced so far.
defaultDeadlineInSeconds = 900
)
type Upstream struct {
workerName string
registrationToken string
sessionToken string
rpcEndpoint string
rpcTarget string
rpcInsecure bool
rpcClient api.CirrusWorkersServiceClient
agentEndpoint endpoint.Endpoint
pollIntervalSeconds uint32
logger logrus.FieldLogger
connected bool
}
func New(workerName string, registrationToken string, opts ...Option) (*Upstream, error) {
upstream := &Upstream{
workerName: workerName,
registrationToken: registrationToken,
pollIntervalSeconds: defaultPollIntervalSeconds,
logger: logrus.New(),
}
// Apply options
for _, opt := range opts {
opt(upstream)
}
// Apply defaults
if upstream.rpcEndpoint == "" {
upstream.rpcEndpoint = DefaultRPCEndpoint
}
if upstream.agentEndpoint == nil {
upstream.agentEndpoint = endpoint.NewRemote(DefaultRPCEndpoint)
}
// Sanity check
if upstream.workerName == "" {
return nil, fmt.Errorf("%w: must provide a worker name", ErrFailed)
}
if upstream.registrationToken == "" {
return nil, fmt.Errorf("%w: must provide a registration token", ErrFailed)
}
// Parse endpoint
upstream.rpcTarget, upstream.rpcInsecure = grpchelper.TransportSettings(upstream.rpcEndpoint)
return upstream, nil
}
func (upstream *Upstream) WorkerName() string {
return upstream.workerName
}
func (upstream *Upstream) AgentEndpoint() endpoint.Endpoint {
return upstream.agentEndpoint
}
func (upstream *Upstream) PollIntervalSeconds() uint32 {
return upstream.pollIntervalSeconds
}
func (upstream *Upstream) Name() string {
return upstream.rpcEndpoint
}
func (upstream *Upstream) Connect(ctx context.Context) error {
if upstream.connected {
return nil
}
var rpcSecurity grpc.DialOption
if upstream.rpcInsecure {
rpcSecurity = grpc.WithTransportCredentials(insecure.NewCredentials())
} else {
tlsCredentials := credentials.NewTLS(&tls.Config{
MinVersion: tls.VersionTLS13,
})
rpcSecurity = grpc.WithTransportCredentials(tlsCredentials)
}
// https://github.com/grpc/grpc-go/blob/master/Documentation/concurrency.md
conn, err := grpc.DialContext(ctx, upstream.rpcTarget, rpcSecurity,
grpc.WithUnaryInterceptor(deadlineUnaryInterceptor(defaultDeadlineInSeconds*time.Second)),
grpc.WithStatsHandler(otelgrpc.NewClientHandler()),
)
if err != nil {
return fmt.Errorf("%w: failed to dial upstream %s: %v",
ErrFailed, upstream.Name(), err)
}
upstream.rpcClient = api.NewCirrusWorkersServiceClient(conn)
upstream.connected = true
return nil
}
func (upstream *Upstream) Register(ctx context.Context, workerInfo *api.WorkerInfo) error {
// Check if we've already registered
if upstream.sessionToken != "" {
return nil
}
if err := upstream.Connect(ctx); err != nil {
return err
}
response, err := upstream.rpcClient.Register(ctx, &api.RegisterRequest{
WorkerInfo: workerInfo,
RegistrationToken: upstream.registrationToken,
})
if err != nil {
return err
}
upstream.sessionToken = response.SessionToken
upstream.logger.Infof("worker successfully registered on upstream %s", upstream.Name())
return nil
}
func (upstream *Upstream) Poll(ctx context.Context, request *api.PollRequest) (*api.PollResponse, error) {
if err := upstream.Connect(ctx); err != nil {
return nil, err
}
upstream.logger.Debugf("polling upstream %s", upstream.Name())
response, err := upstream.rpcClient.Poll(ctx, request, grpc.PerRPCCredentials(upstream))
if err != nil {
return nil, err
}
if response.PollIntervalInSeconds != 0 && response.PollIntervalInSeconds <= uint32(time.Hour.Seconds()) {
upstream.pollIntervalSeconds = response.PollIntervalInSeconds
}
return response, nil
}
func (upstream *Upstream) TaskFailed(ctx context.Context, request *api.TaskFailedRequest) error {
if err := upstream.Connect(ctx); err != nil {
return err
}
_, err := upstream.rpcClient.TaskFailed(ctx, request, grpc.PerRPCCredentials(upstream))
return err
}
func (upstream *Upstream) TaskStarted(ctx context.Context, request *api.TaskIdentification) error {
if err := upstream.Connect(ctx); err != nil {
return err
}
_, err := upstream.rpcClient.TaskStarted(ctx, request, grpc.PerRPCCredentials(upstream))
return err
}
func (upstream *Upstream) TaskStopped(ctx context.Context, request *api.TaskIdentification) error {
if err := upstream.Connect(ctx); err != nil {
return err
}
_, err := upstream.rpcClient.TaskStopped(ctx, request, grpc.PerRPCCredentials(upstream))
return err
}
func (upstream *Upstream) SetDisabled(ctx context.Context, disabled bool) error {
if err := upstream.Connect(ctx); err != nil {
return err
}
request := &api.UpdateStatusRequest{
Disabled: disabled,
}
response, err := upstream.rpcClient.UpdateStatus(ctx, request, grpc.PerRPCCredentials(upstream))
if err != nil {
return fmt.Errorf("%w: failed to set disabled state on upstream %s: %v",
ErrFailed, upstream.Name(), err)
}
if response.Disabled != disabled {
return fmt.Errorf("%w: failed to set disabled state on upstream %s, expected %t, got %t",
ErrFailed, upstream.Name(), disabled, response.Disabled)
}
return err
}
func (upstream *Upstream) QueryRunningTasks(
ctx context.Context,
request *api.QueryRunningTasksRequest,
) (*api.QueryRunningTasksResponse, error) {
if err := upstream.Connect(ctx); err != nil {
return nil, err
}
return upstream.rpcClient.QueryRunningTasks(ctx, request, grpc.PerRPCCredentials(upstream))
}