-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
main.go
52 lines (43 loc) · 1.24 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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
flag "github.com/spf13/pflag"
"golang.org/x/sys/unix"
healthDefaults "github.com/cilium/cilium/pkg/health/defaults"
"github.com/cilium/cilium/pkg/health/probe/responder"
"github.com/cilium/cilium/pkg/pidfile"
)
func main() {
var (
pidfilePath string
listen int
)
flag.StringVar(&pidfilePath, "pidfile", "", "Write pid to the specified file")
flag.IntVar(&listen, "listen", healthDefaults.HTTPPathPort, "Port on which the responder listens")
flag.Parse()
// Shutdown gracefully to halt server and remove pidfile
ctx, cancel := signal.NotifyContext(context.Background(), unix.SIGINT, unix.SIGHUP, unix.SIGTERM, unix.SIGQUIT)
srv := responder.NewServers([]string{""}, listen)
defer srv.Shutdown()
go func() {
if err := srv.Serve(); !errors.Is(err, http.ErrServerClosed) {
fmt.Fprintf(os.Stderr, "error while listening: %s\n", err.Error())
cancel()
}
}()
if pidfilePath != "" {
defer pidfile.Clean()
if err := pidfile.Write(pidfilePath); err != nil {
fmt.Fprintf(os.Stderr, "cannot write pidfile: %s: %s\n", pidfilePath, err.Error())
os.Exit(1)
}
}
<-ctx.Done()
}