From 9db8d2f731e0ddbfd093fd31a3e5a429d60e137e Mon Sep 17 00:00:00 2001 From: Damien Grisonnet Date: Mon, 29 Mar 2021 17:24:49 +0200 Subject: [PATCH] cmd/adapter: add signal handler Add a signal handler stopping the adapter if it receives a SIGINT or SIGTERM signal. This prevent the prometheus-adapter pod from being stuck in "Terminating" state. Signed-off-by: Damien Grisonnet --- cmd/adapter/adapter.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cmd/adapter/adapter.go b/cmd/adapter/adapter.go index efdc407c8..56a9505ba 100644 --- a/cmd/adapter/adapter.go +++ b/cmd/adapter/adapter.go @@ -27,7 +27,6 @@ import ( "os" "time" - "k8s.io/apimachinery/pkg/util/wait" openapinamer "k8s.io/apiserver/pkg/endpoints/openapi" genericapiserver "k8s.io/apiserver/pkg/server" "k8s.io/client-go/rest" @@ -280,8 +279,11 @@ func main() { klog.Fatalf("unable to load metrics discovery config: %v", err) } + // stop channel closed on SIGTERM and SIGINT + stopCh := genericapiserver.SetupSignalHandler() + // construct the provider - cmProvider, err := cmd.makeProvider(promClient, wait.NeverStop) + cmProvider, err := cmd.makeProvider(promClient, stopCh) if err != nil { klog.Fatalf("unable to construct custom metrics provider: %v", err) } @@ -292,7 +294,7 @@ func main() { } // construct the external provider - emProvider, err := cmd.makeExternalProvider(promClient, wait.NeverStop) + emProvider, err := cmd.makeExternalProvider(promClient, stopCh) if err != nil { klog.Fatalf("unable to construct external metrics provider: %v", err) } @@ -308,7 +310,7 @@ func main() { } // run the server - if err := cmd.Run(wait.NeverStop); err != nil { + if err := cmd.Run(stopCh); err != nil { klog.Fatalf("unable to run custom metrics adapter: %v", err) } }