Skip to content

Commit 63ed200

Browse files
stttssoltysh
authored andcommitted
UPSTREAM: <carry>: kube-apiserver: ignore SIGTERM/INT after the first one
UPSTREAM: <carry>: kube-apiserver: set up separate signal handler functions 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. openshift-rebase(v1.24):source=bfea75be7a2
1 parent c56aa0b commit 63ed200

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

cmd/kube-apiserver/app/server.go

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

171-
return Run(completedOptions, genericapiserver.SetupSignalHandler())
171+
return Run(completedOptions, genericapiserver.SetupSignalHandlerIgnoringFurtherSignals())
172172
},
173173
Args: func(cmd *cobra.Command, args []string) error {
174174
for _, arg := range args {

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

Lines changed: 27 additions & 2 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{})
@@ -34,10 +36,26 @@ func SetupSignalHandler() <-chan struct{} {
3436
return SetupSignalContext().Done()
3537
}
3638

39+
// SetupSignalHandlerIgnoringFurtherSignals is the same as SetupSignalContext, except
40+
// it ignores further exit signals after receiving the first one.
41+
func SetupSignalHandlerIgnoringFurtherSignals() <-chan struct{} {
42+
return SetupSignalContextNotExiting().Done()
43+
}
44+
3745
// SetupSignalContext is same as SetupSignalHandler, but a context.Context is returned.
3846
// Only one of SetupSignalContext and SetupSignalHandler should be called, and only can
3947
// be called once.
4048
func SetupSignalContext() context.Context {
49+
return setupSignalContext(true)
50+
}
51+
52+
// SetupSignalContextNotExiting is the same as SetupSignalContext, except
53+
// it ignores further exit signals after receiving the first one.
54+
func SetupSignalContextNotExiting() context.Context {
55+
return setupSignalContext(false)
56+
}
57+
58+
func setupSignalContext(exitOnSecondSignal bool) context.Context {
4159
close(onlyOneSignalHandler) // panics when called twice
4260

4361
shutdownHandler = make(chan os.Signal, 2)
@@ -47,8 +65,15 @@ func SetupSignalContext() context.Context {
4765
go func() {
4866
<-shutdownHandler
4967
cancel()
50-
<-shutdownHandler
51-
os.Exit(1) // second signal. Exit directly.
68+
if exitOnSecondSignal {
69+
<-shutdownHandler
70+
os.Exit(1)
71+
} else {
72+
for {
73+
<-shutdownHandler
74+
klog.Infof("Termination signal has been received already. Ignoring signal.")
75+
}
76+
}
5277
}()
5378

5479
return ctx

0 commit comments

Comments
 (0)