-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpod_tracker.go
More file actions
200 lines (163 loc) · 4.66 KB
/
pod_tracker.go
File metadata and controls
200 lines (163 loc) · 4.66 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
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
package main
import (
"encoding/json"
"net/http"
"sync"
director "github.com/relistan/go-director"
log "github.com/sirupsen/logrus"
)
var startup sync.Once
type NewTailerFunc func(pod *Pod) LogTailer
// A PodTracker keeps track of all the Tailers and orchestrates them based on
// what it finds out from discovery, on time loop controlled by the looper.
type PodTracker struct {
LogTails map[string]LogTailer
Filter DiscoveryFilter
disco Discoverer
looper director.Looper
newTailerFunc NewTailerFunc
tailsLock sync.RWMutex
}
// NewPodTracker configures a PodTracker for use, assigning the given Looper
// and Discoverer, and making sure the caching map is made.
func NewPodTracker(looper director.Looper, disco Discoverer,
newTailerFunc NewTailerFunc, filter DiscoveryFilter) *PodTracker {
return &PodTracker{
LogTails: make(map[string]LogTailer, 5),
looper: looper,
disco: disco,
newTailerFunc: newTailerFunc,
Filter: filter,
}
}
// Run invokes the looper to poll discovery and then add or remove Pods from
// tracking. The work of the actual file tailing is done by the Tailers.
func (t *PodTracker) Run() {
t.looper.Loop(func() error {
discovered, err := t.disco.Discover()
if err != nil {
log.Error(err.Error())
return err
}
newTails := make(map[string]LogTailer, len(t.LogTails))
for _, pod := range discovered {
// Handle existing/known pods
var wasKnown bool
t.withLock(func() {
tailer, ok := t.LogTails[pod.Name]
if ok {
wasKnown = true // Can't continue from in here
// Copy it over because we still see this pod
newTails[pod.Name] = tailer
// Find all the new files for the pod
logFiles, err := t.disco.LogFiles(pod.Name)
if err != nil {
log.Warnf("Failed to get logs for pod %s: %s", pod.Name, err)
return
}
// Update the followed files
err = tailer.TailLogs(logFiles)
if err != nil {
log.Errorf("Failed to tail logs for %s: %s", pod.Name, err)
return
}
// State for debugging
pod.Logs = logFiles
}
})
if wasKnown {
continue
}
// Handle newly discovered pods
log.Infof("new pod --> %s:%s [%s]", pod.Namespace, pod.ServiceName, pod.Name)
shouldTail, err := t.Filter.ShouldTailLogs(pod)
if err != nil {
log.Errorf(
"Failed to check filter for pod %s, disabling logging: %s", pod.Name, err,
)
continue
}
var tailer LogTailer
if shouldTail {
// Find the files and actually tail them
logFiles, err := t.disco.LogFiles(pod.Name)
if err != nil {
log.Warnf("Failed to get logs for pod %s: %s", pod.Name, err)
continue
}
pod.Logs = logFiles
tailer = t.newTailerFunc(pod)
err = tailer.TailLogs(logFiles)
if err != nil {
log.Warnf("Failed to tail logs for pod %s: %s", pod.Name, err)
continue
}
} else {
// We want to keep state on these, so we just use a mock instead
log.Infof("Skipping pod %s because filter says to", pod.Name)
tailer = &MockTailer{PodTailed: pod}
}
if _, ok := tailer.(*MockTailer); !ok {
log.Infof("Adding and running new tailer for pod %s", pod.Name)
}
newTails[pod.Name] = tailer
// Will exit when the looper is stopped, when Stop() is called on the Tailer
tailer.Run()
}
// Swap the new list with the old list
var oldTails map[string]LogTailer
t.withLock(func() {
oldTails = t.LogTails
t.LogTails = newTails
})
// Iterate over the old list to remove pods no longer present
t.withReadLock(func() {
for podName, tailer := range oldTails {
if _, ok := t.LogTails[podName]; !ok {
// Do some pod dropping
log.Infof("drop pod: %s", podName)
tailer.Stop()
}
}
})
return nil
})
}
func (t *PodTracker) FlushOffsets() {
t.withReadLock(func() {
for _, tailer := range t.LogTails {
tailer.FlushOffsets()
}
})
}
func (t *PodTracker) withReadLock(fn func()) {
t.tailsLock.RLock()
fn()
t.tailsLock.RUnlock()
}
func (t *PodTracker) withLock(fn func()) {
t.tailsLock.Lock()
fn()
t.tailsLock.Unlock()
}
func (t *PodTracker) ServeHTTP() {
go func() {
// Set up the route and handler.
http.HandleFunc("/state", func(w http.ResponseWriter, r *http.Request) {
t.tailsLock.RLock()
defer t.tailsLock.RUnlock()
// Set the Content-Type header.
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(t.LogTails)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
// Start the server.
log.Println("State server starting on :8080...")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Error(err.Error())
}
}()
}