github.com/cilium/cilium@v1.16.2/cilium-health/responder/main.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package main 5 6 import ( 7 "context" 8 "errors" 9 "fmt" 10 "net/http" 11 "os" 12 "os/signal" 13 14 flag "github.com/spf13/pflag" 15 "golang.org/x/sys/unix" 16 17 healthDefaults "github.com/cilium/cilium/pkg/health/defaults" 18 "github.com/cilium/cilium/pkg/health/probe/responder" 19 "github.com/cilium/cilium/pkg/pidfile" 20 ) 21 22 func main() { 23 var ( 24 pidfilePath string 25 listen int 26 ) 27 flag.StringVar(&pidfilePath, "pidfile", "", "Write pid to the specified file") 28 flag.IntVar(&listen, "listen", healthDefaults.HTTPPathPort, "Port on which the responder listens") 29 flag.Parse() 30 31 // Shutdown gracefully to halt server and remove pidfile 32 ctx, cancel := signal.NotifyContext(context.Background(), unix.SIGINT, unix.SIGHUP, unix.SIGTERM, unix.SIGQUIT) 33 34 srv := responder.NewServers([]string{""}, listen) 35 defer srv.Shutdown() 36 go func() { 37 if err := srv.Serve(); !errors.Is(err, http.ErrServerClosed) { 38 fmt.Fprintf(os.Stderr, "error while listening: %s\n", err.Error()) 39 cancel() 40 } 41 }() 42 43 if pidfilePath != "" { 44 defer pidfile.Clean() 45 if err := pidfile.Write(pidfilePath); err != nil { 46 fmt.Fprintf(os.Stderr, "cannot write pidfile: %s: %s\n", pidfilePath, err.Error()) 47 os.Exit(1) 48 } 49 } 50 51 <-ctx.Done() 52 }