From: Yolanda Robla Date: Wed, 28 Aug 2019 13:34:40 +0000 (+0200) Subject: Allow to specify the kubeconfig file X-Git-Tag: akraino_r2~29 X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=commitdiff_plain;h=556d947364921522007000c9aabd1316fc4cdf8e;p=kni%2Finstaller.git Allow to specify the kubeconfig file In order to apply from any place, we need to parameterize the kubeconfig path. Accept a kubeconfig parameter, that defaults to the one from prepare_manifests is not set, or add the ability to specify "local" to deploy to a local cluster without kubeconfig file. Signed-off-by: Yolanda Robla Change-Id: I9fffeeae28369f0fbe4dd2de127a61e99e35e62e --- diff --git a/cmd/apply_workloads.go b/cmd/apply_workloads.go index 608060d..ed27971 100644 --- a/cmd/apply_workloads.go +++ b/cmd/apply_workloads.go @@ -45,9 +45,17 @@ var applyWorkloadsCmd = &cobra.Command{ buildPath = fmt.Sprintf("%s/.kni", os.Getenv("HOME")) } + kubeconfig, _ := cmd.Flags().GetString("kubeconfig") + if len(kubeconfig) == 0 { + // set to default value + kubeconfig = fmt.Sprintf("%s/final_manifests/auth/kubeconfig", buildPath) + } else if kubeconfig == "local" { + kubeconfig = "" + } + // define a site object and proceed with applying workloads s := site.NewWithName(siteName, buildPath) - s.ApplyWorkloads() + s.ApplyWorkloads(kubeconfig) }, } @@ -55,5 +63,6 @@ func init() { rootCmd.AddCommand(applyWorkloadsCmd) 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") } diff --git a/pkg/site/site.go b/pkg/site/site.go index b8e8a1b..0cb95b3 100644 --- a/pkg/site/site.go +++ b/pkg/site/site.go @@ -389,36 +389,33 @@ func (s Site) PrepareManifests() { } // using the site contents, applies the workloads on it -func (s Site) ApplyWorkloads() { +func (s Site) ApplyWorkloads(kubeconfigFile string) { siteBuildPath := fmt.Sprintf("%s/%s", s.buildPath, s.siteName) - // check if we have the needed kubeconfig file - kubeconfigFile := fmt.Sprintf("%s/final_manifests/auth/kubeconfig", siteBuildPath) - binariesPath := fmt.Sprintf("%s/requirements", siteBuildPath) - - if _, err := os.Stat(kubeconfigFile); err == nil { - log.Println(fmt.Sprintf("Applying workloads from %s/blueprint/sites/site/02_cluster-addons", siteBuildPath)) - 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.ApplyKubectl(fmt.Sprintf("%s/kubectl", binariesPath), out, kubeconfigFile) - } else { - log.Println(fmt.Sprintf("No manifests found for %s/blueprint/sites/site/02_cluster-addons", siteBuildPath)) - } - - log.Println(fmt.Sprintf("Applying workloads from %s/blueprint/sites/site/03_services", 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.ApplyKubectl(fmt.Sprintf("%s/kubectl", binariesPath), out, kubeconfigFile) - } else { - log.Println(fmt.Sprintf("No manifests found for %s/blueprint/sites/site/03_services", siteBuildPath)) + // if we have kubeconfig, validate that exists + if len(kubeconfigFile) > 0 { + if _, err := os.Stat(kubeconfigFile); err != nil { + log.Fatal(fmt.Sprintf("Error: kubeconfig file %s does not exist", kubeconfigFile)) + os.Exit(1) } + } + binariesPath := fmt.Sprintf("%s/requirements", siteBuildPath) + log.Println(fmt.Sprintf("Applying workloads from %s/blueprint/sites/site/02_cluster-addons", siteBuildPath)) + 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.ApplyKubectl(fmt.Sprintf("%s/kubectl", binariesPath), out, kubeconfigFile) } else { - // no kubeconfig, abort the workload creation - log.Fatal("Error, no kubeconfig file found") - os.Exit(1) + log.Println(fmt.Sprintf("No manifests found for %s/blueprint/sites/site/02_cluster-addons", siteBuildPath)) } + log.Println(fmt.Sprintf("Applying workloads from %s/blueprint/sites/site/03_services", 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.ApplyKubectl(fmt.Sprintf("%s/kubectl", binariesPath), out, kubeconfigFile) + } 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 6f2ae59..d1ca628 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -68,7 +68,10 @@ func ApplyKubectl(kubectlBinary string, kubectlContent []byte, kubeconfigPath st os.Exit(1) } - envVars := []string{fmt.Sprintf("KUBECONFIG=%s", kubeconfigPath)} + var envVars []string + if len(kubeconfigPath) > 0 { + envVars = []string{fmt.Sprintf("KUBECONFIG=%s", kubeconfigPath)} + } for i := 1; i <= 5; i++ { _, err := ExecuteCommand("", envVars, false, true, kubectlBinary, "apply", "-f", tmpFile.Name())