-
Notifications
You must be signed in to change notification settings - Fork 7
/
proxy_get.go
68 lines (58 loc) · 2.07 KB
/
proxy_get.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
package cli
import (
"context"
"fmt"
"io/ioutil"
"net/http"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"github.com/flomesh-io/fsm/pkg/constants"
"github.com/flomesh-io/fsm/pkg/k8s"
"github.com/flomesh-io/fsm/pkg/mesh"
)
// GetSidecarProxyConfig returns the sidecar proxy config of a pod
func GetSidecarProxyConfig(clientSet kubernetes.Interface, config *rest.Config, namespace string, podName string, localPort uint16, query string) ([]byte, error) {
// Check if the pod belongs to a mesh
pod, err := clientSet.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("Could not find pod %s in namespace %s", podName, namespace)
}
if !mesh.ProxyLabelExists(*pod) {
return nil, fmt.Errorf("Pod %s in namespace %s is not a part of a mesh", podName, namespace)
}
if pod.Status.Phase != corev1.PodRunning {
return nil, fmt.Errorf("Pod %s in namespace %s is not running", podName, namespace)
}
dialer, err := k8s.DialerToPod(config, clientSet, podName, namespace)
if err != nil {
return nil, err
}
portForwarder, err := k8s.NewPortForwarder(dialer, fmt.Sprintf("%d:%d", localPort, constants.SidecarAdminPort))
if err != nil {
return nil, fmt.Errorf("Error setting up port forwarding: %w", err)
}
var sidecarProxyConfig []byte
err = portForwarder.Start(func(pf *k8s.PortForwarder) error {
defer pf.Stop()
url := fmt.Sprintf("http://localhost:%d/%s", localPort, query)
// #nosec G107: Potential HTTP request made with variable url
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("Error fetching url %s: %w", url, err)
}
//nolint: errcheck
//#nosec G307
defer resp.Body.Close()
sidecarProxyConfig, err = ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("Error rendering HTTP response: %w", err)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("Error retrieving proxy config for pod %s in namespace %s: %w", podName, namespace, err)
}
return sidecarProxyConfig, nil
}