Skip to content

Commit

Permalink
spinner: Ensure there are no logs/prints within Start and Stop
Browse files Browse the repository at this point in the history
This change is necessary to ensure spinner and suffix are correctly
cleaned up when we call Stop. When we print something between Start and
Stop, the snipper and suggix remains there:

BEFORE THIS PR:

Example #1:

$ ./kubectl-aks config import
/ Importing...WARN[0030] Could not get VMSS VMs via Kubernetes API
WARN[0030] Please provide '--subscription', '--resource-group' and '--cluster-name' flags to get VMSS VMs via Azure API
Error: getting VMSS VMs via Kuberntes API: listing nodes: Get "https://172.28.128.4:6443/api/v1/nodes": dial tcp 172.28.128.4:6443: i/o timeout

Example #2:

$ ./kubectl-aks check-apiserver-connectivity
/ Checking connectivity...Connectivity check: failed with returned value 1

AFTER THIS PR:

Example #1:

$ ./kubectl-aks config import
WARN[0030] Could not get VMSS VMs via Kubernetes API
WARN[0030] Please provide '--subscription', '--resource-group' and '--cluster-name' flags to get VMSS VMs via Azure API
Error: getting VMSS VMs via Kuberntes API: listing nodes: Get "https://172.28.128.4:6443/api/v1/nodes": dial tcp 172.28.128.4:6443: i/o timeout

Example #2:

$ ./kubectl-aks check-apiserver-connectivity
Connectivity check: failed with returned value 1:

Signed-off-by: Jose Blanquicet <josebl@microsoft.com>
  • Loading branch information
blanquicet committed Feb 7, 2024
1 parent 95caebe commit 44df752
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
4 changes: 1 addition & 3 deletions cmd/check-apiserver-connectivity.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func init() {
}

func connCheckCmdRun(cmd *cobra.Command, args []string) error {
utils.DefaultSpinner.Start()
cred, err := utils.GetCredentials()
if err != nil {
return fmt.Errorf("failed to authenticate: %w", err)
Expand All @@ -54,15 +53,14 @@ func connCheckCmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("couldn't parse stdout of response message:\n%s", res.Stdout)
}
if ret != 0 {
fmt.Printf("\nConnectivity check: failed with returned value %d: %s\n",
fmt.Printf("Connectivity check: failed with returned value %d: %s\n",
ret, res.Stderr)

// Force the binary to return an exit code != 0 (forwarding command's
// return value). Useful if it is used in scripts.
os.Exit(ret)
}

utils.DefaultSpinner.Stop()
fmt.Println("Connectivity check: succeeded")

return nil
Expand Down
9 changes: 7 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,27 @@ func importCmdCommand() *cobra.Command {
var clusterName string

virtualMachineScaleSetVMs := func() (map[string]*utils.VirtualMachineScaleSetVM, error) {
utils.DefaultSpinner.Start()
utils.DefaultSpinner.Suffix = " Importing..."

if subscriptionID != "" && resourceGroup != "" && clusterName != "" {
vms, err := utils.VirtualMachineScaleSetVMsViaAzureAPI(subscriptionID, resourceGroup, clusterName)
utils.DefaultSpinner.Stop()
if err != nil {
return nil, fmt.Errorf("getting VMSS VMs via Azure API: %w", err)
}
return vms, nil
}

vms, err := utils.VirtualMachineScaleSetVMsViaKubeconfig()
utils.DefaultSpinner.Stop()
if err != nil {
logrus.Warn("Could not get VMSS VMs via Kubernetes API")
logrus.Warnf("Please provide '--%s', '--%s' and '--%s' flags to get VMSS VMs via Azure API",
utils.SubscriptionIDKey, utils.ResourceGroupKey, utils.ClusterNameKey)
return nil, fmt.Errorf("getting VMSS VMs via Kuberntes API: %w", err)
}

return vms, nil
}

Expand All @@ -152,8 +159,6 @@ func importCmdCommand() *cobra.Command {
SilenceUsage: true,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
utils.DefaultSpinner.Start()
defer utils.DefaultSpinner.Stop()
vms, err := virtualMachineScaleSetVMs()
if err != nil {
return err
Expand Down
2 changes: 0 additions & 2 deletions cmd/run-command.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func init() {
}

func runCommandCmdRun(cmd *cobra.Command, args []string) error {
utils.DefaultSpinner.Start()
cred, err := utils.GetCredentials()
if err != nil {
return fmt.Errorf("authenticating: %w", err)
Expand All @@ -75,7 +74,6 @@ func runCommandCmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("running command: %w", err)
}

utils.DefaultSpinner.Stop()
fmt.Fprintf(os.Stderr, "%s", res.Stderr)
fmt.Fprintf(os.Stdout, "%s", res.Stdout)
return nil
Expand Down
5 changes: 5 additions & 0 deletions cmd/utils/vmss.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,18 @@ func RunCommand(
os.Exit(1)
}()

DefaultSpinner.Start()
DefaultSpinner.Suffix = " Running..."

poller, err := client.BeginRunCommand(ctx, vm.NodeResourceGroup,
vm.VMScaleSet, vm.InstanceID, runCommand, nil)
if err != nil {
DefaultSpinner.Stop()
return nil, fmt.Errorf("begin running command: %w", err)
}

res, err := poller.PollUntilDone(ctx, &runtime.PollUntilDoneOptions{Frequency: pollingFreq})
DefaultSpinner.Stop()
if err != nil {
return nil, fmt.Errorf("polling command response: %w", err)
}
Expand Down

0 comments on commit 44df752

Please sign in to comment.