From 153d8234a95cd43e10ff5116cc5db63926047792 Mon Sep 17 00:00:00 2001 From: sheetalsingala Date: Sun, 26 Jul 2020 10:27:13 -0400 Subject: [PATCH] Parameterize the number of retries Signed-off-by: sheetalsingala Change-Id: I4eccf49258b60a76b45d38712a5d2b6934215f5c --- cmd/apply_workloads.go | 26 +++++++++++++++++++++++++- pkg/site/site.go | 6 +++--- pkg/utils/utils.go | 6 +++--- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/cmd/apply_workloads.go b/cmd/apply_workloads.go index 6ed93a7..f392134 100644 --- a/cmd/apply_workloads.go +++ b/cmd/apply_workloads.go @@ -18,11 +18,17 @@ import ( "fmt" "log" "os" + "strconv" "gerrit.akraino.org/kni/installer/pkg/site" "github.com/spf13/cobra" ) +var ( + RetryCount int + Delay int +) + // applyWorkloadsCmd represents the apply_workloads command var applyWorkloadsCmd = &cobra.Command{ Use: "apply_workloads siteName [--build_path=]", @@ -53,9 +59,25 @@ var applyWorkloadsCmd = &cobra.Command{ kubeconfig = "" } + retryCount, _ := cmd.Flags().GetString("retry_count") + if len(retryCount) == 0 { + //set to default value + RetryCount = 5 + } else { + RetryCount, _ = strconv.Atoi(retryCount) + } + + delay, _ := cmd.Flags().GetString("delay") + if len(delay) == 0 { + //set to default value + Delay = 30 + } else { + Delay, _ = strconv.Atoi(delay) + } + // define a site object and proceed with applying workloads s := site.NewWithName(siteName, buildPath) - s.ApplyWorkloads(kubeconfig) + s.ApplyWorkloads(kubeconfig, RetryCount, Delay) }, } @@ -64,5 +86,7 @@ func init() { applyWorkloadsCmd.Flags().StringP("build_path", "", "", "Directory to use as build path. If that not exists, the installer will generate a default directory") applyWorkloadsCmd.Flags().StringP("kubeconfig", "", "", "Path to kubeconfig file. By default it will be the one generated with prepare_manifests. If set to 'local', no kubeconfig will be used and it will assume running on local cluster") + applyWorkloadsCmd.Flags().StringP("retry_count", "", "", "Number of retries") + applyWorkloadsCmd.Flags().StringP("delay", "", "", "Delay between each retry") } diff --git a/pkg/site/site.go b/pkg/site/site.go index ff0c61f..3a55762 100644 --- a/pkg/site/site.go +++ b/pkg/site/site.go @@ -449,7 +449,7 @@ func (s Site) PrepareManifests() { } // using the site contents, applies the workloads on it -func (s Site) ApplyWorkloads(kubeconfigFile string) { +func (s Site) ApplyWorkloads(kubeconfigFile string, retryCount int, delay int) { siteBuildPath := fmt.Sprintf("%s/%s", s.buildPath, s.siteName) // if we have kubeconfig, validate that exists @@ -469,7 +469,7 @@ func (s Site) ApplyWorkloads(kubeconfigFile string) { out := utils.ApplyKustomize(fmt.Sprintf("%s/kustomize", binariesPath), fmt.Sprintf("%s/blueprint/sites/site/02_cluster-addons", siteBuildPath)) if string(out) != "" { // now we can apply it - utils.ApplyOc(fmt.Sprintf("%s/oc", binariesPath), out, kubeconfigFile) + utils.ApplyOc(fmt.Sprintf("%s/oc", binariesPath), out, kubeconfigFile, retryCount, delay) } else { log.Println(fmt.Sprintf("No manifests found for %s/blueprint/sites/site/02_cluster-addons", siteBuildPath)) } @@ -477,7 +477,7 @@ func (s Site) ApplyWorkloads(kubeconfigFile string) { out = utils.ApplyKustomize(fmt.Sprintf("%s/kustomize", binariesPath), fmt.Sprintf("%s/blueprint/sites/site/03_services", siteBuildPath)) if string(out) != "" { // now we can apply it - utils.ApplyOc(fmt.Sprintf("%s/oc", binariesPath), out, kubeconfigFile) + utils.ApplyOc(fmt.Sprintf("%s/oc", binariesPath), out, kubeconfigFile, retryCount, delay) } else { log.Println(fmt.Sprintf("No manifests found for %s/blueprint/sites/site/03_services", siteBuildPath)) } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index b0b71d0..9e204df 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -54,7 +54,7 @@ func ApplyKustomize(kustomizeBinary string, kustomizePath string) []byte { } // utility to apply OC for a given output -func ApplyOc(ocBinary string, kubectlContent []byte, kubeconfigPath string) { +func ApplyOc(ocBinary string, kubectlContent []byte, kubeconfigPath string, retryCount int, delay int) { // write content to be applied to temporary file tmpFile, err := ioutil.TempFile(os.TempDir(), "kubectl-") if err != nil { @@ -73,7 +73,7 @@ func ApplyOc(ocBinary string, kubectlContent []byte, kubeconfigPath string) { if len(kubeconfigPath) > 0 { envVars = []string{fmt.Sprintf("KUBECONFIG=%s", kubeconfigPath)} } - for i := 1; i <= 5; i++ { + for i := 1; i <= retryCount; i++ { _, err := ExecuteCommand("", envVars, false, true, ocBinary, "apply", "-f", tmpFile.Name()) if err == nil { @@ -82,7 +82,7 @@ func ApplyOc(ocBinary string, kubectlContent []byte, kubeconfigPath string) { } else { log.Println(string(err)) // sleep and retry - time.Sleep(30 * time.Second) + time.Sleep(time.Duration(delay) * time.Second) } } } -- 2.16.6