-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_log.go
More file actions
107 lines (99 loc) · 2.48 KB
/
cmd_log.go
File metadata and controls
107 lines (99 loc) · 2.48 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"fmt"
"os"
"os/exec"
"time"
"k8s.io/api/core/v1"
apiv1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func cmdLog(args []string, config *appConfig) {
if len(args) < 1 {
fmt.Fprintf(os.Stderr, "USAGE: %s log pod-name\n", os.Args[0])
os.Exit(1)
}
podName := args[0]
namespace := "default"
if config.namespace != "" {
namespace = config.namespace
}
clientset, err := loadKubernetesClient(config)
if err != nil {
ErrPrintln(ColorRed, err)
os.Exit(1)
}
tail := "-1"
for {
tailPodLog(clientset, podName, namespace, config.context, tail)
// Check pod exit Status
waitExit := 0
wait_exit:
for {
pod, err := clientset.Core().Pods(namespace).Get(podName, apiv1.GetOptions{})
if err != nil {
ErrPrintln(ColorRed, err)
os.Exit(1)
}
switch pod.Status.Phase {
case v1.PodSucceeded:
os.Exit(0)
case v1.PodFailed:
ErrPrintln(ColorRed, "Pod failed: ", pod.Status.ContainerStatuses[0].State.Terminated.Reason)
os.Exit(1)
case v1.PodRunning, v1.PodPending:
waitExit++
if waitExit <= 5 {
time.Sleep(time.Second)
} else {
containerStatus := pod.Status.ContainerStatuses[0]
fmt.Println(containerStatus)
if containerStatus.State.Terminated != nil {
os.Exit(int(containerStatus.State.Terminated.ExitCode))
}
ErrPrintln(ColorRed, "Log stream quited while pod still running")
ErrPrintln(ColorRed, "Restart log stream, some log lines may be lost")
waitExit = 0
tail = "1"
break wait_exit
}
default:
ErrPrintln(ColorRed, "Unknown pod phase")
os.Exit(1)
}
}
}
}
func tailPodLog(clientset *kubernetes.Clientset, podName, namespace, context, tail string) {
// Wait for pod running
wait_running:
for {
pod, err := clientset.Core().Pods(namespace).Get(podName, apiv1.GetOptions{})
if err != nil {
ErrPrintln(ColorRed, err)
os.Exit(1)
}
switch pod.Status.Phase {
case v1.PodUnknown:
ErrPrintln(ColorRed, "Unknown pod phase")
os.Exit(1)
case v1.PodPending:
time.Sleep(2 * time.Second)
default:
break wait_running
}
}
var cmd *exec.Cmd
if context == "" {
cmd = exec.Command("kubectl", "logs", "-f", "--tail", tail, podName, "--namespace", namespace)
} else {
cmd = exec.Command("kubectl", "logs", "-f", "--tail", tail, podName, "--namespace", namespace, "--context", context)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
ErrPrintln(ColorRed, err)
os.Exit(1)
}
}