Skip to content

Commit

Permalink
UPSTREAM: <carry>: kube-apiserver: ignore SIGTERM/INT after the first…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
sttts authored and damemi committed Aug 27, 2021
1 parent 72a445e commit 3b95ba3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/kube-apiserver/app/server.go
Expand Up @@ -167,7 +167,7 @@ cluster's shared state through which all other components interact.`,
return utilerrors.NewAggregate(errs)
}

return Run(completedOptions, genericapiserver.SetupSignalHandler())
return Run(completedOptions, genericapiserver.SetupSignalHandlerIgnoringFurtherSignals())
},
Args: func(cmd *cobra.Command, args []string) error {
for _, arg := range args {
Expand Down
29 changes: 27 additions & 2 deletions staging/src/k8s.io/apiserver/pkg/server/signal.go
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"os"
"os/signal"

"k8s.io/klog/v2"
)

var onlyOneSignalHandler = make(chan struct{})
Expand All @@ -34,10 +36,26 @@ 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() 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 All @@ -47,8 +65,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

0 comments on commit 3b95ba3

Please sign in to comment.