forked from linkerd/linkerd2
/
http_server.go
217 lines (187 loc) · 5.07 KB
/
http_server.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
package public
import (
"context"
"fmt"
"net/http"
healthcheckPb "github.com/linkerd/linkerd2/controller/gen/common/healthcheck"
tapPb "github.com/linkerd/linkerd2/controller/gen/controller/tap"
pb "github.com/linkerd/linkerd2/controller/gen/public"
"github.com/linkerd/linkerd2/controller/k8s"
"github.com/linkerd/linkerd2/pkg/prometheus"
promApi "github.com/prometheus/client_golang/api"
promv1 "github.com/prometheus/client_golang/api/prometheus/v1"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/metadata"
)
var (
statSummaryPath = fullUrlPathFor("StatSummary")
versionPath = fullUrlPathFor("Version")
listPodsPath = fullUrlPathFor("ListPods")
tapByResourcePath = fullUrlPathFor("TapByResource")
selfCheckPath = fullUrlPathFor("SelfCheck")
)
type handler struct {
grpcServer pb.ApiServer
}
func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
log.WithFields(log.Fields{
"req.Method": req.Method, "req.URL": req.URL, "req.Form": req.Form,
}).Debugf("Serving %s %s", req.Method, req.URL.Path)
// Validate request method
if req.Method != http.MethodPost {
writeErrorToHttpResponse(w, fmt.Errorf("POST required"))
return
}
// Serve request
switch req.URL.Path {
case statSummaryPath:
h.handleStatSummary(w, req)
case versionPath:
h.handleVersion(w, req)
case listPodsPath:
h.handleListPods(w, req)
case tapByResourcePath:
h.handleTapByResource(w, req)
case selfCheckPath:
h.handleSelfCheck(w, req)
default:
http.NotFound(w, req)
}
}
func (h *handler) handleStatSummary(w http.ResponseWriter, req *http.Request) {
var protoRequest pb.StatSummaryRequest
err := httpRequestToProto(req, &protoRequest)
if err != nil {
writeErrorToHttpResponse(w, err)
return
}
rsp, err := h.grpcServer.StatSummary(req.Context(), &protoRequest)
if err != nil {
writeErrorToHttpResponse(w, err)
return
}
err = writeProtoToHttpResponse(w, rsp)
if err != nil {
writeErrorToHttpResponse(w, err)
return
}
}
func (h *handler) handleVersion(w http.ResponseWriter, req *http.Request) {
var protoRequest pb.Empty
err := httpRequestToProto(req, &protoRequest)
if err != nil {
writeErrorToHttpResponse(w, err)
return
}
rsp, err := h.grpcServer.Version(req.Context(), &protoRequest)
if err != nil {
writeErrorToHttpResponse(w, err)
return
}
err = writeProtoToHttpResponse(w, rsp)
if err != nil {
writeErrorToHttpResponse(w, err)
return
}
}
func (h *handler) handleSelfCheck(w http.ResponseWriter, req *http.Request) {
var protoRequest healthcheckPb.SelfCheckRequest
err := httpRequestToProto(req, &protoRequest)
if err != nil {
writeErrorToHttpResponse(w, err)
return
}
rsp, err := h.grpcServer.SelfCheck(req.Context(), &protoRequest)
if err != nil {
writeErrorToHttpResponse(w, err)
return
}
err = writeProtoToHttpResponse(w, rsp)
if err != nil {
writeErrorToHttpResponse(w, err)
return
}
}
func (h *handler) handleListPods(w http.ResponseWriter, req *http.Request) {
var protoRequest pb.ListPodsRequest
err := httpRequestToProto(req, &protoRequest)
if err != nil {
writeErrorToHttpResponse(w, err)
return
}
rsp, err := h.grpcServer.ListPods(req.Context(), &protoRequest)
if err != nil {
writeErrorToHttpResponse(w, err)
return
}
err = writeProtoToHttpResponse(w, rsp)
if err != nil {
writeErrorToHttpResponse(w, err)
return
}
}
func (h *handler) handleTapByResource(w http.ResponseWriter, req *http.Request) {
flushableWriter, err := newStreamingWriter(w)
if err != nil {
writeErrorToHttpResponse(w, err)
return
}
var protoRequest pb.TapByResourceRequest
err = httpRequestToProto(req, &protoRequest)
if err != nil {
writeErrorToHttpResponse(w, err)
return
}
server := tapServer{w: flushableWriter, req: req}
err = h.grpcServer.TapByResource(&protoRequest, server)
if err != nil {
writeErrorToHttpResponse(w, err)
return
}
}
type tapServer struct {
w flushableResponseWriter
req *http.Request
}
func (s tapServer) Send(msg *pb.TapEvent) error {
err := writeProtoToHttpResponse(s.w, msg)
if err != nil {
writeErrorToHttpResponse(s.w, err)
return err
}
s.w.Flush()
return nil
}
// satisfy the pb.Api_TapServer interface
func (s tapServer) SetHeader(metadata.MD) error { return nil }
func (s tapServer) SendHeader(metadata.MD) error { return nil }
func (s tapServer) SetTrailer(metadata.MD) {}
func (s tapServer) Context() context.Context { return s.req.Context() }
func (s tapServer) SendMsg(interface{}) error { return nil }
func (s tapServer) RecvMsg(interface{}) error { return nil }
func fullUrlPathFor(method string) string {
return apiRoot + apiPrefix + method
}
func NewServer(
addr string,
prometheusClient promApi.Client,
tapClient tapPb.TapClient,
k8sAPI *k8s.API,
controllerNamespace string,
ignoredNamespaces []string,
) *http.Server {
baseHandler := &handler{
grpcServer: newGrpcServer(
promv1.NewAPI(prometheusClient),
tapClient,
k8sAPI,
controllerNamespace,
ignoredNamespaces,
),
}
instrumentedHandler := prometheus.WithTelemetry(baseHandler)
return &http.Server{
Addr: addr,
Handler: instrumentedHandler,
}
}