Skip to content

Commit

Permalink
UPSTREAM: <carry>: kube-apiserver: set up separate signal handler fun…
Browse files Browse the repository at this point in the history
…ctions to ignore further signals

This patches the changes from #558 to provide
these new functions without changing the behavior for other repos that depend on them, such
as library-go.
  • Loading branch information
damemi authored and soltysh committed Sep 8, 2021
1 parent a85e535 commit 5a1d65e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/kube-apiserver/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/kube-controller-manager/app/controllermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kube-scheduler/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}()
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubelet/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion staging/src/k8s.io/apiextensions-apiserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 19 additions & 3 deletions staging/src/k8s.io/apiserver/pkg/server/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion staging/src/k8s.io/kube-aggregator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5a1d65e

Please sign in to comment.