forked from weaveworks/scope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.go
341 lines (301 loc) · 9.72 KB
/
filters.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package render
import (
"context"
"strings"
"github.com/weaveworks/common/mtime"
"github.com/weaveworks/scope/report"
)
const (
k8sNamespaceLabel = "io.kubernetes.pod.namespace"
swarmNamespaceLabel = "com.docker.stack.namespace"
)
// CustomRenderer allow for mapping functions that received the entire topology
// in one call - useful for functions that need to consider the entire graph.
// We should minimise the use of this renderer type, as it is very inflexible.
type CustomRenderer struct {
RenderFunc func(Nodes) Nodes
Renderer
}
// Render implements Renderer
func (c CustomRenderer) Render(ctx context.Context, rpt report.Report) Nodes {
return c.RenderFunc(c.Renderer.Render(ctx, rpt))
}
// FilterFunc is the function type used by Filters
type FilterFunc func(report.Node) bool
// AnyFilterFunc checks if any of the filterfuncs matches.
func AnyFilterFunc(fs ...FilterFunc) FilterFunc {
return func(n report.Node) bool {
for _, f := range fs {
if f(n) {
return true
}
}
return false
}
}
// ComposeFilterFuncs composes filterfuncs into a single FilterFunc checking all.
func ComposeFilterFuncs(fs ...FilterFunc) FilterFunc {
return func(n report.Node) bool {
for _, f := range fs {
if !f(n) {
return false
}
}
return true
}
}
// Complement takes a FilterFunc f and returns a FilterFunc that has the same
// effects, if any, and returns the opposite truth value.
func Complement(f FilterFunc) FilterFunc {
return func(node report.Node) bool { return !f(node) }
}
// Transform applies the filter to all nodes
func (f FilterFunc) Transform(nodes Nodes) Nodes {
output := report.Nodes{}
filtered := nodes.Filtered
for id, node := range nodes.Nodes {
if f(node) {
output[id] = node
} else {
filtered++
}
}
// Deleted nodes also need to be cut as destinations in adjacency lists.
for id, node := range output {
newAdjacency := report.MakeIDList()
for _, dstID := range node.Adjacency {
if _, ok := output[dstID]; ok {
newAdjacency = newAdjacency.Add(dstID)
}
}
node.Adjacency = newAdjacency
output[id] = node
}
return Nodes{Nodes: output, Filtered: filtered}
}
// Filter removes nodes from a view based on a predicate.
type Filter struct {
Renderer
FilterFunc FilterFunc
}
// MakeFilter makes a new Filter (that ignores pseudo nodes).
func MakeFilter(f FilterFunc, r Renderer) Renderer {
return Filter{
Renderer: r,
FilterFunc: func(n report.Node) bool {
return n.Topology == Pseudo || f(n)
},
}
}
// MakeFilterPseudo makes a new Filter that will not ignore pseudo nodes.
func MakeFilterPseudo(f FilterFunc, r Renderer) Renderer {
return Filter{
Renderer: r,
FilterFunc: f,
}
}
// Render implements Renderer
func (f Filter) Render(ctx context.Context, rpt report.Report) Nodes {
return f.FilterFunc.Transform(f.Renderer.Render(ctx, rpt))
}
// IsConnectedMark is the key added to Node.Metadata by
// ColorConnected to indicate a node has an edge pointing to it or
// from it
const IsConnectedMark = "is_connected"
// IsConnected checks whether the node has been marked with the
// IsConnectedMark.
func IsConnected(node report.Node) bool {
_, ok := node.Latest.Lookup(IsConnectedMark)
return ok
}
// IsPodComponent check whether given node is everything but PV, PVC, SC
func IsPodComponent(node report.Node) bool {
var ok bool
ok = true
if node.Topology == "persistent_volume" || node.Topology == "persistent_volume_claim" || node.Topology == "storage_class" {
ok = false
}
return ok
}
// IsNonSnapshotComponent checks whether given node is everything but Volume Snapshot, Volume Snapshot Data
func IsNonSnapshotComponent(node report.Node) bool {
if node.Topology == "volume_snapshot" || node.Topology == "volume_snapshot_data" {
return false
}
return true
}
// connected returns the node ids of nodes which have edges to/from
// them, excluding edges to/from themselves.
func connected(nodes report.Nodes) map[string]struct{} {
res := map[string]struct{}{}
void := struct{}{}
for id, node := range nodes {
for _, adj := range node.Adjacency {
if adj != id {
res[id] = void
res[adj] = void
}
}
}
return res
}
// filterInternetAdjacencies filters out edges between the incoming
// and outgoing internet node. These are typically artifacts of
// imperfect connection tracking, e.g. when VIPs and NAT traversal are
// in use.
func filterInternetAdjacencies(nodes report.Nodes) {
incomingInternet, ok := nodes[IncomingInternetID]
if !ok {
return
}
newAdjacency := report.MakeIDList()
for _, dstID := range incomingInternet.Adjacency {
if dstID != OutgoingInternetID {
newAdjacency = newAdjacency.Add(dstID)
}
}
incomingInternet.Adjacency = newAdjacency
nodes[IncomingInternetID] = incomingInternet
}
// ColorConnected colors nodes with the IsConnectedMark key if they
// have edges to or from them. Edges to/from yourself are not counted
// here (see #656).
func ColorConnected(r Renderer) Renderer {
return CustomRenderer{
Renderer: r,
RenderFunc: func(input Nodes) Nodes {
output := input.Copy()
for id := range connected(input.Nodes) {
output[id] = output[id].WithLatest(IsConnectedMark, mtime.Now(), "true")
}
return Nodes{Nodes: output, Filtered: input.Filtered}
},
}
}
type filterUnconnected struct {
onlyPseudo bool
}
// Transform implements Transformer
func (f filterUnconnected) Transform(input Nodes) Nodes {
output := input.Nodes.Copy()
filterInternetAdjacencies(output)
connected := connected(output)
filtered := input.Filtered
for id, node := range output {
if _, ok := connected[id]; !ok && (!f.onlyPseudo || IsPseudoTopology(node)) {
delete(output, id)
filtered++
}
}
return Nodes{Nodes: output, Filtered: filtered}
}
// FilterUnconnected is a transformer that filters unconnected nodes
var FilterUnconnected = filterUnconnected{onlyPseudo: false}
// FilterUnconnectedPseudo is a transformer that filters unconnected
// pseudo nodes
var FilterUnconnectedPseudo = filterUnconnected{onlyPseudo: true}
// Noop allows all nodes through
func Noop(_ report.Node) bool { return true }
// IsRunning checks if the node is a running docker container
func IsRunning(n report.Node) bool {
state, ok := n.Latest.Lookup(report.DockerContainerState)
return !ok || (state == report.StateRunning || state == report.StateRestarting || state == report.StatePaused)
}
// IsStopped checks if the node is *not* a running docker container
var IsStopped = Complement(IsRunning)
// IsApplication checks if the node is an "application" node
func IsApplication(n report.Node) bool {
containerName, _ := n.Latest.Lookup(report.DockerContainerName)
if _, ok := systemContainerNames[containerName]; ok {
return false
}
imageName, _ := n.Latest.Lookup(report.DockerImageName)
imagePrefix := strings.SplitN(imageName, ":", 2)[0] // :(
if _, ok := systemImagePrefixes[imagePrefix]; ok || report.IsPauseImageName(imagePrefix) {
return false
}
roleLabel, _ := n.Latest.Lookup(report.DockerLabelPrefix + "works.weave.role")
if roleLabel == "system" {
return false
}
roleLabel, _ = n.Latest.Lookup(report.DockerImageLabelPrefix + "works.weave.role")
if roleLabel == "system" {
return false
}
namespace, _ := n.Latest.Lookup(report.DockerLabelPrefix + k8sNamespaceLabel)
if namespace == "kube-system" {
return false
}
podName, _ := n.Latest.Lookup(report.DockerLabelPrefix + "io.kubernetes.pod.name")
if strings.HasPrefix(podName, "kube-system/") {
return false
}
return true
}
// IsSystem checks if the node is a "system" node
var IsSystem = Complement(IsApplication)
// HasLabel checks if the node has the desired docker label
func HasLabel(labelKey string, labelValue string) FilterFunc {
return func(n report.Node) bool {
value, _ := n.Latest.Lookup(report.DockerLabelPrefix + labelKey)
if value == labelValue {
return true
}
return false
}
}
// DoesNotHaveLabel checks if the node does NOT have the specified docker label
func DoesNotHaveLabel(labelKey string, labelValue string) FilterFunc {
return Complement(HasLabel(labelKey, labelValue))
}
// IsNotPseudo returns true if the node is not a pseudo node
// or internet/service nodes.
func IsNotPseudo(n report.Node) bool {
return n.Topology != Pseudo || IsInternetNode(n) || strings.HasPrefix(n.ID, ServiceNodeIDPrefix)
}
// IsNamespace checks if the node is a pod/service in the specified namespace
func IsNamespace(namespace string) FilterFunc {
return func(n report.Node) bool {
tryKeys := []string{report.KubernetesNamespace, report.DockerLabelPrefix + k8sNamespaceLabel, report.DockerDefaultNamespace, report.DockerLabelPrefix + swarmNamespaceLabel}
gotNamespace := ""
for _, key := range tryKeys {
if value, ok := n.Latest.Lookup(key); ok {
gotNamespace = value
break
}
}
// Special case for docker
if namespace == report.DockerDefaultNamespace && gotNamespace == "" {
return true
}
return namespace == gotNamespace
}
}
// IsTopology checks if the node is from a particular report topology
func IsTopology(topology string) FilterFunc {
return func(n report.Node) bool {
return n.Topology == topology
}
}
// IsPseudoTopology returns true if the node is in a pseudo topology,
// mimicing the check performed by MakeFilter() instead of the more complex check in IsNotPseudo()
var IsPseudoTopology = IsTopology(Pseudo)
var systemContainerNames = map[string]struct{}{
"weavescope": {},
"weavedns": {},
"weave": {},
"weaveproxy": {},
"weaveexec": {},
"ecs-agent": {},
}
var systemImagePrefixes = map[string]struct{}{
"swarm": {},
"weaveworks/scope": {},
"weaveworks/weavedns": {},
"weaveworks/weave": {},
"weaveworks/weaveproxy": {},
"weaveworks/weaveexec": {},
"amazon/amazon-ecs-agent": {},
"openshift/origin-pod": {},
"docker.io/openshift/origin-pod": {},
}