Parameterize the number of retries 46/3646/2
authorsheetalsingala <ssingala@redhat.com>
Sun, 26 Jul 2020 14:27:13 +0000 (10:27 -0400)
committerSheetal Chetan Singala <ssingala@redhat.com>
Sun, 26 Jul 2020 14:29:46 +0000 (14:29 +0000)
Signed-off-by: sheetalsingala <ssingala@redhat.com>
Change-Id: I4eccf49258b60a76b45d38712a5d2b6934215f5c

cmd/apply_workloads.go
pkg/site/site.go
pkg/utils/utils.go

index 6ed93a7..f392134 100644 (file)
@@ -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=<local_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")
 
 }
index ff0c61f..3a55762 100644 (file)
@@ -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))
        }
index b0b71d0..9e204df 100644 (file)
@@ -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)
                }
        }
 }