Skip to content

Commit c4adb51

Browse files
committed
UPSTREAM: <carry>: kube-apiserver: ignore SIGTERM/INT after the first one
1 parent e509061 commit c4adb51

File tree

7 files changed

+20
-11
lines changed

7 files changed

+20
-11
lines changed

cmd/kube-apiserver/app/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ cluster's shared state through which all other components interact.`,
178178
return utilerrors.NewAggregate(errs)
179179
}
180180

181-
return Run(completedOptions, genericapiserver.SetupSignalHandler())
181+
return Run(completedOptions, genericapiserver.SetupSignalHandler(false))
182182
},
183183
Args: func(cmd *cobra.Command, args []string) error {
184184
for _, arg := range args {

cmd/kube-controller-manager/app/controllermanager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ controller, and serviceaccounts controller.`,
126126
os.Exit(1)
127127
}
128128

129-
stopCh := server.SetupSignalHandler()
129+
stopCh := server.SetupSignalHandler(true)
130130
if err := Run(c.Complete(), stopCh); err != nil {
131131
fmt.Fprintf(os.Stderr, "%v\n", err)
132132
os.Exit(1)

cmd/kube-scheduler/app/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func runCommand(cmd *cobra.Command, opts *options.Options, registryOptions ...Op
124124
ctx, cancel := context.WithCancel(context.Background())
125125
defer cancel()
126126
go func() {
127-
stopCh := server.SetupSignalHandler()
127+
stopCh := server.SetupSignalHandler(true)
128128
<-stopCh
129129
cancel()
130130
}()

cmd/kubelet/app/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ HTTP server: The kubelet can also listen for HTTP and respond to a simple API
261261
kubeletDeps.KubeletConfigController = kubeletConfigController
262262

263263
// set up signal context here in order to be reused by kubelet and docker shim
264-
ctx := genericapiserver.SetupSignalContext()
264+
ctx := genericapiserver.SetupSignalContext(true)
265265

266266
// run the kubelet
267267
klog.V(5).Infof("KubeletConfiguration: %#v", kubeletServer.KubeletConfiguration)

staging/src/k8s.io/apiextensions-apiserver/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func main() {
3131
logs.InitLogs()
3232
defer logs.FlushLogs()
3333

34-
stopCh := genericapiserver.SetupSignalHandler()
34+
stopCh := genericapiserver.SetupSignalHandler(true)
3535
cmd := server.NewServerCommand(os.Stdout, os.Stderr, stopCh)
3636
cmd.Flags().AddGoFlagSet(flag.CommandLine)
3737
if err := cmd.Execute(); err != nil {

staging/src/k8s.io/apiserver/pkg/server/signal.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"context"
2121
"os"
2222
"os/signal"
23+
24+
"k8s.io/klog/v2"
2325
)
2426

2527
var onlyOneSignalHandler = make(chan struct{})
@@ -30,14 +32,14 @@ var shutdownHandler chan os.Signal
3032
// is terminated with exit code 1.
3133
// Only one of SetupSignalContext and SetupSignalHandler should be called, and only can
3234
// be called once.
33-
func SetupSignalHandler() <-chan struct{} {
34-
return SetupSignalContext().Done()
35+
func SetupSignalHandler(exitOnSecondSignal bool) <-chan struct{} {
36+
return SetupSignalContext(exitOnSecondSignal).Done()
3537
}
3638

3739
// SetupSignalContext is same as SetupSignalHandler, but a context.Context is returned.
3840
// Only one of SetupSignalContext and SetupSignalHandler should be called, and only can
3941
// be called once.
40-
func SetupSignalContext() context.Context {
42+
func SetupSignalContext(exitOnSecondSignal bool) context.Context {
4143
close(onlyOneSignalHandler) // panics when called twice
4244

4345
shutdownHandler = make(chan os.Signal, 2)
@@ -47,8 +49,15 @@ func SetupSignalContext() context.Context {
4749
go func() {
4850
<-shutdownHandler
4951
cancel()
50-
<-shutdownHandler
51-
os.Exit(1) // second signal. Exit directly.
52+
if exitOnSecondSignal {
53+
<-shutdownHandler
54+
os.Exit(1)
55+
} else {
56+
for {
57+
<-shutdownHandler
58+
klog.Infof("Termination signal has been received already. Ignoring signal.")
59+
}
60+
}
5261
}()
5362

5463
return ctx

staging/src/k8s.io/kube-aggregator/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func main() {
3838
logs.InitLogs()
3939
defer logs.FlushLogs()
4040

41-
stopCh := genericapiserver.SetupSignalHandler()
41+
stopCh := genericapiserver.SetupSignalHandler(true)
4242
options := server.NewDefaultOptions(os.Stdout, os.Stderr)
4343
cmd := server.NewCommandStartAggregator(options, stopCh)
4444
cmd.Flags().AddGoFlagSet(flag.CommandLine)

0 commit comments

Comments
 (0)