-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
235 lines (182 loc) · 6.08 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
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Package main provides the OpenFaaS Classic Watchdog. The Classic Watchdog is a HTTP
// shim for serverless functions providing health-checking, graceful shutdowns,
// timeouts and a consistent logging experience.
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"sync/atomic"
"syscall"
"time"
"github.com/openfaas/classic-watchdog/metrics"
"github.com/openfaas/classic-watchdog/types"
"github.com/openfaas/faas-middleware/auth"
"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()
if runHealthcheck {
if lockFilePresent() {
os.Exit(0)
}
fmt.Fprintf(os.Stderr, "unable to find lock file.\n")
os.Exit(1)
}
printVersion()
if versionFlag {
return
}
atomic.StoreInt32(&acceptingConnections, 0)
osEnv := types.OsEnv{}
readConfig := ReadConfig{}
config := readConfig.Read(osEnv)
if len(config.faasProcess) == 0 {
log.Panicln("Provide a valid process via fprocess environmental variable.")
return
}
readTimeout := config.readTimeout
writeTimeout := config.writeTimeout
healthcheckInterval := config.healthcheckInterval
s := &http.Server{
Addr: fmt.Sprintf(":%d", config.port),
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
MaxHeaderBytes: 1 << 20, // Max header of 1MB
}
httpMetrics := metrics.NewHttp()
log.Printf("Timeouts: read: %s write: %s hard: %s health: %s.\n",
readTimeout,
writeTimeout,
config.execTimeout,
healthcheckInterval)
log.Printf("Listening on port: %d\n", config.port)
requestHandler := makeRequestHandler(&config)
if config.jwtAuthentication {
handler, err := makeJWTAuthHandler(config, requestHandler)
if err != nil {
log.Fatalf("Error creating JWTAuthMiddleware: %s", err.Error())
}
requestHandler = handler
}
http.HandleFunc("/_/health", makeHealthHandler())
http.HandleFunc("/", metrics.InstrumentHandler(requestHandler, httpMetrics))
metricsServer := metrics.MetricsServer{}
metricsServer.Register(config.metricsPort)
cancel := make(chan bool)
go metricsServer.Serve(cancel)
listenUntilShutdown(s, healthcheckInterval, writeTimeout, config.suppressLock, &httpMetrics)
}
// listenUntilShutdown will listen for HTTP requests until SIGTERM
// is sent at which point the code will wait `shutdownTimeout` before
// closing off connections and a futher `shutdownTimeout` before
// exiting
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 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 printVersion() {
sha := "unknown"
if len(GitCommit) > 0 {
sha = GitCommit
}
log.Printf("Version: %v\tSHA: %v\n", BuildVersion(), sha)
}
func makeJWTAuthHandler(c 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 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
}