From fe4e45658187131675d2e9e4a78f8e3710cadeaa Mon Sep 17 00:00:00 2001 From: Yolanda Robla Date: Mon, 15 Jul 2019 10:46:43 +0200 Subject: [PATCH] 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 --- pkg/utils/utils.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) 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) } -- 2.16.6