-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontroller_http.go
495 lines (416 loc) · 13.3 KB
/
controller_http.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package ctrl
import (
"context"
"fmt"
"net/http"
"net/url"
"runtime/debug"
"strings"
"time"
"github.com/google/uuid"
orc "github.com/metal-toolbox/conditionorc/pkg/api/v1/orchestrator/client"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"golang.org/x/oauth2/clientcredentials"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/metal-toolbox/rivets/v2/condition"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)
const (
pkgHTTPController = "events/httpcontroller"
// max number of times times to retry the Orc API queries
// 300 * 30s = 2.5h
orcQueryRetries = 300
// query interval duration
queryInterval = 30 * time.Second
// Orchestrator API query timeout
orcQueryTimeout = 60 * time.Second
)
var (
ErrHandlerInit = errors.New("error initializing handler")
ErrEmptyResponse = errors.New("empty response with error")
errRetryRequest = errors.New("request retry required")
ErrNoCondition = errors.New("no condition available")
errNothingToDo = errors.New("nothing to do here")
errFetchTask = errors.New("error fetching Task object")
errFetchCondition = errors.New("error fetching Condition object")
)
// HTTPController implements the TaskHandler interface to interact with the NATS queue, KV over HTTP(s)
type HTTPController struct {
appName string
logger *logrus.Logger
facilityCode string
serverID uuid.UUID
conditionKind condition.Kind
orcQueryRetries int
queryInterval time.Duration
handlerTimeout time.Duration
orcQueryor orc.Queryor
}
type OrchestratorAPIConfig struct {
AuthDisabled bool
Endpoint string
OidcIssuerEndpoint string
OidcAudienceEndpoint string
OidcClientSecret string
OidcClientID string
OidcClientScopes []string
}
// OptionHTTPController sets parameters on the HTTPController
type OptionHTTPController func(*HTTPController)
func NewHTTPController(
appName,
facilityCode string,
serverID uuid.UUID,
conditionKind condition.Kind,
orcClientCfg *OrchestratorAPIConfig,
options ...OptionHTTPController) (*HTTPController, error) {
logger := logrus.New()
logger.Formatter = &logrus.JSONFormatter{}
nhc := &HTTPController{
appName: appName,
facilityCode: facilityCode,
serverID: serverID,
conditionKind: conditionKind,
orcQueryRetries: orcQueryRetries,
handlerTimeout: handlerTimeout,
queryInterval: queryInterval,
logger: logger,
}
for _, opt := range options {
opt(nhc)
}
if nhc.orcQueryor == nil {
orcQueryor, err := newConditionsAPIClient(orcClientCfg)
if err != nil {
return nil, errors.Wrap(ErrHandlerInit, "error in Conditions API client init: "+err.Error())
}
nhc.orcQueryor = orcQueryor
}
return nhc, nil
}
func newConditionsAPIClient(cfg *OrchestratorAPIConfig) (orc.Queryor, error) {
if cfg.AuthDisabled {
client := http.DefaultClient
client.Timeout = orcQueryTimeout
return orc.NewClient(
cfg.Endpoint,
orc.WithHTTPClient(client),
)
}
client, err := newOAuthClient(cfg)
if err != nil {
return nil, err
}
return orc.NewClient(
cfg.Endpoint,
orc.WithHTTPClient(client),
orc.WithAuthToken(cfg.OidcClientSecret),
)
}
// returns an http client setup with oauth and otelhttp
func newOAuthClient(cfg *OrchestratorAPIConfig) (*http.Client, error) {
errProvider := errors.New("orchestrator client OIDC provider setup error")
// otel http client
client := otelhttp.DefaultClient
client.Timeout = orcQueryTimeout
// context for OIDC issuer endpoint query
ctxp, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// setup oidc provider
provider, err := oidc.NewProvider(ctxp, cfg.OidcIssuerEndpoint)
if err != nil {
return nil, errors.Wrap(errProvider, err.Error())
}
// setup oauth configuration
oauthConfig := clientcredentials.Config{
ClientID: cfg.OidcClientID,
ClientSecret: cfg.OidcClientSecret,
TokenURL: provider.Endpoint().TokenURL,
Scopes: cfg.OidcClientScopes,
EndpointParams: url.Values{"audience": []string{cfg.OidcAudienceEndpoint}},
}
oAuthclient := oauthConfig.Client(context.Background())
client.Transport = oAuthclient.Transport
client.Jar = oAuthclient.Jar
return client, nil
}
// Sets a logger on the controller
func WithNATSHTTPLogger(logger *logrus.Logger) OptionHTTPController {
return func(n *HTTPController) {
n.logger = logger
}
}
// Sets the Orchestrator API queryor client
func WithOrchestratorClient(c orc.Queryor) OptionHTTPController {
return func(n *HTTPController) {
n.orcQueryor = c
}
}
func traceSpaceContextFromValues(traceID, spanID string) (trace.SpanContext, error) {
// extract traceID and spanID
pTraceID, _ := trace.TraceIDFromHex(traceID)
pSpanID, _ := trace.SpanIDFromHex(spanID)
// add a trace span
if pTraceID.IsValid() && pSpanID.IsValid() {
return trace.NewSpanContext(trace.SpanContextConfig{
TraceID: pTraceID,
SpanID: pSpanID,
TraceFlags: trace.FlagsSampled,
Remote: true,
}), nil
}
errExtract := errors.New("unable to extract span context")
return trace.SpanContext{}, errExtract
}
func (n *HTTPController) Run(ctx context.Context, handler TaskHandler) error {
ctx, span := otel.Tracer(pkgHTTPController).Start(
ctx,
"Run",
)
defer span.End()
var err error
task, err := n.fetchTaskWithRetries(ctx, n.serverID, n.orcQueryRetries, n.queryInterval)
if err != nil {
if errors.Is(err, errNothingToDo) {
n.logger.WithError(err).WithFields(logrus.Fields{
"conditionID": task.ID.String(),
}).Info("nothing to do here")
return nil
}
return errors.Wrap(ErrHandlerInit, err.Error())
}
// init publisher
publisher := NewHTTPPublisher(n.appName, n.serverID, task.ID, n.conditionKind, n.orcQueryor, n.logger)
if task.State == condition.Pending {
task.Status.Append("In process by controller: " + n.serverID.String())
} else {
task.Status.Append("resumed by controller: " + n.serverID.String())
}
if errPublish := publisher.Publish(ctx, task, false); errPublish != nil {
msg := "error publishing initial Task, Status KV record, condition aborted"
n.logger.WithError(errPublish).WithFields(logrus.Fields{
"conditionID": task.ID.String(),
}).Error(msg)
return errors.Wrap(errPublish, msg)
}
// set remote span context
remoteSpanCtx, err := traceSpaceContextFromValues(task.TraceID, task.SpanID)
if err != nil {
n.logger.Debug(err.Error())
} else {
// overwrite span context with remote span when available
var span trace.Span
ctx, span = otel.Tracer(pkgHTTPController).Start(
trace.ContextWithRemoteSpanContext(ctx, remoteSpanCtx),
"Run",
)
defer span.End()
}
return n.runTaskWithMonitor(ctx, handler, task, publisher, statusInterval)
}
func (n *HTTPController) runTaskWithMonitor(
ctx context.Context,
handler TaskHandler,
task *condition.Task[any, any],
publisher Publisher,
publishInterval time.Duration,
) error {
ctx, span := otel.Tracer(pkgHTTPController).Start(
ctx,
"runTaskWithMonitor",
)
defer span.End()
// doneCh indicates the handler run completed
doneCh := make(chan bool)
// monitor updates TS on status until the task handler returns.
monitor := func() {
ticker := time.NewTicker(publishInterval)
defer ticker.Stop()
// periodically update the LastUpdate TS in status KV,
/// which keeps the Orchestrator from reconciling this condition.
Loop:
for {
select {
case <-ticker.C:
if errPublish := publisher.Publish(
ctx,
task,
true,
); errPublish != nil {
n.logger.WithError(errPublish).Error("failed to publish update")
}
case <-doneCh:
break Loop
}
}
}
go monitor()
defer close(doneCh)
logger := n.logger.WithFields(
logrus.Fields{
"taskID": task.ID,
"state": task.State,
"serverID": task.Server.ID,
"kind": task.Kind,
},
)
publish := func(state condition.State, status string) {
// append to existing status record, unless it was overwritten by the controller somehow
task.Status.Append(status)
task.State = state
// publish failed state, status
if err := publisher.Publish(
ctx,
task,
false,
); err != nil {
logger.WithError(err).Error("failed to publish final status")
}
}
// panic handler
defer func() {
if rec := recover(); rec != nil {
// overwrite returned err - declared in func signature
err := errors.New("Panic occurred while running Condition handler")
logger.Printf("!!panic %s: %s", rec, debug.Stack())
logger.Error(err)
publish(condition.Failed, "Fatal error occurred, check logs for details")
}
}() // nolint:errcheck // nope
logger.Info("Controller initialized, running task..")
// set handler timeout
handlerCtx, cancel := context.WithTimeout(ctx, n.handlerTimeout)
defer cancel()
if err := handler.HandleTask(handlerCtx, task, publisher); err != nil {
task.Status.Append("controller returned error: " + err.Error())
task.State = condition.Failed
msg := "Controller returned error: " + err.Error()
logger.Error(msg)
publish(condition.Failed, msg)
}
// TODO:
// If the handler has returned and not updated the Task.State, StatusValue.State
// into a final state, then set those fields to failed.
logger.Info("Controller completed task")
return nil
}
func sleepWithContext(ctx context.Context, t time.Duration) error {
select {
case <-time.After(t):
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (n *HTTPController) fetchTaskWithRetries(ctx context.Context, serverID uuid.UUID, tries int, interval time.Duration) (*condition.Task[any, any], error) {
for attempt := 0; attempt <= tries; attempt++ {
le := n.logger.WithField("attempt", fmt.Sprintf("%d/%d", attempt, tries))
if attempt > 0 {
// returns error on context cancellation
if errSleep := sleepWithContext(ctx, interval); errSleep != nil {
return nil, errSleep
}
}
// fetch condition
le.Info("Fetching Condition..")
cond, err := n.fetchCondition(ctx, serverID)
if err != nil {
le.WithError(err).Warn("Condition fetch error")
if errors.Is(err, errRetryRequest) {
continue
}
return nil, err
}
// kind matches configured
if cond.Kind != n.conditionKind {
le.WithFields(logrus.Fields{
"conditionID": cond.ID,
"received": cond.Kind,
"expect": n.conditionKind,
"state": cond.State,
}).Debug("waiting for configured condition kind...")
continue
}
// state finalized
if condition.StateIsComplete(cond.State) {
le.WithFields(logrus.Fields{
"conditionID": cond.ID,
"received": cond.Kind,
"expect": n.conditionKind,
"state": cond.State,
}).Info("condition state is final, nothing to do here.")
return nil, errNothingToDo
}
// state pending
if cond.State == condition.Pending {
return condition.NewTaskFromCondition(cond), nil
}
// state active
if cond.State == condition.Active {
task, errFetch := n.fetchTask(ctx, serverID)
if errFetch != nil {
le.WithError(errFetch).Warn("Task fetch error")
if errors.Is(errFetch, errRetryRequest) {
continue
}
return nil, errFetch
}
return task, nil
}
}
return nil, errNothingToDo
}
// fetch condition - this will retrieve the current active/pending condition
func (n *HTTPController) fetchCondition(ctx context.Context, serverID uuid.UUID) (*condition.Condition, error) {
resp, err := n.orcQueryor.ConditionQuery(ctx, serverID)
if err != nil {
if strings.Contains(err.Error(), "EOF") {
return nil, errors.Wrap(errRetryRequest, "unexpected empty response")
}
return nil, errors.Wrap(errFetchCondition, err.Error())
}
if resp == nil {
return nil, errors.Wrap(errRetryRequest, "unexpected empty response")
}
switch resp.StatusCode {
case http.StatusOK:
if resp.Condition == nil {
return nil, errors.Wrap(errFetchCondition, "got nil object")
}
return resp.Condition, nil
case http.StatusNotFound, http.StatusInternalServerError:
return nil, errors.Wrap(errRetryRequest, fmt.Sprintf("%d, message: %s", resp.StatusCode, resp.Message))
case http.StatusBadRequest:
return nil, errors.Wrap(errFetchCondition, fmt.Sprintf("%d, message: %s", resp.StatusCode, resp.Message))
default:
return nil, errors.Wrap(errFetchCondition, fmt.Sprintf("unexpected status code %d, message: %s", resp.StatusCode, resp.Message))
}
}
func (n *HTTPController) fetchTask(ctx context.Context, serverID uuid.UUID) (*condition.Task[any, any], error) {
resp, err := n.orcQueryor.ConditionTaskQuery(ctx, n.conditionKind, serverID)
if err != nil {
if strings.Contains(err.Error(), "EOF") {
return nil, errors.Wrap(errRetryRequest, "unexpected empty response")
}
return nil, errors.Wrap(errFetchTask, err.Error())
}
if resp == nil {
return nil, errors.Wrap(errRetryRequest, "unexpected empty response")
}
switch resp.StatusCode {
case http.StatusOK:
if resp.Task == nil {
return nil, errors.Wrap(errFetchTask, "got nil object")
}
return resp.Task, nil
case http.StatusNotFound, http.StatusInternalServerError, http.StatusUnprocessableEntity:
return nil, errors.Wrap(errRetryRequest, fmt.Sprintf("%d, message: %s", resp.StatusCode, resp.Message))
case http.StatusBadRequest:
return nil, errors.Wrap(errFetchTask, fmt.Sprintf("%d, message: %s", resp.StatusCode, resp.Message))
default:
return nil, errors.Wrap(errFetchTask, fmt.Sprintf("unexpected status code %d, message: %s", resp.StatusCode, resp.Message))
}
}