-
Notifications
You must be signed in to change notification settings - Fork 118
/
main.go
523 lines (418 loc) · 14.1 KB
/
main.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// Copyright (c) OpenFaaS Author(s) 2021. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/signal"
"path/filepath"
"strings"
"sync/atomic"
"syscall"
"time"
units "github.com/docker/go-units"
"github.com/openfaas/faas-middleware/auth"
limiter "github.com/openfaas/faas-middleware/concurrency-limiter"
"github.com/openfaas/of-watchdog/config"
"github.com/openfaas/of-watchdog/executor"
"github.com/openfaas/of-watchdog/metrics"
"github.com/prometheus/client_golang/prometheus/testutil"
)
var (
acceptingConnections int32
)
func main() {
var runHealthcheck bool
var versionFlag bool
flag.BoolVar(&versionFlag, "version", false, "Print the version and exit")
flag.BoolVar(&runHealthcheck,
"run-healthcheck",
false,
"Check for the a lock-file, when using an exec healthcheck. Exit 0 for present, non-zero when not found.")
flag.Parse()
printVersion()
if versionFlag {
return
}
if runHealthcheck {
if lockFilePresent() {
os.Exit(0)
}
fmt.Fprintf(os.Stderr, "unable to find lock file.\n")
os.Exit(1)
}
atomic.StoreInt32(&acceptingConnections, 0)
watchdogConfig, err := config.New(os.Environ())
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading config: %s", err.Error())
os.Exit(1)
}
// baseFunctionHandler is the function invoker without any other middlewares.
// It is used to provide a generic way to implement the readiness checks regardless
// of the request mode.
baseFunctionHandler := buildRequestHandler(watchdogConfig, watchdogConfig.PrefixLogs)
requestHandler := baseFunctionHandler
if watchdogConfig.JWTAuthentication {
handler, err := makeJWTAuthHandler(watchdogConfig, baseFunctionHandler)
if err != nil {
log.Fatalf("Error creating JWTAuthMiddleware: %s", err.Error())
}
requestHandler = handler
}
var limit limiter.Limiter
if watchdogConfig.MaxInflight > 0 {
requestLimiter := limiter.NewConcurrencyLimiter(requestHandler, watchdogConfig.MaxInflight)
requestHandler = requestLimiter.Handler()
limit = requestLimiter
}
log.Printf("Watchdog mode: %s\tfprocess: %q\n", config.WatchdogMode(watchdogConfig.OperationalMode), watchdogConfig.FunctionProcess)
httpMetrics := metrics.NewHttp()
http.HandleFunc("/", metrics.InstrumentHandler(requestHandler, httpMetrics))
http.HandleFunc("/_/health", makeHealthHandler())
http.Handle("/_/ready", &readiness{
// make sure to pass original handler, before it's been wrapped by
// the limiter
functionHandler: baseFunctionHandler,
endpoint: watchdogConfig.ReadyEndpoint,
lockCheck: lockFilePresent,
limiter: limit,
})
metricsServer := metrics.MetricsServer{}
metricsServer.Register(watchdogConfig.MetricsPort)
cancel := make(chan bool)
go metricsServer.Serve(cancel)
s := &http.Server{
Addr: fmt.Sprintf(":%d", watchdogConfig.TCPPort),
ReadTimeout: watchdogConfig.HTTPReadTimeout,
WriteTimeout: watchdogConfig.HTTPWriteTimeout,
MaxHeaderBytes: 1 << 20, // Max header of 1MB
}
log.Printf("Timeouts: read: %s write: %s hard: %s health: %s\n",
watchdogConfig.HTTPReadTimeout,
watchdogConfig.HTTPWriteTimeout,
watchdogConfig.ExecTimeout,
watchdogConfig.HealthcheckInterval)
if watchdogConfig.JWTAuthentication {
log.Printf("JWT Auth: %v\n", watchdogConfig.JWTAuthentication)
}
log.Printf("Listening on port: %d\n", watchdogConfig.TCPPort)
listenUntilShutdown(s,
watchdogConfig.HealthcheckInterval,
watchdogConfig.HTTPWriteTimeout,
watchdogConfig.SuppressLock,
&httpMetrics)
}
func markUnhealthy() error {
atomic.StoreInt32(&acceptingConnections, 0)
path := filepath.Join(os.TempDir(), ".lock")
log.Printf("Removing lock-file : %s\n", path)
removeErr := os.Remove(path)
return removeErr
}
func listenUntilShutdown(s *http.Server, healthcheckInterval time.Duration, writeTimeout time.Duration, suppressLock bool, httpMetrics *metrics.Http) {
idleConnsClosed := make(chan struct{})
go func() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGTERM)
<-sig
log.Printf("SIGTERM: no new connections in %s\n", healthcheckInterval.String())
if err := markUnhealthy(); err != nil {
log.Printf("Unable to mark server as unhealthy: %s\n", err.Error())
}
<-time.Tick(healthcheckInterval)
connections := int64(testutil.ToFloat64(httpMetrics.InFlight))
log.Printf("No new connections allowed, draining: %d requests\n", connections)
// The maximum time to wait for active connections whilst shutting down is
// equivalent to the maximum execution time i.e. writeTimeout.
ctx, cancel := context.WithTimeout(context.Background(), writeTimeout)
defer cancel()
if err := s.Shutdown(ctx); err != nil {
log.Printf("Error in Shutdown: %v", err)
}
connections = int64(testutil.ToFloat64(httpMetrics.InFlight))
log.Printf("Exiting. Active connections: %d\n", connections)
close(idleConnsClosed)
}()
// Run the HTTP server in a separate go-routine.
go func() {
if err := s.ListenAndServe(); err != http.ErrServerClosed {
log.Printf("Error ListenAndServe: %v", err)
close(idleConnsClosed)
}
}()
if suppressLock == false {
path, writeErr := createLockFile()
if writeErr != nil {
log.Panicf("Cannot write %s. To disable lock-file set env suppress_lock=true.\n Error: %s.\n", path, writeErr.Error())
}
} else {
log.Println("Warning: \"suppress_lock\" is enabled. No automated health-checks will be in place for your function.")
atomic.StoreInt32(&acceptingConnections, 1)
}
<-idleConnsClosed
}
func buildRequestHandler(watchdogConfig config.WatchdogConfig, prefixLogs bool) http.Handler {
var requestHandler http.HandlerFunc
switch watchdogConfig.OperationalMode {
case config.ModeStreaming:
requestHandler = makeStreamingRequestHandler(watchdogConfig, prefixLogs, watchdogConfig.LogBufferSize)
case config.ModeSerializing:
requestHandler = makeSerializingForkRequestHandler(watchdogConfig, prefixLogs)
case config.ModeHTTP:
requestHandler = makeHTTPRequestHandler(watchdogConfig, prefixLogs, watchdogConfig.LogBufferSize)
case config.ModeStatic:
requestHandler = makeStaticRequestHandler(watchdogConfig)
default:
log.Panicf("unknown watchdog mode: %d", watchdogConfig.OperationalMode)
}
return requestHandler
}
// createLockFile returns a path to a lock file and/or an error
// if the file could not be created.
func createLockFile() (string, error) {
path := filepath.Join(os.TempDir(), ".lock")
log.Printf("Writing lock-file to: %s\n", path)
if err := os.MkdirAll(os.TempDir(), os.ModePerm); err != nil {
return path, err
}
if err := os.WriteFile(path, []byte{}, 0660); err != nil {
return path, err
}
atomic.StoreInt32(&acceptingConnections, 1)
return path, nil
}
func makeSerializingForkRequestHandler(watchdogConfig config.WatchdogConfig, logPrefix bool) func(http.ResponseWriter, *http.Request) {
functionInvoker := executor.SerializingForkFunctionRunner{
ExecTimeout: watchdogConfig.ExecTimeout,
LogPrefix: logPrefix,
LogBufferSize: watchdogConfig.LogBufferSize,
}
return func(w http.ResponseWriter, r *http.Request) {
var environment []string
if watchdogConfig.InjectCGIHeaders {
environment = getEnvironment(r)
}
commandName, arguments := watchdogConfig.Process()
req := executor.FunctionRequest{
Process: commandName,
ProcessArgs: arguments,
InputReader: r.Body,
ContentLength: &r.ContentLength,
OutputWriter: w,
Environment: environment,
RequestURI: r.RequestURI,
Method: r.Method,
UserAgent: r.UserAgent(),
}
w.Header().Set("Content-Type", watchdogConfig.ContentType)
err := functionInvoker.Run(req, w)
if err != nil {
log.Println(err)
}
}
}
func makeStreamingRequestHandler(watchdogConfig config.WatchdogConfig, prefixLogs bool, logBufferSize int) func(http.ResponseWriter, *http.Request) {
functionInvoker := executor.StreamingFunctionRunner{
ExecTimeout: watchdogConfig.ExecTimeout,
LogPrefix: prefixLogs,
LogBufferSize: logBufferSize,
}
return func(w http.ResponseWriter, r *http.Request) {
var environment []string
if watchdogConfig.InjectCGIHeaders {
environment = getEnvironment(r)
}
ww := WriterCounter{}
ww.setWriter(w)
start := time.Now()
commandName, arguments := watchdogConfig.Process()
req := executor.FunctionRequest{
Process: commandName,
ProcessArgs: arguments,
InputReader: r.Body,
OutputWriter: &ww,
Environment: environment,
RequestURI: r.RequestURI,
Method: r.Method,
UserAgent: r.UserAgent(),
}
w.Header().Set("Content-Type", watchdogConfig.ContentType)
err := functionInvoker.Run(req)
if err != nil {
log.Println(err.Error())
// Cannot write a status code to the client because we
// already have written a header
done := time.Since(start)
if !strings.HasPrefix(req.UserAgent, "kube-probe") {
log.Printf("%s %s - %d - ContentLength: %s (%.4fs)", req.Method, req.RequestURI, http.StatusInternalServerError, units.HumanSize(float64(ww.Bytes())), done.Seconds())
return
}
}
done := time.Since(start)
if !strings.HasPrefix(req.UserAgent, "kube-probe") {
log.Printf("%s %s - %d - ContentLength: %s (%.4fs)", req.Method, req.RequestURI, http.StatusOK, units.HumanSize(float64(ww.Bytes())), done.Seconds())
}
}
}
func getEnvironment(r *http.Request) []string {
var envs []string
envs = os.Environ()
for k, v := range r.Header {
kv := fmt.Sprintf("Http_%s=%s", strings.Replace(k, "-", "_", -1), v[0])
envs = append(envs, kv)
}
envs = append(envs, fmt.Sprintf("Http_Method=%s", r.Method))
if len(r.URL.RawQuery) > 0 {
envs = append(envs, fmt.Sprintf("Http_Query=%s", r.URL.RawQuery))
}
if len(r.URL.Path) > 0 {
envs = append(envs, fmt.Sprintf("Http_Path=%s", r.URL.Path))
}
if len(r.TransferEncoding) > 0 {
envs = append(envs, fmt.Sprintf("Http_Transfer_Encoding=%s", r.TransferEncoding[0]))
}
return envs
}
func makeHTTPRequestHandler(watchdogConfig config.WatchdogConfig, prefixLogs bool, logBufferSize int) func(http.ResponseWriter, *http.Request) {
upstreamURL, _ := url.Parse(watchdogConfig.UpstreamURL)
commandName, arguments := watchdogConfig.Process()
functionInvoker := executor.HTTPFunctionRunner{
ExecTimeout: watchdogConfig.ExecTimeout,
Process: commandName,
ProcessArgs: arguments,
BufferHTTPBody: watchdogConfig.BufferHTTPBody,
LogPrefix: prefixLogs,
LogBufferSize: logBufferSize,
LogCallId: watchdogConfig.LogCallId,
ReverseProxy: &httputil.ReverseProxy{
Director: func(req *http.Request) {
req.URL.Host = upstreamURL.Host
req.URL.Scheme = "http"
},
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
},
ErrorLog: log.New(io.Discard, "", 0),
},
}
if len(watchdogConfig.UpstreamURL) == 0 {
log.Fatal(`For "mode=http" you must specify a valid URL for "http_upstream_url"`)
}
urlValue, err := url.Parse(watchdogConfig.UpstreamURL)
if err != nil {
log.Fatalf(`For "mode=http" you must specify a valid URL for "http_upstream_url", error: %s`, err)
}
functionInvoker.UpstreamURL = urlValue
log.Printf("Forking: %s, arguments: %s", commandName, arguments)
functionInvoker.Start()
return func(w http.ResponseWriter, r *http.Request) {
req := executor.FunctionRequest{
Process: commandName,
ProcessArgs: arguments,
OutputWriter: w,
}
if r.Body != nil {
defer r.Body.Close()
}
if err := functionInvoker.Run(req, r.ContentLength, r, w); err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
}
}
}
func makeStaticRequestHandler(watchdogConfig config.WatchdogConfig) http.HandlerFunc {
if watchdogConfig.StaticPath == "" {
log.Fatal(`For mode=static you must specify the "static_path" to serve`)
}
log.Printf("Serving files at: %s", watchdogConfig.StaticPath)
return http.FileServer(http.Dir(watchdogConfig.StaticPath)).ServeHTTP
}
func lockFilePresent() bool {
path := filepath.Join(os.TempDir(), ".lock")
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
func makeHealthHandler() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
if atomic.LoadInt32(&acceptingConnections) == 0 || lockFilePresent() == false {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
}
func makeJWTAuthHandler(c config.WatchdogConfig, next http.Handler) (http.Handler, error) {
namespace, err := getFnNamespace()
if err != nil {
return nil, fmt.Errorf("failed to get function namespace: %w", err)
}
name, err := getFnName()
if err != nil {
return nil, fmt.Errorf("failed to get function name: %w", err)
}
authOpts := auth.JWTAuthOptions{
Name: name,
Namespace: namespace,
LocalAuthority: c.JWTAuthLocal,
Debug: c.JWTAuthDebug,
}
return auth.NewJWTAuthMiddleware(authOpts, next)
}
func printVersion() {
sha := "unknown"
if len(GitCommit) > 0 {
sha = GitCommit
}
log.Printf("Version: %v\tSHA: %v\n", BuildVersion(), sha)
}
type WriterCounter struct {
w io.Writer
bytes int64
}
func (nc *WriterCounter) setWriter(w io.Writer) {
nc.w = w
}
func (nc *WriterCounter) Bytes() int64 {
return nc.bytes
}
func (nc *WriterCounter) Write(p []byte) (int, error) {
n, err := nc.w.Write(p)
if err != nil {
return n, err
}
nc.bytes += int64(n)
return n, err
}
func getFnName() (string, error) {
name, ok := os.LookupEnv("OPENFAAS_NAME")
if !ok || len(name) == 0 {
return "", fmt.Errorf("env variable 'OPENFAAS_NAME' not set")
}
return name, nil
}
// getFnNamespace gets the namespace name from the env variable OPENFAAS_NAMESPACE
// or reads it from the service account if the env variable is not present
func getFnNamespace() (string, error) {
if namespace, ok := os.LookupEnv("OPENFAAS_NAMESPACE"); ok {
return namespace, nil
}
nsVal, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil {
return "", err
}
return string(nsVal), nil
}