Graceful shutdown for admin server (#6817)

* Graceful shutdown for admin server

Signed-off-by: Stepan Rabotkin <epicstyt@gmail.com>
This commit is contained in:
Stepan Rabotkin 2021-09-07 18:50:31 +03:00 committed by GitHub
parent 6847e6a042
commit 5e6a1b5508
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 60 additions and 13 deletions

View File

@ -131,11 +131,17 @@ func Main(args []string) {
server.Serve(lis) server.Serve(lis)
}() }()
go admin.StartServer(*metricsAddr) adminServer := admin.NewServer(*metricsAddr)
go func() {
log.Infof("starting admin server on %s", *metricsAddr)
adminServer.ListenAndServe()
}()
<-stop <-stop
log.Infof("shutting down gRPC server on %s", *addr) log.Infof("shutting down gRPC server on %s", *addr)
close(done) close(done)
server.GracefulStop() server.GracefulStop()
adminServer.Shutdown(ctx)
} }

View File

@ -167,7 +167,13 @@ func Main(args []string) {
// //
// Bind and serve // Bind and serve
// //
go admin.StartServer(*adminAddr) adminServer := admin.NewServer(*adminAddr)
go func() {
log.Infof("starting admin server on %s", *adminAddr)
adminServer.ListenAndServe()
}()
lis, err := net.Listen("tcp", *addr) lis, err := net.Listen("tcp", *addr)
if err != nil { if err != nil {
log.Fatalf("Failed to listen on %s: %s", *addr, err) log.Fatalf("Failed to listen on %s: %s", *addr, err)
@ -187,4 +193,5 @@ func Main(args []string) {
<-stop <-stop
log.Infof("shutting down gRPC server on %s", *addr) log.Infof("shutting down gRPC server on %s", *addr)
srv.GracefulStop() srv.GracefulStop()
adminServer.Shutdown(ctx)
} }

View File

@ -40,7 +40,13 @@ func Launch(
go s.Start() go s.Start()
k8sAPI.Sync(nil) k8sAPI.Sync(nil)
go admin.StartServer(metricsAddr)
adminServer := admin.NewServer(metricsAddr)
go func() {
log.Infof("starting admin server on %s", metricsAddr)
adminServer.ListenAndServe()
}()
<-stop <-stop
log.Info("shutting down webhook server") log.Info("shutting down webhook server")
@ -49,4 +55,6 @@ func Launch(
if err := s.Shutdown(ctx); err != nil { if err := s.Shutdown(ctx); err != nil {
log.Error(err) log.Error(err)
} }
adminServer.Shutdown(ctx)
} }

View File

@ -75,7 +75,13 @@ func Main(args []string) {
linkClient := k8sAPI.DynamicClient.Resource(multicluster.LinkGVR).Namespace(*namespace) linkClient := k8sAPI.DynamicClient.Resource(multicluster.LinkGVR).Namespace(*namespace)
metrics := servicemirror.NewProbeMetricVecs() metrics := servicemirror.NewProbeMetricVecs()
go admin.StartServer(*metricsAddr)
adminServer := admin.NewServer(*metricsAddr)
go func() {
log.Infof("starting admin server on %s", *metricsAddr)
adminServer.ListenAndServe()
}()
controllerK8sAPI.Sync(nil) controllerK8sAPI.Sync(nil)

View File

@ -7,22 +7,22 @@ import (
"strings" "strings"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
) )
type handler struct { type handler struct {
promHandler http.Handler promHandler http.Handler
} }
// StartServer starts an admin server listening on a given address. // NewServer returns an initialized `http.Server`, configured to listen on an address.
func StartServer(addr string) { func NewServer(addr string) *http.Server {
log.Infof("starting admin server on %s", addr)
h := &handler{ h := &handler{
promHandler: promhttp.Handler(), promHandler: promhttp.Handler(),
} }
log.Fatal(http.ListenAndServe(addr, h)) return &http.Server{
Addr: addr,
Handler: h,
}
} }
func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {

View File

@ -79,10 +79,16 @@ func main() {
server.ListenAndServe() server.ListenAndServe()
}() }()
go admin.StartServer(*metricsAddr) adminServer := admin.NewServer(*metricsAddr)
go func() {
log.Infof("starting admin server on %s", *metricsAddr)
adminServer.ListenAndServe()
}()
<-stop <-stop
log.Infof("shutting down HTTP server on %+v", *addr) log.Infof("shutting down HTTP server on %+v", *addr)
server.Shutdown(ctx) server.Shutdown(ctx)
adminServer.Shutdown(ctx)
} }

View File

@ -63,8 +63,16 @@ func Main(args []string) {
} }
k8sAPI.Sync(nil) k8sAPI.Sync(nil)
go apiServer.Start(ctx) go apiServer.Start(ctx)
go admin.StartServer(*metricsAddr)
adminServer := admin.NewServer(*metricsAddr)
go func() {
log.Infof("starting admin server on %s", *metricsAddr)
adminServer.ListenAndServe()
}()
<-stop <-stop
log.Infof("shutting down APIServer on %s", *apiServerAddr) log.Infof("shutting down APIServer on %s", *apiServerAddr)
apiServer.Shutdown(ctx) apiServer.Shutdown(ctx)
adminServer.Shutdown(ctx)
} }

View File

@ -100,7 +100,12 @@ func main() {
server.ListenAndServe() server.ListenAndServe()
}() }()
go admin.StartServer(*metricsAddr) adminServer := admin.NewServer(*metricsAddr)
go func() {
log.Infof("starting admin server on %s", *metricsAddr)
adminServer.ListenAndServe()
}()
<-stop <-stop
@ -108,6 +113,7 @@ func main() {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second) ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel() defer cancel()
server.Shutdown(ctx) server.Shutdown(ctx)
adminServer.Shutdown(ctx)
} }
func getUUIDAndVersion(ctx context.Context, k8sAPI *k8s.KubernetesAPI, controllerNamespace string) (string, string) { func getUUIDAndVersion(ctx context.Context, k8sAPI *k8s.KubernetesAPI, controllerNamespace string) (string, string) {