-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow-logger.go
293 lines (270 loc) · 8.96 KB
/
workflow-logger.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package logs
import (
"bufio"
"context"
"sort"
"strings"
"sync"
"time"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
workflowpkg "github.com/argoproj/argo/pkg/apiclient/workflow"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo/pkg/client/clientset/versioned"
"github.com/argoproj/argo/workflow/common"
)
// The goal of this class is to stream the logs of the workflow you want.
// * If you request "follow" and the workflow is not completed: logs will be tailed until the workflow is completed or context done.
// * Otherwise, it will print recent logs and exit.
type request interface {
GetNamespace() string
GetName() string
GetPodName() string
GetLogOptions() *corev1.PodLogOptions
}
type sender interface {
Send(entry *workflowpkg.LogEntry) error
}
func WorkflowLogs(ctx context.Context, wfClient versioned.Interface, kubeClient kubernetes.Interface, req request, sender sender) error {
wfInterface := wfClient.ArgoprojV1alpha1().Workflows(req.GetNamespace())
_, err := wfInterface.Get(req.GetName(), metav1.GetOptions{})
if err != nil {
return err
}
podInterface := kubeClient.CoreV1().Pods(req.GetNamespace())
logCtx := log.WithFields(log.Fields{"workflow": req.GetName(), "namespace": req.GetNamespace()})
// we create a watch on the pods labelled with the workflow name,
// but we also filter by pod name if that was requested
podListOptions := metav1.ListOptions{LabelSelector: common.LabelKeyWorkflow + "=" + req.GetName()}
if req.GetPodName() != "" {
podListOptions.FieldSelector = "metadata.name=" + req.GetPodName()
}
logCtx.WithField("options", podListOptions).Debug("List options")
// Keep a track of those we are logging, we also have a mutex to guard reads. Even if we stop streaming, we
// keep a marker here so we don't start again.
streamedPods := make(map[types.UID]bool)
var streamedPodsGuard sync.Mutex
var wg sync.WaitGroup
// A non-blocking channel for log entries to go down.
unsortedEntries := make(chan logEntry, 128)
logOptions := req.GetLogOptions()
if logOptions == nil {
logOptions = &corev1.PodLogOptions{}
}
logCtx.WithField("options", logOptions).Debug("Log options")
// make a copy of requested log options and set timestamps to true, so they can be parsed out later
podLogStreamOptions := *logOptions
podLogStreamOptions.Timestamps = true
// this func start a stream if one is not already running
ensureWeAreStreaming := func(pod *corev1.Pod) {
streamedPodsGuard.Lock()
defer streamedPodsGuard.Unlock()
logCtx := logCtx.WithField("podName", pod.GetName())
logCtx.WithFields(log.Fields{"podPhase": pod.Status.Phase, "alreadyStreaming": streamedPods[pod.UID]}).Debug("Ensuring pod logs stream")
if pod.Status.Phase != corev1.PodPending && !streamedPods[pod.UID] {
streamedPods[pod.UID] = true
wg.Add(1)
go func(podName string) {
defer wg.Done()
logCtx.Debug("Streaming pod logs")
defer logCtx.Debug("Pod logs stream done")
stream, err := podInterface.GetLogs(podName, &podLogStreamOptions).Stream()
if err != nil {
logCtx.Error(err)
return
}
scanner := bufio.NewScanner(stream)
for scanner.Scan() {
select {
case <-ctx.Done():
return
default:
line := scanner.Text()
parts := strings.SplitN(line, " ", 2)
content := parts[1]
timestamp, err := time.Parse(time.RFC3339, parts[0])
if err != nil {
logCtx.Errorf("unable to decode or infer timestamp from log line: %s", err)
// The current timestamp is the next best substitute. This won't be shown, but will be used
// for sorting
timestamp = time.Now()
content = line
}
// You might ask - why don't we let the client do this? Well, it is because
// this is the same as how this works for `kubectl logs`
if req.GetLogOptions().Timestamps {
content = line
}
logCtx.WithFields(log.Fields{"timestamp": timestamp, "content": content}).Debug("Log line")
unsortedEntries <- logEntry{podName: podName, content: content, timestamp: timestamp}
}
}
logCtx.Debug("No more log lines to stream")
// out of data, we do not want to start watching again
}(pod.GetName())
}
}
podWatch, err := podInterface.Watch(podListOptions)
if err != nil {
return err
}
defer podWatch.Stop()
// only list after we start the watch
logCtx.Debug("Listing workflow pods")
list, err := podInterface.List(podListOptions)
if err != nil {
return err
}
// start watches by start-time
sort.Slice(list.Items, func(i, j int) bool {
return list.Items[i].Status.StartTime.Before(list.Items[j].Status.StartTime)
})
for _, pod := range list.Items {
ensureWeAreStreaming(&pod)
}
if req.GetLogOptions().Follow {
wfListOptions := metav1.ListOptions{FieldSelector: "metadata.name=" + req.GetName()}
wfWatch, err := wfInterface.Watch(wfListOptions)
if err != nil {
return err
}
defer wfWatch.Stop()
// We never send anything on this channel apart from closing it to indicate we should stop waiting for new pods.
stopWatchingPods := make(chan struct{})
// The purpose of this watch is to make sure we do not exit until the workflow is completed or deleted.
// When that happens, it signals we are done by closing the stop channel.
wg.Add(1)
go func() {
defer close(stopWatchingPods)
defer wg.Done()
defer logCtx.Debug("Done watching workflow events")
logCtx.Debug("Watching for workflow events")
for {
select {
case <-ctx.Done():
return
case event, open := <-wfWatch.ResultChan():
if !open {
logCtx.Debug("Re-establishing workflow watch")
wfWatch.Stop()
wfWatch, err = wfInterface.Watch(wfListOptions)
if err != nil {
logCtx.Error(err)
return
}
continue
}
wf, ok := event.Object.(*wfv1.Workflow)
if !ok {
// object is probably probably metav1.Status
logCtx.WithError(apierr.FromObject(event.Object)).Warn("watch object was not a workflow")
return
}
logCtx.WithFields(log.Fields{"eventType": event.Type, "completed": wf.Status.Fulfilled()}).Debug("Workflow event")
if event.Type == watch.Deleted || wf.Status.Fulfilled() {
return
}
// in case we re-establish the watch, make sure we start at the same place
wfListOptions.ResourceVersion = wf.ResourceVersion
}
}
}()
// The purpose of this watch is to start streaming any new pods that appear when we are running.
wg.Add(1)
go func() {
defer wg.Done()
defer logCtx.Debug("Done watching pod events")
logCtx.Debug("Watching for pod events")
for {
select {
case <-stopWatchingPods:
return
case event, open := <-podWatch.ResultChan():
if !open {
logCtx.Info("Re-establishing pod watch")
podWatch.Stop()
podWatch, err = podInterface.Watch(podListOptions)
if err != nil {
logCtx.Error(err)
return
}
continue
}
pod, ok := event.Object.(*corev1.Pod)
if !ok {
// object is probably probably metav1.Status
logCtx.WithError(apierr.FromObject(event.Object)).Warn("watch object was not a pod")
return
}
logCtx.WithFields(log.Fields{"eventType": event.Type, "podName": pod.GetName(), "phase": pod.Status.Phase}).Debug("Pod event")
if pod.Status.Phase == corev1.PodRunning {
ensureWeAreStreaming(pod)
}
podListOptions.ResourceVersion = pod.ResourceVersion
}
}
}()
} else {
logCtx.Debug("Not starting watches")
}
doneSorting := make(chan struct{})
go func() {
defer close(doneSorting)
defer logCtx.Debug("Done sorting entries")
logCtx.Debug("Sorting entries")
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
entries := logEntries{}
// Ugly to have this func, but we use it in two places (normal operation and finishing up).
send := func() error {
sort.Sort(entries)
for len(entries) > 0 {
// head
var e logEntry
e, entries = entries[0], entries[1:]
logCtx.WithFields(log.Fields{"timestamp": e.timestamp, "content": e.content}).Debug("Sending entry")
err := sender.Send(&workflowpkg.LogEntry{Content: e.content, PodName: e.podName})
if err != nil {
return err
}
}
return nil
}
// This defer make sure we flush any remaining entries on exit.
defer func() {
err := send()
if err != nil {
logCtx.Error(err)
}
}()
for {
select {
case entry, ok := <-unsortedEntries:
if !ok {
// The fact this channel is closed indicates that we need to finish-up.
return
} else {
entries = append(entries, entry)
}
case <-ticker.C:
err := send()
if err != nil {
logCtx.Error(err)
return
}
}
}
}()
logCtx.Debug("Waiting for work-group")
wg.Wait()
logCtx.Debug("Work-group done")
close(unsortedEntries)
<-doneSorting
logCtx.Debug("Done-done")
return nil
}