Send output of kustomize to tempfile 87/1187/2
authorYolanda Robla <yroblamo@redhat.com>
Mon, 15 Jul 2019 08:46:43 +0000 (10:46 +0200)
committerYolanda Robla <yroblamo@redhat.com>
Mon, 15 Jul 2019 09:03:38 +0000 (11:03 +0200)
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 <yroblamo@redhat.com>
Change-Id: Ic2d0513acadd2554f8739d31ad7fec0117dd3339

pkg/utils/utils.go

index 4fa2805..8a30228 100644 (file)
@@ -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)
                }