@@ -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.
@@ -127,12 +128,10 @@ var (
127128 Short : "Create an OpenShift cluster" ,
128129 // FIXME: add longer descriptions for our commands with examples for better UX.
129130 // Long: "",
130- PostRun : func (_ * cobra.Command , _ []string ) {
131- // Setup a context that is canceled when the user presses Ctrl+C,
132- // or SIGTERM and SIGINT are received, this allows for a clean shutdown.
133- ctx , cancel := context .WithCancel (context .TODO ())
134- defer cancel ()
135- logrus .RegisterExitHandler (cancel )
131+ PostRun : func (cmd * cobra.Command , _ []string ) {
132+
133+ // Get the context that was set in newCreateCmd.
134+ ctx := cmd .Context ()
136135
137136 exitCode , err := clusterCreatePostRun (ctx )
138137 if err != nil {
@@ -167,7 +166,7 @@ func clusterCreatePostRun(ctx context.Context) (int, error) {
167166 }
168167
169168 // Handle the case when the API server is not reachable.
170- if err := handleUnreachableAPIServer (config ); err != nil {
169+ if err := handleUnreachableAPIServer (ctx , config ); err != nil {
171170 logrus .Fatal (fmt .Errorf ("unable to handle api server override: %w" , err ))
172171 }
173172
@@ -176,7 +175,7 @@ func clusterCreatePostRun(ctx context.Context) (int, error) {
176175 //
177176 timer .StartTimer ("Bootstrap Complete" )
178177 if err := waitForBootstrapComplete (ctx , config ); err != nil {
179- bundlePath , gatherErr := runGatherBootstrapCmd (command .RootOpts .Dir )
178+ bundlePath , gatherErr := runGatherBootstrapCmd (ctx , command .RootOpts .Dir )
180179 if gatherErr != nil {
181180 logrus .Error ("Attempted to gather debug logs after installation failure: " , gatherErr )
182181 }
@@ -277,7 +276,7 @@ func newClientError(errorInfo error) *clusterCreateError {
277276 }
278277}
279278
280- func newCreateCmd () * cobra.Command {
279+ func newCreateCmd (ctx context. Context ) * cobra.Command {
281280 cmd := & cobra.Command {
282281 Use : "create" ,
283282 Short : "Create part of an OpenShift cluster" ,
@@ -288,22 +287,25 @@ func newCreateCmd() *cobra.Command {
288287
289288 for _ , t := range targets {
290289 t .command .Args = cobra .ExactArgs (0 )
291- t .command .Run = runTargetCmd (t .assets ... )
290+ t .command .Run = runTargetCmd (ctx , t .assets ... )
292291 cmd .AddCommand (t .command )
293292 }
294293
295294 return cmd
296295}
297296
298- func runTargetCmd (targets ... asset.WritableAsset ) func (cmd * cobra.Command , args []string ) {
297+ func runTargetCmd (ctx context. Context , targets ... asset.WritableAsset ) func (cmd * cobra.Command , args []string ) {
299298 runner := func (directory string ) error {
300299 fetcher := assetstore .NewAssetsFetcher (directory )
301- return fetcher .FetchAndPersist (targets )
300+ return fetcher .FetchAndPersist (ctx , targets )
302301 }
303302
304303 return func (cmd * cobra.Command , args []string ) {
305304 timer .StartTimer (timer .TotalTimeElapsed )
306305
306+ // Set the context to be used in the PostRun function.
307+ cmd .SetContext (ctx )
308+
307309 cleanup := command .SetupFileHook (command .RootOpts .Dir )
308310 defer cleanup ()
309311
@@ -855,15 +857,15 @@ func meetsStabilityThreshold(progressing *configv1.ClusterOperatorStatusConditio
855857 return progressing .Status == configv1 .ConditionFalse && time .Since (progressing .LastTransitionTime .Time ).Seconds () > coStabilityThreshold
856858}
857859
858- func handleUnreachableAPIServer (config * rest.Config ) error {
860+ func handleUnreachableAPIServer (ctx context. Context , config * rest.Config ) error {
859861 assetStore , err := assetstore .NewStore (command .RootOpts .Dir )
860862 if err != nil {
861863 return fmt .Errorf ("failed to create asset store: %w" , err )
862864 }
863865
864866 // Ensure that the install is expecting the user to provision their own DNS solution.
865867 installConfig := & installconfig.InstallConfig {}
866- if err := assetStore .Fetch (installConfig ); err != nil {
868+ if err := assetStore .Fetch (ctx , installConfig ); err != nil {
867869 return fmt .Errorf ("failed to fetch %s: %w" , installConfig .Name (), err )
868870 }
869871 switch installConfig .Config .Platform .Name () { //nolint:gocritic
@@ -876,7 +878,7 @@ func handleUnreachableAPIServer(config *rest.Config) error {
876878 }
877879
878880 lbConfig := & lbconfig.Config {}
879- if err := assetStore .Fetch (lbConfig ); err != nil {
881+ if err := assetStore .Fetch (ctx , lbConfig ); err != nil {
880882 return fmt .Errorf ("failed to fetch %s: %w" , lbConfig .Name (), err )
881883 }
882884
0 commit comments