forked from openservicemesh/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvoy.go
More file actions
54 lines (44 loc) · 1.33 KB
/
envoy.go
File metadata and controls
54 lines (44 loc) · 1.33 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
package debugger
import (
"fmt"
"io/ioutil"
"math/rand"
"net/http"
v1 "k8s.io/api/core/v1"
)
func (ds DebugConfig) getEnvoyConfig(pod *v1.Pod, url string) string {
log.Debug().Msgf("Getting Envoy config on Pod with UID=%s", pod.ObjectMeta.UID)
minPort := 16000
maxPort := 18000
// #nosec G404
portFwdRequest := portForward{
Pod: pod,
LocalPort: rand.Intn(maxPort-minPort) + minPort,
PodPort: 15000,
Stop: make(chan struct{}),
Ready: make(chan struct{}),
}
go ds.forwardPort(portFwdRequest)
<-portFwdRequest.Ready
client := &http.Client{}
resp, err := client.Get(fmt.Sprintf("http://%s:%d/%s", "localhost", portFwdRequest.LocalPort, url))
if err != nil {
log.Error().Err(err).Msgf("Error getting Pod with UID=%s", pod.ObjectMeta.UID)
return fmt.Sprintf("Error: %s", err)
}
defer func() {
portFwdRequest.Stop <- struct{}{}
_ = resp.Body.Close()
}()
if resp.StatusCode != http.StatusOK {
log.Error().Msgf("Error getting Envoy config on Pod with UID=%s; HTTP Error %d", pod.ObjectMeta.UID, resp.StatusCode)
portFwdRequest.Stop <- struct{}{}
return fmt.Sprintf("Error: %s", err)
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error().Err(err).Msgf("Error getting Pod with UID=%s", pod.ObjectMeta.UID)
return fmt.Sprintf("Error: %s", err)
}
return string(bodyBytes)
}