/
logs.go
55 lines (50 loc) · 1.18 KB
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package kube
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
)
// TailLogs will tail the logs for the pod in ns with containerName,
// returning when the logs are complete. It writes to errOut and out.
func TailLogs(ns string, pod string, containerName string, errOut io.Writer, out io.Writer) error {
args := []string{"logs", "-n", ns, "-f"}
if containerName != "" {
args = append(args, "-c", containerName)
}
args = append(args, pod)
name := "kubectl"
e := exec.Command(name, args...)
e.Stderr = errOut
stdout, _ := e.StdoutPipe()
err := os.Setenv("PATH", util.PathWithBinary())
if err != nil {
return errors.Wrap(err, "failed to set PATH env variable")
}
err = e.Start()
if err != nil {
log.Logger().Errorf("Error: Command failed %s %s", name, strings.Join(args, " "))
}
scanner := bufio.NewScanner(stdout)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
m := scanner.Text()
_, err = fmt.Fprintln(out, m)
if err != nil {
return err
}
if m == "Finished: FAILURE" {
os.Exit(1)
}
}
err = e.Wait()
if err != nil {
return err
}
return nil
}