Skip to content

Commit ee26f03

Browse files
committed
make sure all spinners close on error
1 parent 1fa1ba1 commit ee26f03

File tree

9 files changed

+127
-69
lines changed

9 files changed

+127
-69
lines changed

cmd/installer/cli/install.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,6 @@ func verifyNoInstallation(name string, cmdName string) error {
549549

550550
func initializeInstall(ctx context.Context, flags InstallCmdFlags) error {
551551
spinner := spinner.Start()
552-
553552
spinner.Infof("Initializing")
554553

555554
if err := materializeFiles(flags.airgapBundle); err != nil {
@@ -586,8 +585,7 @@ func initializeInstall(ctx context.Context, flags InstallCmdFlags) error {
586585
logrus.Debugf("unable to configure firewalld: %v", err)
587586
}
588587

589-
spinner.Infof("Initialization complete")
590-
spinner.Close()
588+
spinner.Closef("Initialization complete")
591589
return nil
592590
}
593591

@@ -619,37 +617,45 @@ func materializeFiles(airgapBundle string) error {
619617

620618
func installAndStartCluster(ctx context.Context, networkInterface string, airgapBundle string, proxy *ecv1beta1.ProxySpec, cidrCfg *CIDRConfig, overrides string, mutate func(*k0sv1beta1.ClusterConfig) error) (*k0sv1beta1.ClusterConfig, error) {
621619
loading := spinner.Start()
622-
defer loading.Close()
623620
loading.Infof("Installing node")
624621
logrus.Debugf("creating k0s configuration file")
625622

626623
cfg, err := k0s.WriteK0sConfig(ctx, networkInterface, airgapBundle, cidrCfg.PodCIDR, cidrCfg.ServiceCIDR, overrides, mutate)
627624
if err != nil {
625+
loading.Errorf("Failed to install node")
626+
loading.CloseWithError()
628627
return nil, fmt.Errorf("create config file: %w", err)
629628
}
630629
logrus.Debugf("creating systemd unit files")
631630
if err := createSystemdUnitFiles(ctx, false, proxy); err != nil {
631+
loading.Errorf("Failed to install node")
632+
loading.CloseWithError()
632633
return nil, fmt.Errorf("create systemd unit files: %w", err)
633634
}
634635

635636
logrus.Debugf("installing k0s")
636637
if err := k0s.Install(networkInterface); err != nil {
638+
loading.Errorf("Failed to install node")
639+
loading.CloseWithError()
637640
return nil, fmt.Errorf("install cluster: %w", err)
638641
}
639642

640-
loading.Infof("Waiting for node")
641-
642643
logrus.Debugf("waiting for k0s to be ready")
643644
if err := waitForK0s(); err != nil {
645+
loading.Errorf("Failed to install node")
646+
loading.CloseWithError()
644647
return nil, fmt.Errorf("wait for k0s: %w", err)
645648
}
646649

650+
loading.Infof("Waiting for node to be ready")
647651
logrus.Debugf("waiting for node to be ready")
648652
if err := waitForNode(ctx); err != nil {
653+
loading.Errorf("Failed to wait for node")
654+
loading.CloseWithError()
649655
return nil, fmt.Errorf("wait for node: %w", err)
650656
}
651657

652-
loading.Infof("Node installed")
658+
loading.Closef("Node is ready")
653659
return cfg, nil
654660
}
655661

cmd/installer/cli/join.go

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func runJoin(ctx context.Context, name string, flags JoinCmdFlags, jcmd *kotsadm
128128
// both controller and worker nodes will have 'worker' in the join command
129129
isWorker := !strings.Contains(jcmd.K0sJoinCommand, "controller")
130130
if !isWorker {
131-
logrus.Warnf("Do not join another node until this join is complete.")
131+
logrus.Warn("Do not join another node until this join is complete.")
132132
}
133133

134134
if err := runJoinVerifyAndPrompt(name, flags, jcmd); err != nil {
@@ -149,46 +149,57 @@ func runJoin(ctx context.Context, name string, flags JoinCmdFlags, jcmd *kotsadm
149149
}
150150

151151
logrus.Debugf("installing and joining cluster")
152+
loading := spinner.Start()
153+
loading.Infof("Installing node")
152154
if err := installAndJoinCluster(ctx, jcmd, name, flags, isWorker); err != nil {
155+
loading.Errorf("Failed to install node")
156+
loading.CloseWithError()
153157
return err
154158
}
155159

156160
kcli, err := kubeutils.KubeClient()
157161
if err != nil {
162+
loading.Errorf("Failed to install node")
163+
loading.CloseWithError()
158164
return fmt.Errorf("unable to get kube client: %w", err)
159165
}
160166

161167
hostname, err := os.Hostname()
162168
if err != nil {
169+
loading.Errorf("Failed to install node")
170+
loading.CloseWithError()
163171
return fmt.Errorf("unable to get hostname: %w", err)
164172
}
165173

166174
logrus.Debugf("waiting for node to join cluster")
175+
loading.Infof("Waiting for node")
167176
if err := waitForNodeToJoin(ctx, kcli, hostname, isWorker); err != nil {
177+
loading.Errorf("Failed to wait for node")
178+
loading.CloseWithError()
168179
return fmt.Errorf("unable to wait for node: %w", err)
169180
}
170181

182+
loading.Closef("Node is ready")
171183
if isWorker {
172184
logrus.Debugf("worker node join finished")
173185
return nil
174186
}
175187

176-
airgapChartsPath := ""
177-
if flags.isAirgap {
178-
airgapChartsPath = runtimeconfig.EmbeddedClusterChartsSubDir()
179-
}
180-
181-
hcli, err := helm.NewClient(helm.HelmOptions{
182-
KubeConfig: runtimeconfig.PathToKubeConfig(),
183-
K0sVersion: versions.K0sVersion,
184-
AirgapPath: airgapChartsPath,
185-
})
186-
if err != nil {
187-
return fmt.Errorf("unable to create helm client: %w", err)
188-
}
189-
defer hcli.Close()
190-
191188
if flags.enableHighAvailability {
189+
airgapChartsPath := ""
190+
if flags.isAirgap {
191+
airgapChartsPath = runtimeconfig.EmbeddedClusterChartsSubDir()
192+
}
193+
194+
hcli, err := helm.NewClient(helm.HelmOptions{
195+
KubeConfig: runtimeconfig.PathToKubeConfig(),
196+
K0sVersion: versions.K0sVersion,
197+
AirgapPath: airgapChartsPath,
198+
})
199+
if err != nil {
200+
return fmt.Errorf("unable to create helm client: %w", err)
201+
}
202+
defer hcli.Close()
192203
if err := maybeEnableHA(ctx, kcli, hcli, flags.isAirgap, cidrCfg.ServiceCIDR, jcmd.InstallationSpec.Proxy, jcmd.InstallationSpec.Config); err != nil {
193204
return fmt.Errorf("unable to enable high availability: %w", err)
194205
}
@@ -252,9 +263,17 @@ func runJoinVerifyAndPrompt(name string, flags JoinCmdFlags, jcmd *kotsadm.JoinC
252263
return nil
253264
}
254265

255-
func initializeJoin(ctx context.Context, name string, flags JoinCmdFlags, jcmd *kotsadm.JoinCommandResponse) (*CIDRConfig, error) {
266+
func initializeJoin(ctx context.Context, name string, flags JoinCmdFlags, jcmd *kotsadm.JoinCommandResponse) (cidrCfg *CIDRConfig, err error) {
256267
spinner := spinner.Start()
257268
spinner.Infof("Initializing")
269+
defer func() {
270+
if err != nil {
271+
spinner.Errorf("Initialization failed")
272+
spinner.CloseWithError()
273+
} else {
274+
spinner.Closef("Initialization complete")
275+
}
276+
}()
258277

259278
// set the umask to 022 so that we can create files/directories with 755 permissions
260279
// this does not return an error - it returns the previous umask
@@ -268,8 +287,6 @@ func initializeJoin(ctx context.Context, name string, flags JoinCmdFlags, jcmd *
268287

269288
logrus.Debugf("materializing %s binaries", name)
270289
if err := materializeFiles(flags.airgapBundle); err != nil {
271-
spinner.Errorf("Initialization failed")
272-
spinner.CloseWithError()
273290
return nil, err
274291
}
275292

@@ -285,15 +302,11 @@ func initializeJoin(ctx context.Context, name string, flags JoinCmdFlags, jcmd *
285302

286303
logrus.Debugf("configuring network manager")
287304
if err := configureNetworkManager(ctx); err != nil {
288-
spinner.Errorf("Initialization failed")
289-
spinner.CloseWithError()
290305
return nil, fmt.Errorf("unable to configure network manager: %w", err)
291306
}
292307

293-
cidrCfg, err := getJoinCIDRConfig(jcmd)
308+
cidrCfg, err = getJoinCIDRConfig(jcmd)
294309
if err != nil {
295-
spinner.Errorf("Initialization failed")
296-
spinner.CloseWithError()
297310
return nil, fmt.Errorf("unable to get join CIDR config: %w", err)
298311
}
299312

@@ -302,9 +315,6 @@ func initializeJoin(ctx context.Context, name string, flags JoinCmdFlags, jcmd *
302315
logrus.Debugf("unable to configure firewalld: %v", err)
303316
}
304317

305-
spinner.Infof("Initialization complete")
306-
spinner.Close()
307-
308318
return cidrCfg, nil
309319
}
310320

@@ -431,9 +441,6 @@ func applyNetworkConfiguration(networkInterface string, jcmd *kotsadm.JoinComman
431441

432442
// startAndWaitForK0s starts the k0s service and waits for the node to be ready.
433443
func startAndWaitForK0s(ctx context.Context, name string, jcmd *kotsadm.JoinCommandResponse) error {
434-
loading := spinner.Start()
435-
defer loading.Close()
436-
loading.Infof("Installing node")
437444
logrus.Debugf("starting %s service", name)
438445
if _, err := helpers.RunCommand(runtimeconfig.K0sBinaryPath(), "start"); err != nil {
439446
return fmt.Errorf("unable to start service: %w", err)
@@ -444,7 +451,6 @@ func startAndWaitForK0s(ctx context.Context, name string, jcmd *kotsadm.JoinComm
444451
return fmt.Errorf("unable to wait for node: %w", err)
445452
}
446453

447-
loading.Infof("Node installed")
448454
return nil
449455
}
450456

@@ -503,13 +509,9 @@ func runK0sInstallCommand(networkInterface string, fullcmd string) error {
503509
}
504510

505511
func waitForNodeToJoin(ctx context.Context, kcli client.Client, hostname string, isWorker bool) error {
506-
loading := spinner.Start()
507-
defer loading.Close()
508-
loading.Infof("Waiting for node")
509512
if err := kubeutils.WaitForNode(ctx, kcli, hostname, isWorker); err != nil {
510513
return fmt.Errorf("unable to wait for node: %w", err)
511514
}
512-
loading.Infof("Node is ready")
513515
return nil
514516
}
515517

0 commit comments

Comments
 (0)