This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
report.go
326 lines (297 loc) · 7.89 KB
/
report.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
package main
import (
"encoding/json"
"fmt"
"strings"
"time"
)
const (
trafficControlTablePrefix = "traffic-control-table-"
)
type report struct {
Container topology
Plugins []pluginSpec
}
type topology struct {
Nodes map[string]node `json:"nodes"`
Controls map[string]control `json:"controls"`
MetadataTemplates map[string]metadataTemplate `json:"metadata_templates,omitempty"`
TableTemplates map[string]tableTemplate `json:"table_templates,omitempty"`
}
type tableTemplate struct {
ID string `json:"id"`
Label string `json:"label"`
Prefix string `json:"prefix"`
}
type metadataTemplate struct {
ID string `json:"id"`
Label string `json:"label,omitempty"` // Human-readable descriptor for this row
Truncate int `json:"truncate,omitempty"` // If > 0, truncate the value to this length.
Datatype string `json:"dataType,omitempty"`
Priority float64 `json:"priority,omitempty"`
From string `json:"from,omitempty"` // Defines how to get the value from a report node
}
type node struct {
LatestControls map[string]controlEntry `json:"latestControls,omitempty"`
Latest map[string]stringEntry `json:"latest,omitempty"`
}
type controlEntry struct {
Timestamp time.Time `json:"timestamp"`
Value controlData `json:"value"`
}
type controlData struct {
Dead bool `json:"dead"`
}
type control struct {
ID string `json:"id"`
Human string `json:"human"`
Icon string `json:"icon"`
Rank int `json:"rank"`
}
type stringEntry struct {
Timestamp time.Time `json:"timestamp"`
Value string `json:"value"`
}
type pluginSpec struct {
ID string `json:"id"`
Label string `json:"label"`
Description string `json:"description,omitempty"`
Interfaces []string `json:"interfaces"`
APIVersion string `json:"api_version,omitempty"`
}
// Reporter internal data structure
type Reporter struct {
store *Store
}
// NewReporter instantiates a new Reporter
func NewReporter(store *Store) *Reporter {
return &Reporter{
store: store,
}
}
// RawReport returns a report
func (r *Reporter) RawReport() ([]byte, error) {
rpt := &report{
Container: topology{
Nodes: r.getContainerNodes(),
Controls: getTrafficControls(),
MetadataTemplates: getMetadataTemplate(),
TableTemplates: getTableTemplate(),
},
Plugins: []pluginSpec{
{
ID: "traffic-control",
Label: "Traffic control",
Description: "Adds traffic controls to the running Docker containers",
Interfaces: []string{"reporter", "controller"},
APIVersion: "1",
},
},
}
raw, err := json.Marshal(rpt)
if err != nil {
return nil, fmt.Errorf("failed to marshal the report: %v", err)
}
return raw, nil
}
// GetHandler returns the function performing the action specified by controlID
func (r *Reporter) GetHandler(nodeID, controlID string) (func() error, error) {
containerID, err := nodeIDToContainerID(nodeID)
if err != nil {
return nil, fmt.Errorf("failed to get container ID from node ID %q: %v", nodeID, err)
}
container, found := r.store.Container(containerID)
if !found {
return nil, fmt.Errorf("container %s not found", containerID)
}
var handler func(pid int) error
for _, c := range getControls() {
if c.control.ID == controlID {
handler = c.handler
break
}
}
if handler == nil {
return nil, fmt.Errorf("unknown control ID %q for node ID %q", controlID, nodeID)
}
return func() error {
return handler(container.PID)
}, nil
}
// states:
// created, destroyed - don't create any node
// running, not running - create node with controls
func (r *Reporter) getContainerNodes() map[string]node {
nodes := map[string]node{}
timestamp := time.Now()
r.store.ForEach(func(containerID string, container Container) {
dead := false
switch container.State {
case Created, Destroyed:
// do nothing, to prevent adding a stale node
// to a report
case Stopped:
dead = true
fallthrough
case Running:
nodeID := containerIDToNodeID(containerID)
latency, _ := getLatency(container.PID)
packetLoss, _ := getPacketLoss(container.PID)
nodes[nodeID] = node{
LatestControls: getTrafficNodeControls(timestamp, dead),
Latest: map[string]stringEntry{
fmt.Sprintf("%s%s", trafficControlTablePrefix, "latency"): {
Timestamp: timestamp,
Value: latency,
},
fmt.Sprintf("%s%s", trafficControlTablePrefix, "pktloss"): {
Timestamp: timestamp,
Value: packetLoss,
},
},
}
}
})
return nodes
}
func getMetadataTemplate() map[string]metadataTemplate {
return map[string]metadataTemplate{
"traffic-control-latency": {
ID: "traffic-control-latency",
Label: "Latency",
Truncate: 0,
Datatype: "",
Priority: 13.5,
From: "latest",
},
"traffic-control-pktloss": {
ID: "traffic-control-pktloss",
Label: "Packet Loss",
Truncate: 0,
Datatype: "",
Priority: 13.6,
From: "latest",
},
}
}
func getTableTemplate() map[string]tableTemplate {
return map[string]tableTemplate{
"traffic-control-table": {
ID: "traffic-control-table",
Label: "Traffic Control",
Prefix: trafficControlTablePrefix,
},
}
}
func getTrafficNodeControls(timestamp time.Time, dead bool) map[string]controlEntry {
controls := map[string]controlEntry{}
entry := controlEntry{
Timestamp: timestamp,
Value: controlData{
Dead: dead,
},
}
for _, c := range getControls() {
controls[c.control.ID] = entry
}
return controls
}
func getTrafficControls() map[string]control {
controls := map[string]control{}
for _, c := range getControls() {
controls[c.control.ID] = c.control
}
return controls
}
type extControl struct {
control control
handler func(pid int) error
}
func getLatencyControls() []extControl {
return []extControl{
{
control: control{
ID: fmt.Sprintf("%s%s", trafficControlTablePrefix, "slow"),
Human: "Traffic speed: slow",
Icon: "fa-hourglass-1",
Rank: 20,
},
handler: func(pid int) error {
return ApplyLatency(pid, "2000ms")
},
},
{
control: control{
ID: fmt.Sprintf("%s%s", trafficControlTablePrefix, "medium"),
Human: "Traffic speed: medium",
Icon: "fa-hourglass-2",
Rank: 21,
},
handler: func(pid int) error {
return ApplyLatency(pid, "1000ms")
},
},
{
control: control{
ID: fmt.Sprintf("%s%s", trafficControlTablePrefix, "fast"),
Human: "Traffic speed: fast",
Icon: "fa-hourglass-3",
Rank: 22,
},
handler: func(pid int) error {
return ApplyLatency(pid, "500ms")
},
},
}
}
func getPacketLossControls() []extControl {
return []extControl{
{
control: control{
ID: fmt.Sprintf("%s%s", trafficControlTablePrefix, "pkt-drop-low"),
Human: "Packet drop: low",
Icon: "fa-cut",
Rank: 23,
},
handler: func(pid int) error {
return ApplyPacketLoss(pid, "10%")
},
},
}
}
func getGeneralControls() []extControl {
return []extControl{
{
control: control{
ID: fmt.Sprintf("%s%s", trafficControlTablePrefix, "clear"),
Human: "Clear traffic control settings",
Icon: "fa-times-circle",
Rank: 24,
},
handler: func(pid int) error {
return ClearTrafficControlSettings(pid)
},
},
}
}
func getControls() []extControl {
controls := getLatencyControls()
// TODO alepuccetti why append(controls, getPacketLossControls()) does not work?
for _, ctrl := range getPacketLossControls() {
controls = append(controls, ctrl)
}
for _, ctrl := range getGeneralControls() {
controls = append(controls, ctrl)
}
return controls
}
const nodeSuffix = ";<container>"
func containerIDToNodeID(containerID string) string {
return fmt.Sprintf("%s%s", containerID, nodeSuffix)
}
func nodeIDToContainerID(nodeID string) (string, error) {
if !strings.HasSuffix(nodeID, nodeSuffix) {
return "", fmt.Errorf("no suffix %q in node ID %q", nodeSuffix, nodeID)
}
return strings.TrimSuffix(nodeID, nodeSuffix), nil
}