From 5a1d65ef4ce99b297043167b240b6545f88ba287 Mon Sep 17 00:00:00 2001 From: Mike Dame Date: Thu, 11 Mar 2021 12:57:31 -0500 Subject: [PATCH] UPSTREAM: : kube-apiserver: set up separate signal handler functions to ignore further signals This patches the changes from https://github.com/openshift/kubernetes/pull/558 to provide these new functions without changing the behavior for other repos that depend on them, such as library-go. --- cmd/kube-apiserver/app/server.go | 2 +- .../app/controllermanager.go | 2 +- cmd/kube-scheduler/app/server.go | 2 +- cmd/kubelet/app/server.go | 2 +- .../k8s.io/apiextensions-apiserver/main.go | 2 +- .../src/k8s.io/apiserver/pkg/server/signal.go | 22 ++++++++++++++++--- staging/src/k8s.io/kube-aggregator/main.go | 2 +- 7 files changed, 25 insertions(+), 9 deletions(-) diff --git a/cmd/kube-apiserver/app/server.go b/cmd/kube-apiserver/app/server.go index 4cb510bcf7276..36856aa32a35d 100644 --- a/cmd/kube-apiserver/app/server.go +++ b/cmd/kube-apiserver/app/server.go @@ -178,7 +178,7 @@ cluster's shared state through which all other components interact.`, return utilerrors.NewAggregate(errs) } - return Run(completedOptions, genericapiserver.SetupSignalHandler(false)) + return Run(completedOptions, genericapiserver.SetupSignalHandlerIgnoringFurtherSignals()) }, Args: func(cmd *cobra.Command, args []string) error { for _, arg := range args { diff --git a/cmd/kube-controller-manager/app/controllermanager.go b/cmd/kube-controller-manager/app/controllermanager.go index 9a3d04c734995..9a933d9b3d3a4 100644 --- a/cmd/kube-controller-manager/app/controllermanager.go +++ b/cmd/kube-controller-manager/app/controllermanager.go @@ -126,7 +126,7 @@ controller, and serviceaccounts controller.`, os.Exit(1) } - stopCh := server.SetupSignalHandler(true) + stopCh := server.SetupSignalHandler() if err := Run(c.Complete(), stopCh); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) diff --git a/cmd/kube-scheduler/app/server.go b/cmd/kube-scheduler/app/server.go index 0da62b9bac06f..4b03016f7ada9 100644 --- a/cmd/kube-scheduler/app/server.go +++ b/cmd/kube-scheduler/app/server.go @@ -124,7 +124,7 @@ func runCommand(cmd *cobra.Command, opts *options.Options, registryOptions ...Op ctx, cancel := context.WithCancel(context.Background()) defer cancel() go func() { - stopCh := server.SetupSignalHandler(true) + stopCh := server.SetupSignalHandler() <-stopCh cancel() }() diff --git a/cmd/kubelet/app/server.go b/cmd/kubelet/app/server.go index 8594ad293bc67..ff7e05feec125 100644 --- a/cmd/kubelet/app/server.go +++ b/cmd/kubelet/app/server.go @@ -261,7 +261,7 @@ HTTP server: The kubelet can also listen for HTTP and respond to a simple API kubeletDeps.KubeletConfigController = kubeletConfigController // set up signal context here in order to be reused by kubelet and docker shim - ctx := genericapiserver.SetupSignalContext(true) + ctx := genericapiserver.SetupSignalContext() // run the kubelet klog.V(5).Infof("KubeletConfiguration: %#v", kubeletServer.KubeletConfiguration) diff --git a/staging/src/k8s.io/apiextensions-apiserver/main.go b/staging/src/k8s.io/apiextensions-apiserver/main.go index fb6d946171c85..a4784e14ff475 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/main.go +++ b/staging/src/k8s.io/apiextensions-apiserver/main.go @@ -31,7 +31,7 @@ func main() { logs.InitLogs() defer logs.FlushLogs() - stopCh := genericapiserver.SetupSignalHandler(true) + stopCh := genericapiserver.SetupSignalHandler() cmd := server.NewServerCommand(os.Stdout, os.Stderr, stopCh) cmd.Flags().AddGoFlagSet(flag.CommandLine) if err := cmd.Execute(); err != nil { diff --git a/staging/src/k8s.io/apiserver/pkg/server/signal.go b/staging/src/k8s.io/apiserver/pkg/server/signal.go index 89cc4ce8f6b59..bdd2728f8abf8 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/signal.go +++ b/staging/src/k8s.io/apiserver/pkg/server/signal.go @@ -32,14 +32,30 @@ var shutdownHandler chan os.Signal // is terminated with exit code 1. // Only one of SetupSignalContext and SetupSignalHandler should be called, and only can // be called once. -func SetupSignalHandler(exitOnSecondSignal bool) <-chan struct{} { - return SetupSignalContext(exitOnSecondSignal).Done() +func SetupSignalHandler() <-chan struct{} { + return SetupSignalContext().Done() +} + +// SetupSignalHandlerIgnoringFurtherSignals is the same as SetupSignalContext, except +// it ignores further exit signals after receiving the first one. +func SetupSignalHandlerIgnoringFurtherSignals() <-chan struct{} { + return SetupSignalContextNotExiting().Done() } // SetupSignalContext is same as SetupSignalHandler, but a context.Context is returned. // Only one of SetupSignalContext and SetupSignalHandler should be called, and only can // be called once. -func SetupSignalContext(exitOnSecondSignal bool) context.Context { +func SetupSignalContext() context.Context { + return setupSignalContext(true) +} + +// SetupSignalContextNotExiting is the same as SetupSignalContext, except +// it ignores further exit signals after receiving the first one. +func SetupSignalContextNotExiting() context.Context { + return setupSignalContext(false) +} + +func setupSignalContext(exitOnSecondSignal bool) context.Context { close(onlyOneSignalHandler) // panics when called twice shutdownHandler = make(chan os.Signal, 2) diff --git a/staging/src/k8s.io/kube-aggregator/main.go b/staging/src/k8s.io/kube-aggregator/main.go index 4e8a5c489e472..e7575e96d0cf8 100644 --- a/staging/src/k8s.io/kube-aggregator/main.go +++ b/staging/src/k8s.io/kube-aggregator/main.go @@ -38,7 +38,7 @@ func main() { logs.InitLogs() defer logs.FlushLogs() - stopCh := genericapiserver.SetupSignalHandler(true) + stopCh := genericapiserver.SetupSignalHandler() options := server.NewDefaultOptions(os.Stdout, os.Stderr) cmd := server.NewCommandStartAggregator(options, stopCh) cmd.Flags().AddGoFlagSet(flag.CommandLine)