Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 1926484: UPSTREAM: <carry>: kube-apiserver: ignore SIGTERM/INT after the first one #558

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a temporary fix?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Temporary in the sense of a downstream carry.

See description for background.

<-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