"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>]",
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)
},
}
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")
}
}
// 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
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))
}
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))
}
}
// 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 {
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 {
} else {
log.Println(string(err))
// sleep and retry
- time.Sleep(30 * time.Second)
+ time.Sleep(time.Duration(delay) * time.Second)
}
}
}