-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
map.go
158 lines (132 loc) · 3.83 KB
/
map.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package cmd
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/go-openapi/runtime"
"github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/strfmt"
"github.com/cilium/cilium/api/v1/models"
restapi "github.com/cilium/cilium/api/v1/server/restapi/daemon"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/ebpf"
"github.com/cilium/cilium/pkg/metrics"
)
type eventsDumper interface {
DumpAndSubscribe(cb bpf.EventCallbackFunc, follow bool) (*bpf.Handle, error)
IsEventsEnabled() bool
}
type mapRefGetter interface {
GetMap(name string) (eventsDumper, bool)
}
type mapGetterImpl struct{}
func (mg mapGetterImpl) GetMap(name string) (eventsDumper, bool) {
m := bpf.GetMap(name)
return m, m != nil
}
type getMapNameEvents struct {
daemon *Daemon
mapGetter mapRefGetter
}
func NewGetMapNameEventsHandler(d *Daemon, maps mapRefGetter) restapi.GetMapNameEventsHandler {
return &getMapNameEvents{
daemon: d,
mapGetter: maps,
}
}
// TODO: This will be easy to break, is there a way we can refactor to make changes to metrics.ResponderWrapper fail
// fail at compile time?
func getFlusher(w http.ResponseWriter) (http.Flusher, error) {
wrapper, ok := w.(*metrics.ResponderWrapper)
if !ok {
return nil, fmt.Errorf("expected ResponseWriter to be of type *metrics.ResponseWrapper: %T", w)
}
f, ok := wrapper.ResponseWriter.(http.Flusher)
if !ok {
return nil, fmt.Errorf("failed to get http.Flusher from ResponseWriter: %T", w)
}
return f, nil
}
type flushWriter struct {
f http.Flusher
w io.Writer
}
func (fw *flushWriter) Write(p []byte) (n int, err error) {
n, err = fw.w.Write(p)
if fw.f != nil {
fw.f.Flush()
}
return
}
func getMapNameEventsHandler(d *Daemon, params restapi.GetMapNameEventsParams) middleware.Responder {
g := &getMapNameEvents{daemon: d, mapGetter: mapGetterImpl{}}
return g.Handle(params)
}
func (h *getMapNameEvents) Handle(params restapi.GetMapNameEventsParams) middleware.Responder {
follow := false
if params.Follow != nil {
follow = *params.Follow
}
m, exists := h.mapGetter.GetMap(params.Name)
if !exists || !m.IsEventsEnabled() {
return restapi.NewGetMapNameNotFound()
}
return middleware.ResponderFunc(func(w http.ResponseWriter, _ runtime.Producer) {
flusher, err := getFlusher(w)
if err != nil {
log.WithError(err).Error("BUG: could not get flushed from ResponseWriter")
w.WriteHeader(http.StatusInternalServerError)
return
}
enc := json.NewEncoder(&flushWriter{f: flusher, w: w})
writeEventFn := func(e *bpf.Event) {
errStr := "<nil>"
if e.GetLastError() != nil {
errStr = e.GetLastError().Error()
}
err := enc.Encode(&models.MapEvent{
Key: e.GetKey(),
Action: e.GetAction(),
Value: e.GetValue(),
LastError: errStr,
Timestamp: strfmt.DateTime(e.Timestamp),
DesiredAction: e.GetDesiredAction().String(),
})
if err != nil {
panic(err)
}
flusher.Flush()
}
h, err := m.DumpAndSubscribe(writeEventFn, follow)
if err != nil {
log.WithError(err).Error("api handler failed to subscribe to map events")
w.WriteHeader(http.StatusInternalServerError)
return
}
if follow && h != nil {
go func() {
<-params.HTTPRequest.Context().Done()
h.Close()
}()
for e := range h.C() {
writeEventFn(e)
}
}
})
}
func getMapNameHandler(_ *Daemon, params restapi.GetMapNameParams) middleware.Responder {
m := bpf.GetMap(params.Name)
if m == nil {
return restapi.NewGetMapNameNotFound()
}
return restapi.NewGetMapNameOK().WithPayload(m.GetModel())
}
func getMapHandler(_ *Daemon, params restapi.GetMapParams) middleware.Responder {
mapList := &models.BPFMapList{
Maps: append(bpf.GetOpenMaps(), ebpf.GetOpenMaps()...),
}
return restapi.NewGetMapOK().WithPayload(mapList)
}