Repository navigation
Expand file tree
/
Copy pathreader.go
More file actions
72 lines (59 loc) · 1.71 KB
/
Copy pathreader.go
File metadata and controls
72 lines (59 loc) · 1.71 KB
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
package pkg
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/url"
"github.com/golang/protobuf/proto"
"github.com/linkerd/linkerd2/pkg/k8s"
"github.com/linkerd/linkerd2/pkg/protohttp"
pb "github.com/linkerd/linkerd2/viz/tap/gen/tap"
log "github.com/sirupsen/logrus"
)
const (
// ErrClosedResponseBody is returned when response body is closed in http2
ErrClosedResponseBody = "http2: response body closed"
)
// TapRbacURL is the link users should visit to remedy issues when attempting
// to tap resources with missing authorizations
const TapRbacURL = "https://linkerd.io/tap-rbac"
// Reader initiates a TapByResourceRequest and returns a buffered Reader.
// It is the caller's responsibility to call Close() on the io.ReadCloser.
func Reader(ctx context.Context, k8sAPI *k8s.KubernetesAPI, req *pb.TapByResourceRequest) (*bufio.Reader, io.ReadCloser, error) {
client, err := k8sAPI.NewClient()
if err != nil {
return nil, nil, err
}
reqBytes, err := proto.Marshal(req)
if err != nil {
return nil, nil, err
}
url, err := url.Parse(k8sAPI.Host)
if err != nil {
return nil, nil, err
}
url.Path = fmt.Sprintf("%s%s", url.Path, TapReqToURL(req))
httpReq, err := http.NewRequest(
http.MethodPost,
url.String(),
bytes.NewReader(reqBytes),
)
if err != nil {
return nil, nil, err
}
httpRsp, err := client.Do(httpReq.WithContext(ctx))
if err != nil {
log.Debugf("Error invoking [%s]: %v", url, err)
return nil, nil, err
}
log.Debugf("Response from [%s] had headers: %v", url, httpRsp.Header)
if err := protohttp.CheckIfResponseHasError(httpRsp); err != nil {
httpRsp.Body.Close()
return nil, nil, err
}
reader := bufio.NewReader(httpRsp.Body)
return reader, httpRsp.Body, nil
}