Skip to content

Commit 66c8ea5

Browse files
committed
main: add top-level context and graceful shutdown
Adds a top-level context to be passed down from main, as well as an interrupt handler with graceful shutdown logic. The signal handler allows us to trap a user interrupt and run graceful shutdown logic rather than exiting immediately. This graceful shutdown allows us to run any cleanup operations, particularly we can shutdown locally-running CAPI controller processes. Otherise they can potentially leak and continue running in the background (continuing to perform reconcile actions such as creating cloud resources).
1 parent a6f102c commit 66c8ea5

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

cmd/openshift-install/create.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const (
6868
exitCodeBootstrapFailed
6969
exitCodeInstallFailed
7070
exitCodeOperatorStabilityFailed
71+
exitCodeInterrupt
7172

7273
// coStabilityThreshold is how long a cluster operator must have Progressing=False
7374
// in order to be considered stable. Measured in seconds.

cmd/openshift-install/main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56
"io"
67
"os"
@@ -12,8 +13,10 @@ import (
1213
terminal "golang.org/x/term"
1314
"k8s.io/klog"
1415
klogv2 "k8s.io/klog/v2"
16+
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
1517

1618
"github.com/openshift/installer/cmd/openshift-install/command"
19+
"github.com/openshift/installer/pkg/clusterapi"
1720
)
1821

1922
func main() {
@@ -36,6 +39,10 @@ func main() {
3639
func installerMain() {
3740
rootCmd := newRootCmd()
3841

42+
// Perform a graceful shutdown upon interrupt or at exit.
43+
ctx := handleInterrupt(signals.SetupSignalHandler())
44+
logrus.RegisterExitHandler(shutdown)
45+
3946
for _, subCmd := range []*cobra.Command{
4047
newCreateCmd(),
4148
newDestroyCmd(),
@@ -96,3 +103,26 @@ func runRootCmd(cmd *cobra.Command, args []string) {
96103
logrus.Fatal(errors.Wrap(err, "invalid log-level"))
97104
}
98105
}
106+
107+
// handleInterrupt executes a graceful shutdown then exits in
108+
// the case of a user interrupt. It returns a new context that
109+
// will be cancelled upon interrupt.
110+
func handleInterrupt(signalCtx context.Context) context.Context {
111+
ctx, cancel := context.WithCancel(context.Background())
112+
113+
// If the context from the signal handler is done,
114+
// an interrupt has been received, so shutdown & exit.
115+
go func() {
116+
<-signalCtx.Done()
117+
logrus.Warn("Received interrupt signal")
118+
shutdown()
119+
cancel()
120+
logrus.Exit(exitCodeInterrupt)
121+
}()
122+
123+
return ctx
124+
}
125+
126+
func shutdown() {
127+
clusterapi.System().Teardown()
128+
}

0 commit comments

Comments
 (0)