Skip to content

Commit

Permalink
Merge pull request #558 from sttts/sttts-ignore-second-signal
Browse files Browse the repository at this point in the history
Bug 1926484: UPSTREAM: <carry>: kube-apiserver: ignore SIGTERM/INT after the first one
  • Loading branch information
openshift-merge-robot committed Feb 10, 2021
2 parents 52d6e7e + c4adb51 commit 070a94d
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 11 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())
return Run(completedOptions, genericapiserver.SetupSignalHandler(false))
},
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()
stopCh := server.SetupSignalHandler(true)
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()
stopCh := server.SetupSignalHandler(true)
<-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()
ctx := genericapiserver.SetupSignalContext(true)

// 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()
stopCh := genericapiserver.SetupSignalHandler(true)
cmd := server.NewServerCommand(os.Stdout, os.Stderr, stopCh)
cmd.Flags().AddGoFlagSet(flag.CommandLine)
if err := cmd.Execute(); err != nil {
Expand Down
19 changes: 14 additions & 5 deletions staging/src/k8s.io/apiserver/pkg/server/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"os"
"os/signal"

"k8s.io/klog/v2"
)

var onlyOneSignalHandler = make(chan struct{})
Expand All @@ -30,14 +32,14 @@ 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() <-chan struct{} {
return SetupSignalContext().Done()
func SetupSignalHandler(exitOnSecondSignal bool) <-chan struct{} {
return SetupSignalContext(exitOnSecondSignal).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() context.Context {
func SetupSignalContext(exitOnSecondSignal bool) context.Context {
close(onlyOneSignalHandler) // panics when called twice

shutdownHandler = make(chan os.Signal, 2)
Expand All @@ -47,8 +49,15 @@ func SetupSignalContext() context.Context {
go func() {
<-shutdownHandler
cancel()
<-shutdownHandler
os.Exit(1) // second signal. Exit directly.
if exitOnSecondSignal {
<-shutdownHandler
os.Exit(1)
} else {
for {
<-shutdownHandler
klog.Infof("Termination signal has been received already. Ignoring signal.")
}
}
}()

return ctx
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()
stopCh := genericapiserver.SetupSignalHandler(true)
options := server.NewDefaultOptions(os.Stdout, os.Stderr)
cmd := server.NewCommandStartAggregator(options, stopCh)
cmd.Flags().AddGoFlagSet(flag.CommandLine)
Expand Down

0 comments on commit 070a94d

Please sign in to comment.