@@ -128,12 +128,10 @@ var (
128128 Short : "Create an OpenShift cluster" ,
129129 // FIXME: add longer descriptions for our commands with examples for better UX.
130130 // Long: "",
131- PostRun : func (_ * cobra.Command , _ []string ) {
132- // Setup a context that is canceled when the user presses Ctrl+C,
133- // or SIGTERM and SIGINT are received, this allows for a clean shutdown.
134- ctx , cancel := context .WithCancel (context .TODO ())
135- defer cancel ()
136- logrus .RegisterExitHandler (cancel )
131+ PostRun : func (cmd * cobra.Command , _ []string ) {
132+
133+ // Get the context that was set in newCreateCmd.
134+ ctx := cmd .Context ()
137135
138136 exitCode , err := clusterCreatePostRun (ctx )
139137 if err != nil {
@@ -168,7 +166,7 @@ func clusterCreatePostRun(ctx context.Context) (int, error) {
168166 }
169167
170168 // Handle the case when the API server is not reachable.
171- if err := handleUnreachableAPIServer (config ); err != nil {
169+ if err := handleUnreachableAPIServer (ctx , config ); err != nil {
172170 logrus .Fatal (fmt .Errorf ("unable to handle api server override: %w" , err ))
173171 }
174172
@@ -177,7 +175,7 @@ func clusterCreatePostRun(ctx context.Context) (int, error) {
177175 //
178176 timer .StartTimer ("Bootstrap Complete" )
179177 if err := waitForBootstrapComplete (ctx , config ); err != nil {
180- bundlePath , gatherErr := runGatherBootstrapCmd (command .RootOpts .Dir )
178+ bundlePath , gatherErr := runGatherBootstrapCmd (ctx , command .RootOpts .Dir )
181179 if gatherErr != nil {
182180 logrus .Error ("Attempted to gather debug logs after installation failure: " , gatherErr )
183181 }
@@ -278,7 +276,7 @@ func newClientError(errorInfo error) *clusterCreateError {
278276 }
279277}
280278
281- func newCreateCmd () * cobra.Command {
279+ func newCreateCmd (ctx context. Context ) * cobra.Command {
282280 cmd := & cobra.Command {
283281 Use : "create" ,
284282 Short : "Create part of an OpenShift cluster" ,
@@ -289,22 +287,25 @@ func newCreateCmd() *cobra.Command {
289287
290288 for _ , t := range targets {
291289 t .command .Args = cobra .ExactArgs (0 )
292- t .command .Run = runTargetCmd (t .assets ... )
290+ t .command .Run = runTargetCmd (ctx , t .assets ... )
293291 cmd .AddCommand (t .command )
294292 }
295293
296294 return cmd
297295}
298296
299- 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 ) {
300298 runner := func (directory string ) error {
301299 fetcher := assetstore .NewAssetsFetcher (directory )
302- return fetcher .FetchAndPersist (targets )
300+ return fetcher .FetchAndPersist (ctx , targets )
303301 }
304302
305303 return func (cmd * cobra.Command , args []string ) {
306304 timer .StartTimer (timer .TotalTimeElapsed )
307305
306+ // Set the context to be used in the PostRun function.
307+ cmd .SetContext (ctx )
308+
308309 cleanup := command .SetupFileHook (command .RootOpts .Dir )
309310 defer cleanup ()
310311
@@ -856,15 +857,15 @@ func meetsStabilityThreshold(progressing *configv1.ClusterOperatorStatusConditio
856857 return progressing .Status == configv1 .ConditionFalse && time .Since (progressing .LastTransitionTime .Time ).Seconds () > coStabilityThreshold
857858}
858859
859- func handleUnreachableAPIServer (config * rest.Config ) error {
860+ func handleUnreachableAPIServer (ctx context. Context , config * rest.Config ) error {
860861 assetStore , err := assetstore .NewStore (command .RootOpts .Dir )
861862 if err != nil {
862863 return fmt .Errorf ("failed to create asset store: %w" , err )
863864 }
864865
865866 // Ensure that the install is expecting the user to provision their own DNS solution.
866867 installConfig := & installconfig.InstallConfig {}
867- if err := assetStore .Fetch (installConfig ); err != nil {
868+ if err := assetStore .Fetch (ctx , installConfig ); err != nil {
868869 return fmt .Errorf ("failed to fetch %s: %w" , installConfig .Name (), err )
869870 }
870871 switch installConfig .Config .Platform .Name () { //nolint:gocritic
@@ -877,7 +878,7 @@ func handleUnreachableAPIServer(config *rest.Config) error {
877878 }
878879
879880 lbConfig := & lbconfig.Config {}
880- if err := assetStore .Fetch (lbConfig ); err != nil {
881+ if err := assetStore .Fetch (ctx , lbConfig ); err != nil {
881882 return fmt .Errorf ("failed to fetch %s: %w" , lbConfig .Name (), err )
882883 }
883884
0 commit comments