/
setup.go
320 lines (263 loc) · 8.71 KB
/
setup.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
/*This file is part of kuberpult.
Kuberpult is free software: you can redistribute it and/or modify
it under the terms of the Expat(MIT) License as published by
the Free Software Foundation.
Kuberpult is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MIT License for more details.
You should have received a copy of the MIT License
along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
Copyright 2023 freiheit.com*/
// Setup implementation shared between all microservices.
// If this file is changed it will affect _all_ microservices in the monorepo (and this
// is deliberately so).
package setup
import (
"context"
"crypto/subtle"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.uber.org/zap"
"github.com/freiheit-com/kuberpult/pkg/logger"
"github.com/freiheit-com/kuberpult/pkg/metrics"
"google.golang.org/grpc"
)
var (
osSignalChannel = make(chan os.Signal, 1) // System writes here when shutdown
)
func init() {
signal.Notify(osSignalChannel, syscall.SIGINT, syscall.SIGTERM)
}
type shutdown struct {
name string
fn func(context.Context) error
}
// Setup structure that holds only the shutdown callbacks for all
// grpc and http server for endpoints, metrics, health checks, etc.
type setup struct {
health HealthServer
shutdown []shutdown
shutdownChannel chan bool
}
type BasicAuth struct {
Username string
Password string
}
type GRPCConfig struct {
// required
Port string
Register func(*grpc.Server)
Opts []grpc.ServerOption
// optional
Shutdown func(context.Context) error
}
type HTTPConfig struct {
// required
Port string
Register func(*http.ServeMux)
// optional
BasicAuth *BasicAuth
Shutdown func(context.Context) error
}
type BackgroundFunc func(context.Context, *HealthReporter) error
type BackgroundTaskConfig struct {
// a function that triggers a graceful shutdown of all other resources after completion
Run BackgroundFunc
Name string
// optional
Shutdown func(context.Context) error
}
// Config contains configurations for all servers & tasks will be started.
// A startup order is not guaranteed.
type ServerConfig struct {
GRPC *GRPCConfig
HTTP []HTTPConfig
// BackgroundTasks are tasks that are running forever, like Pub/sub receiver. If they
// finish, a graceful shutdown will be triggered.
Background []BackgroundTaskConfig
Shutdown func(context.Context) error
}
func Run(ctx context.Context, config ServerConfig) {
s := &setup{}
ctx, cancel := context.WithCancel(ctx)
pv, handler, _ := metrics.Init()
ctx = metrics.WithProvider(ctx, pv)
pv.Meter("setup").Int64ObservableGauge("background_job_ready", metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error {
reports := s.health.reports()
for name, report := range reports {
var value int64
if report.isReady(s.health.now()) {
value = 1
}
o.Observe(value, metric.WithAttributes(attribute.String("name", name)))
}
return nil
}))
// Start the listening on each protocol
for _, cfg := range config.HTTP {
setupHTTP(ctx, s, cfg, cancel, handler)
}
if config.GRPC != nil {
setupGRPC(ctx, s, *config.GRPC, cancel)
}
for _, task := range config.Background {
setupBackgroundTask(ctx, s, task, cancel)
}
if config.Shutdown != nil {
s.RegisterShutdown(
"global shutdown handler",
config.Shutdown,
)
}
// Listening for shutdown signal
s.listenToShutdownSignal(ctx, cancel)
}
func (s *setup) RegisterShutdown(name string, shutdownFN func(ctx context.Context) error) {
s.shutdown = append(s.shutdown, shutdown{name: name, fn: shutdownFN})
}
func (s *setup) listenToShutdownSignal(ctx context.Context, cancelFunc context.CancelFunc) {
// Wait for a signal to shutdown all servers.
// This should be a blocking call, because program will exit as soon as
// the main goroutine returns (so it doesn't wait for other goroutines).
// Non-blocking call could lead to unfinished cleanup during the shutdown.
// See also https://golang.org/ref/spec#Program_execution
select {
case <-osSignalChannel:
case <-ctx.Done():
}
// cancel the context
cancelFunc()
// call shutdown hooks
gracefulShutdown(ctx, s, 30*time.Second)
}
func gracefulShutdown(ctx context.Context, s *setup, timeout time.Duration) {
// Instantiate background context
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
for i := len(s.shutdown) - 1; i >= 0; i-- {
sd := s.shutdown[i]
if err := sd.fn(ctx); err != nil {
logger.FromContext(ctx).Error("shutdown.failed", zap.Error(err), zap.String("handler", sd.name))
}
}
}
func setupGRPC(ctx context.Context, s *setup, config GRPCConfig, cancel context.CancelFunc) {
// Get service listening port
addrGRPC := ":" + config.Port
// Setup a listener for gRPC port
grpcL, err := net.Listen("tcp", addrGRPC)
if err != nil {
logger.FromContext(ctx).Panic("grpc.listen.error", zap.Error(err), zap.String("addr", addrGRPC))
return
}
s.RegisterShutdown("GRPC net listener", func(context.Context) error {
return grpcL.Close()
})
// Instantiate gRPC server
grpcS := grpc.NewServer(config.Opts...)
s.RegisterShutdown("GRPC server", func(context.Context) error {
grpcS.GracefulStop()
return nil
})
config.Register(grpcS)
if config.Shutdown != nil {
s.RegisterShutdown("GRPC shutdown handler", config.Shutdown)
}
go serveGRPC(ctx, grpcS, grpcL, cancel)
}
func serveGRPC(ctx context.Context, grpcS *grpc.Server, grpcL net.Listener, cancel context.CancelFunc) {
defer cancel()
if err := grpcS.Serve(grpcL); err != nil {
logger.FromContext(ctx).Error("grpc.serve.error", zap.Error(err))
}
}
func setupHTTP(ctx context.Context, s *setup, config HTTPConfig, cancel context.CancelFunc, metricHandler http.Handler) {
mux := http.NewServeMux()
if config.Register != nil {
config.Register(mux)
}
mux.Handle("/metrics", metricHandler)
mux.Handle("/healthz", &s.health)
runHTTPHandler(ctx, s, mux, config.Port, config.BasicAuth, config.Shutdown, cancel)
}
func runHTTPHandler(ctx context.Context, s *setup, handler http.Handler, port string, basicAuth *BasicAuth, shutdown func(context.Context) error, cancel context.CancelFunc) {
if basicAuth != nil {
handler = NewBasicAuthHandler(basicAuth, handler)
}
httpS := &http.Server{
Handler: handler,
}
s.RegisterShutdown(
fmt.Sprintf("http server on %s", port),
func(ctx context.Context) error {
return shutdownHTTP(ctx, httpS)
},
)
if shutdown != nil {
s.RegisterShutdown(
fmt.Sprintf("http shutdown handler on %s", port),
shutdown,
)
}
go serveHTTP(ctx, httpS, port, cancel)
}
var shutdownHTTP = func(ctx context.Context, httpS *http.Server) error {
return httpS.Shutdown(ctx)
}
var serveHTTP = func(ctx context.Context, httpS *http.Server, port string, cancel context.CancelFunc) {
// if this function returns, the server was stopped, so stop also all the other services
defer cancel()
addr := ":" + port
httpL, err := net.Listen("tcp", addr)
if err != nil {
logger.FromContext(ctx).Panic("http.listen.error", zap.Error(err), zap.String("addr", addr))
return
}
if err := httpS.Serve(httpL); err != nil && err != http.ErrServerClosed {
logger.FromContext(ctx).Error("http.serve.error", zap.Error(err))
}
}
func setupBackgroundTask(ctx context.Context, s *setup, config BackgroundTaskConfig, cancel context.CancelFunc) {
if config.Shutdown != nil {
s.RegisterShutdown(
fmt.Sprintf("shutdown handler for %s", config.Name),
config.Shutdown,
)
}
reporter := s.health.Reporter(config.Name)
go runBackgroundTask(ctx, config, cancel, reporter)
}
func runBackgroundTask(ctx context.Context, config BackgroundTaskConfig, cancel context.CancelFunc, reporter *HealthReporter) {
defer cancel()
if err := config.Run(ctx, reporter); err != nil {
logger.FromContext(ctx).Error("background.error", zap.Error(err), zap.String("job", config.Name))
}
}
func NewBasicAuthHandler(basicAuth *BasicAuth, chainedHandler http.Handler) http.Handler {
return &BasicAuthHandler{
basicAuth: basicAuth,
chainedHandler: chainedHandler,
}
}
type BasicAuthHandler struct {
basicAuth *BasicAuth
chainedHandler http.Handler
}
func (h *BasicAuthHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
reqUser, reqPass, ok := req.BasicAuth()
if !ok || subtle.ConstantTimeCompare([]byte(reqUser), []byte(h.basicAuth.Username)) != 1 || subtle.ConstantTimeCompare([]byte(reqPass), []byte(h.basicAuth.Password)) != 1 {
rw.Header().Set("WWW-Authenticate", `Basic realm="Please enter credentials"`)
rw.WriteHeader(401)
_, _ = rw.Write([]byte("Unauthorised.\n"))
return
}
h.chainedHandler.ServeHTTP(rw, req)
}