-
Notifications
You must be signed in to change notification settings - Fork 104
/
pod_container_log.go
189 lines (164 loc) · 4.2 KB
/
pod_container_log.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package logs
import (
"bufio"
"context"
"fmt"
"io"
"sync/atomic"
"time"
"github.com/cppforlife/go-cli-ui/ui"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
type PodContainerLog struct {
pod corev1.Pod
container string
podsClient typedcorev1.PodInterface
tag string
opts PodLogOpts
}
func NewPodContainerLog(
pod corev1.Pod,
container string,
podsClient typedcorev1.PodInterface,
tag string,
opts PodLogOpts,
) PodContainerLog {
return PodContainerLog{
pod: pod,
container: container,
podsClient: podsClient,
tag: tag,
opts: opts,
}
}
func (l PodContainerLog) Tail(ui ui.UI, cancelCh chan struct{}) error {
linePrefix := ""
if len(l.opts.LinePrefix) > 0 {
linePrefix = l.opts.LinePrefix + " | "
}
for {
err := l.StartTail(ui, cancelCh)
if err == io.EOF {
if l.opts.Follow {
ui.BeginLinef("%s# container stopped '%s' logs\n", linePrefix, l.tag)
// Making it 1sec instead of 500ms as some times for initContainers, it fetches the older stream if we go by 500ms.
time.Sleep(1 * time.Second)
continue
} else {
ui.BeginLinef("%s# ending tailing '%s' logs\n", linePrefix, l.tag)
return nil
}
}
if err != nil {
return err
}
}
}
func (l PodContainerLog) StartTail(ui ui.UI, cancelCh chan struct{}) error {
var streamCanceled atomic.Value
linePrefix := ""
if len(l.opts.LinePrefix) > 0 {
linePrefix = l.opts.LinePrefix + " | "
}
stream, err := l.obtainStream(ui, linePrefix, cancelCh)
if err != nil {
return err
}
if stream == nil {
return nil
}
defer stream.Close()
go func() {
<-cancelCh
streamCanceled.Store(true)
stream.Close()
}()
reader := bufio.NewReader(stream)
ui.BeginLinef("%s# starting tailing '%s' logs\n", linePrefix, l.tag)
for {
line, err := reader.ReadBytes('\n')
if err != nil {
typedCanceled, ok := streamCanceled.Load().(bool)
if ok && typedCanceled {
return nil // ignore error if stream was canceled
}
return err
}
if l.opts.ContainerTag {
ui.PrintBlock([]byte(fmt.Sprintf("%s%s | %s", linePrefix, l.tag, line)))
} else {
ui.PrintBlock([]byte(fmt.Sprintf("%s%s", linePrefix, line)))
}
}
}
func (l PodContainerLog) obtainStream(ui ui.UI, linePrefix string, cancelCh chan struct{}) (io.ReadCloser, error) {
var (
isWaitingMsgPrintedOnce bool
err error
)
for {
// TODO infinite retry
// It appears that GetLogs will successfully return log stream
// almost immediately after pod has been created; however,
// returned log stream will not carry any data, even after containers have started.
// Wait for pod object to have container status fields initialized
// since that appears to make GetLogs call return actual log stream.
if l.readyToGetLogs() {
logs := l.podsClient.GetLogs(l.pod.Name, &corev1.PodLogOptions{
Follow: l.opts.Follow,
TailLines: l.opts.Lines,
Container: l.container,
// TODO other options
})
stream, err := logs.Stream(context.TODO())
if err == nil {
return stream, nil
}
}
if !isWaitingMsgPrintedOnce {
ui.BeginLinef("%s# waiting for '%s' logs to become available...\n", linePrefix, l.tag)
isWaitingMsgPrintedOnce = true
if !l.opts.Follow {
if err == nil {
err = fmt.Errorf("Container %s not in Ready state", l.container)
}
return nil, err
}
}
select {
case <-cancelCh:
return nil, nil
default:
time.Sleep(500 * time.Millisecond)
}
}
}
func (l PodContainerLog) readyToGetLogs() bool {
pod, err := l.podsClient.Get(context.TODO(), l.pod.Name, metav1.GetOptions{})
if err != nil {
return false
}
containerStatuses := pod.Status.ContainerStatuses
initContainerStatuses := pod.Status.InitContainerStatuses
for _, containerStatus := range containerStatuses {
if l.container != containerStatus.Name {
continue
}
if containerStatus.State.Running != nil {
return true
}
}
for _, initContainerStatus := range initContainerStatuses {
if l.container != initContainerStatus.Name {
continue
}
if initContainerStatus.State.Running != nil {
return true
}
}
return false
}