Skip to content

Commit 3f040eb

Browse files
mads-hartmannroboquat
authored andcommitted
Add retry option to install-context
1 parent 2ce9d02 commit 3f040eb

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

dev/preview/previewctl/BUILD.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ scripts:
1717
script: |
1818
set -euo pipefail
1919
20-
source="$(which previewctl)"
2120
destination="/workspace/bin/previewctl"
21+
# Leeway puts the dependencies at the end of the PATH which means that /workspace/bin takes precedence
22+
# on the path. So for `which previewctl` to return the previewctl Leeway just build (/tmp/build/.../previewctl)
23+
# we have to delete /workspace/bin/previewctl first.
24+
rm -f "$destination"
25+
source="$(which previewctl)"
2226
2327
mkdir -p /workspace/bin
2428

dev/preview/previewctl/cmd/install_context.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type installContextCmdOpts struct {
2525

2626
watch bool
2727
timeout time.Duration
28+
retry int
2829
kubeConfigSavePath string
2930
sshPrivateKeyPath string
3031

@@ -106,7 +107,7 @@ func newInstallContextCmd(logger *logrus.Logger) *cobra.Command {
106107
if opts.watch {
107108
for range time.Tick(15 * time.Second) {
108109
// We're using a short timeout here to handle the scenario where someone switches
109-
// to a branch that doens't have a preview envrionment. In that case the default
110+
// to a branch that doesn't have a preview environment. In that case the default
110111
// timeout would mean that we would block for 10 minutes, potentially missing
111112
// if the user changes to a new branch that does that a preview.
112113
err := install(30 * time.Second)
@@ -115,15 +116,28 @@ func newInstallContextCmd(logger *logrus.Logger) *cobra.Command {
115116
}
116117
}
117118
} else {
118-
return install(opts.timeout)
119+
sleep := 10 * time.Second
120+
remainingRetries := opts.retry
121+
for remainingRetries > 0 {
122+
remainingRetries -= 1
123+
err := install(opts.timeout)
124+
if err == nil {
125+
logger.Info("Successfully installed kubectx")
126+
return nil
127+
}
128+
logger.WithFields(logrus.Fields{"err": err}).Infof("Failed to install context. Waiting %s before trying again. %d attempts left", sleep, remainingRetries)
129+
time.Sleep(sleep)
130+
}
131+
return errors.New("Failed to install kubectx")
119132
}
120133

121134
return nil
122135
},
123136
}
124137

125-
cmd.Flags().BoolVar(&opts.watch, "watch", false, "If watch is enabled, previewctl will keep trying to install the kube-context every 15 seconds.")
138+
cmd.Flags().BoolVar(&opts.watch, "watch", false, "If watch is enabled, previewctl will keep trying to install the kube-context every 15 seconds, even when successful.")
126139
cmd.Flags().DurationVarP(&opts.timeout, "timeout", "t", 10*time.Minute, "Timeout before considering the installation failed")
140+
cmd.Flags().IntVar(&opts.retry, "retry", 1, "If retry is enabled previewctl will retry the specified number of times. This option is ignored if watch is used.")
127141
cmd.PersistentFlags().StringVar(&opts.sshPrivateKeyPath, "private-key-path", fmt.Sprintf("%s/.ssh/vm_id_rsa", homedir.HomeDir()), "path to the private key used to authenticate with the VM")
128142
cmd.PersistentFlags().StringVar(&opts.getCredentialsOpts.serviceAccountPath, "gcp-service-account", "", "path to the GCP service account to use")
129143

dev/preview/workflow/preview/preview.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ ensure_gcloud_auth
2929

3030
leeway run dev/preview:create-preview
3131
leeway run dev/preview:build
32-
previewctl install-context
32+
previewctl install-context --retry 30
3333
leeway run dev/preview:deploy-gitpod

0 commit comments

Comments
 (0)