-
Notifications
You must be signed in to change notification settings - Fork 712
/
topologies.go
289 lines (257 loc) · 6.94 KB
/
topologies.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
package render
import (
"fmt"
"net"
"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/probe/host"
"github.com/weaveworks/scope/probe/process"
"github.com/weaveworks/scope/report"
)
// EndpointRenderer is a Renderer which produces a renderable endpoint graph.
var EndpointRenderer = MakeMap(
MapEndpointIdentity,
SelectEndpoint,
)
// ProcessRenderer is a Renderer which produces a renderable process
// graph by merging the endpoint graph and the process topology.
var ProcessRenderer = MakeReduce(
MakeMap(
MapEndpoint2Process,
EndpointRenderer,
),
MakeMap(
MapProcessIdentity,
SelectProcess,
),
)
// processWithContainerNameRenderer is a Renderer which produces a process
// graph enriched with container names where appropriate
type processWithContainerNameRenderer struct {
Renderer
}
func (r processWithContainerNameRenderer) Render(rpt report.Report) RenderableNodes {
processes := r.Renderer.Render(rpt)
containers := MakeMap(
MapContainerIdentity,
SelectContainer,
).Render(rpt)
for id, p := range processes {
pid, ok := p.Node.Latest.Lookup(process.PID)
if !ok {
continue
}
containerID, ok := p.Node.Latest.Lookup(docker.ContainerID)
if !ok {
continue
}
container, ok := containers[MakeContainerID(containerID)]
if !ok {
continue
}
p.LabelMinor = fmt.Sprintf("%s (%s:%s)", report.ExtractHostID(p.Node), container.LabelMajor, pid)
processes[id] = p
}
return processes
}
// ProcessWithContainerNameRenderer is a Renderer which produces a process
// graph enriched with container names where appropriate
var ProcessWithContainerNameRenderer = processWithContainerNameRenderer{ProcessRenderer}
// ProcessNameRenderer is a Renderer which produces a renderable process
// name graph by munging the progess graph.
var ProcessNameRenderer = MakeMap(
MapCountProcessName,
MakeMap(
MapProcess2Name,
ProcessRenderer,
),
)
// ContainerRenderer is a Renderer which produces a renderable container
// graph by merging the process graph and the container topology.
// NB We only want processes in container _or_ processes with network connections
// but we need to be careful to ensure we only include each edge once, by only
// including the ProcessRenderer once.
var ContainerRenderer = MakeReduce(
MakeFilter(
func(n RenderableNode) bool {
_, inContainer := n.Node.Latest.Lookup(docker.ContainerID)
_, isConnected := n.Node.Latest.Lookup(IsConnected)
return inContainer || isConnected
},
MakeMap(
MapProcess2Container,
ColorConnected(ProcessRenderer),
),
),
// This mapper brings in short lived connections by joining with container IPs.
// We need to be careful to ensure we only include each edge once. Edges brought in
// by the above renders will have a pid, so its enough to filter out any nodes with
// pids.
FilterUnconnected(MakeMap(
MapIP2Container,
MakeReduce(
MakeMap(
MapContainer2IP,
SelectContainer,
),
MakeMap(
MapEndpoint2IP,
SelectEndpoint,
),
),
)),
MakeMap(
MapContainerIdentity,
SelectContainer,
),
)
type containerWithHostIPsRenderer struct {
Renderer
}
// Render produces a process graph where the ips for host network mode are set
// to the host's IPs.
func (r containerWithHostIPsRenderer) Render(rpt report.Report) RenderableNodes {
containers := r.Renderer.Render(rpt)
hosts := MakeMap(
MapHostIdentity,
SelectHost,
).Render(rpt)
for id, c := range containers {
networkMode, ok := c.Node.Latest.Lookup(docker.ContainerNetworkMode)
if !ok || networkMode != docker.NetworkModeHost {
continue
}
h, ok := hosts[MakeHostID(report.ExtractHostID(c.Node))]
if !ok {
continue
}
newIPs := report.MakeStringSet()
hostNetworks, _ := h.Sets.Lookup(host.LocalNetworks)
for _, cidr := range hostNetworks {
if ip, _, err := net.ParseCIDR(cidr); err == nil {
newIPs = newIPs.Add(ip.String())
}
}
c.Sets = c.Sets.Add(docker.ContainerIPs, newIPs)
containers[id] = c
}
return containers
}
// ContainerWithHostIPsRenderer is a Renderer which produces a container graph
// enriched with host IPs on containers where NetworkMode is Host
var ContainerWithHostIPsRenderer = containerWithHostIPsRenderer{ContainerRenderer}
type containerWithImageNameRenderer struct {
Renderer
}
// Render produces a process graph where the minor labels contain the
// container name, if found. It also merges the image node metadata into the
// container metadata.
func (r containerWithImageNameRenderer) Render(rpt report.Report) RenderableNodes {
containers := r.Renderer.Render(rpt)
images := MakeMap(
MapContainerImageIdentity,
SelectContainerImage,
).Render(rpt)
for id, c := range containers {
imageID, ok := c.Node.Latest.Lookup(docker.ImageID)
if !ok {
continue
}
image, ok := images[MakeContainerImageID(imageID)]
if !ok {
continue
}
c.Rank = ImageNameWithoutVersion(image.LabelMajor)
c.Latest = image.Latest.Merge(c.Latest)
containers[id] = c
}
return containers
}
// ContainerWithImageNameRenderer is a Renderer which produces a container
// graph where the ranks are the image names, not their IDs
var ContainerWithImageNameRenderer = containerWithImageNameRenderer{ContainerWithHostIPsRenderer}
// ContainerImageRenderer is a Renderer which produces a renderable container
// image graph by merging the container graph and the container image topology.
var ContainerImageRenderer = MakeMap(
MapCountContainers,
MakeMap(
MapContainerImage2Name,
MakeReduce(
MakeMap(
MapContainer2ContainerImage,
ContainerRenderer,
),
MakeMap(
MapContainerImageIdentity,
SelectContainerImage,
),
),
),
)
// ContainerHostnameRenderer is a Renderer which produces a renderable container
// by hostname graph..
var ContainerHostnameRenderer = MakeMap(
MapCountContainers,
MakeMap(
MapContainer2Hostname,
ContainerRenderer,
),
)
// AddressRenderer is a Renderer which produces a renderable address
// graph from the address topology.
var AddressRenderer = MakeMap(
MapAddressIdentity,
SelectAddress,
)
// HostRenderer is a Renderer which produces a renderable host
// graph from the host topology and address graph.
var HostRenderer = MakeReduce(
MakeMap(
MapX2Host,
FilterPseudo(ContainerImageRenderer),
),
MakeMap(
MapX2Host,
MakeMap(
MapPodIdentity,
SelectPod,
),
),
MakeMap(
MapX2Host,
AddressRenderer,
),
MakeMap(
MapHostIdentity,
SelectHost,
),
)
// PodRenderer is a Renderer which produces a renderable kubernetes
// graph by merging the container graph and the pods topology.
var PodRenderer = MakeMap(
MapCountContainers,
MakeReduce(
MakeMap(
MapContainer2Pod,
ContainerRenderer,
),
MakeMap(
MapPodIdentity,
SelectPod,
),
),
)
// PodServiceRenderer is a Renderer which produces a renderable kubernetes services
// graph by merging the pods graph and the services topology.
var PodServiceRenderer = MakeMap(
MapCountPods,
MakeReduce(
MakeMap(
MapPod2Service,
PodRenderer,
),
MakeMap(
MapServiceIdentity,
SelectService,
),
),
)