From: Yolanda Robla Date: Tue, 9 Jul 2019 15:59:25 +0000 (+0200) Subject: Add new command to run workloads X-Git-Tag: akraino_r2~58^2 X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=commitdiff_plain;h=8f62e7d70ef685305c10f2d226237872050182dc;p=kni%2Finstaller.git Add new command to run workloads This command will apply kustomize on the extra workloads directories, and appy it with kubectl Signed-off-by: Yolanda Robla Change-Id: I41639c99602cae2962533db807be951f293d2451 --- diff --git a/cmd/apply_workloads.go b/cmd/apply_workloads.go new file mode 100644 index 0000000..608060d --- /dev/null +++ b/cmd/apply_workloads.go @@ -0,0 +1,59 @@ +// Copyright © 2019 Red Hat +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "fmt" + "log" + "os" + + "gerrit.akraino.org/kni/installer/pkg/site" + "github.com/spf13/cobra" +) + +// applyWorkloadsCmd represents the apply_workloads command +var applyWorkloadsCmd = &cobra.Command{ + Use: "apply_workloads siteName [--build_path=]", + Short: "Command to apply the workloads after deploying a site", + Long: ``, + TraverseChildren: true, + Run: func(cmd *cobra.Command, args []string) { + // retrieve config values and start fetching + var siteName string + if len(args) == 0 { + log.Fatal("Please specify site name as first argument") + os.Exit(1) + } else { + siteName = args[0] + } + + buildPath, _ := cmd.Flags().GetString("build_path") + if len(buildPath) == 0 { + // will generate a temporary directory + buildPath = fmt.Sprintf("%s/.kni", os.Getenv("HOME")) + } + + // define a site object and proceed with applying workloads + s := site.NewWithName(siteName, buildPath) + s.ApplyWorkloads() + }, +} + +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") + +} diff --git a/pkg/site/site.go b/pkg/site/site.go index 66ac819..02426d3 100644 --- a/pkg/site/site.go +++ b/pkg/site/site.go @@ -281,3 +281,38 @@ func (s Site) PrepareManifests() { } } + +// using the site contents, applies the workloads on it +func (s Site) ApplyWorkloads() { + 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("Applying workloads from %s", fmt.Sprintf("%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("Applying workloads from %s", fmt.Sprintf("%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)) + } + + } else { + // no kubeconfig, abort the workload creation + log.Fatal("Error, no kubeconfig file found") + os.Exit(1) + } + +} diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index d1d19fc..f08e122 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "path/filepath" + "time" ) // utility to validate pre-requisites for deploying @@ -59,3 +60,33 @@ func ApplyKustomize(kustomizeBinary string, kustomizePath string) []byte { return out } + +// utility to apply kubectl for a given output +func ApplyKubectl(kubectlBinary string, kubectlContent []byte, kubeconfigPath string) { + var out []byte + var err string + for i := 1; i <= 10; i++ { + cmd := exec.Command(kubectlBinary, "apply", "-f", "-") + + cmd.Env = os.Environ() + cmd.Env = append(cmd.Env, fmt.Sprintf("KUBECONFIG_PATH=%s", kubeconfigPath)) + + // add output to stdin + stdin, err := cmd.StdinPipe() + stdin.Write(kubectlContent) + stdin.Close() + + out, err = cmd.Output() + + // show output for user to see progress + log.Println(string(out)) + + if err == nil { + // it is ok, stop the loop + break + } else { + // sleep and retry + time.Sleep(60 * time.Second) + } + } +}