From: Yolanda Robla Date: Mon, 15 Jul 2019 08:46:43 +0000 (+0200) Subject: Send output of kustomize to tempfile X-Git-Tag: akraino_r2~50 X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F87%2F1187%2F2;p=kni%2Finstaller.git Send output of kustomize to tempfile There is an issue with pipes when running on a ci system like jenkins. So instead of relying on stdin pipe, send the output to a tmp file and apply from there. Signed-off-by: Yolanda Robla Change-Id: Ic2d0513acadd2554f8739d31ad7fec0117dd3339 --- diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 4fa2805..8a30228 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -2,6 +2,7 @@ package utils import ( "fmt" + "io/ioutil" "log" "os" "os/exec" @@ -64,17 +65,26 @@ func ApplyKustomize(kustomizeBinary string, kustomizePath string) []byte { // utility to apply kubectl for a given output func ApplyKubectl(kubectlBinary string, kubectlContent []byte, kubeconfigPath string) { var out []byte - for i := 1; i <= 10; i++ { - cmd := exec.Command(kubectlBinary, "apply", "-f", "-") + // write content to be applied to temporary file + tmpFile, err := ioutil.TempFile(os.TempDir(), "kubectl-") + if err != nil { + log.Fatal(fmt.Sprintf("Cannot create temporary file: %s", err)) + os.Exit(1) + } + defer os.Remove(tmpFile.Name()) + + _, err = tmpFile.Write(kubectlContent) + if err != nil { + log.Fatal(fmt.Sprintf("Error writing kubectl file: %s", err)) + os.Exit(1) + } + + for i := 1; i <= 10; i++ { + cmd := exec.Command(kubectlBinary, "apply", "-f", tmpFile.Name()) 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 @@ -84,6 +94,7 @@ func ApplyKubectl(kubectlBinary string, kubectlContent []byte, kubeconfigPath st // it is ok, stop the loop break } else { + log.Println(err) // sleep and retry time.Sleep(60 * time.Second) }