-
Notifications
You must be signed in to change notification settings - Fork 432
/
uri.go
282 lines (248 loc) · 7.63 KB
/
uri.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package cliutil
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/hashicorp/go-multierror"
errors "github.com/rotisserie/eris"
"github.com/solo-io/k8s-utils/kubeutils"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
)
// GetResource identified by the given URI.
// The URI can either be a http(s) address or a relative/absolute file path.
func GetResource(uri string) (io.ReadCloser, error) {
var file io.ReadCloser
if strings.HasPrefix(uri, "http://") || strings.HasPrefix(uri, "https://") {
resp, err := http.Get(uri)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("http GET returned status %d for resource %s", resp.StatusCode, uri)
}
file = resp.Body
} else {
path, err := filepath.Abs(uri)
if err != nil {
return nil, errors.Wrapf(err, "getting absolute path for %v", uri)
}
f, err := os.Open(path)
if err != nil {
return nil, errors.Wrapf(err, "opening file %v", path)
}
file = f
}
// Write the body to file
return file, nil
}
// GetIngressHost returns the host address of the ingress
func GetIngressHost(ctx context.Context, proxyName, proxyNamespace, proxyPort string, localCluster bool, clusterName string) (string, error) {
restCfg, err := kubeutils.GetConfig("", "")
if err != nil {
return "", errors.Wrapf(err, "getting kube rest config")
}
kube, err := kubernetes.NewForConfig(restCfg)
if err != nil {
return "", errors.Wrapf(err, "starting kube client")
}
svc, err := kube.CoreV1().Services(proxyNamespace).Get(ctx, proxyName, metav1.GetOptions{})
if err != nil {
return "", errors.Wrapf(err, "could not detect '%v' service in %v namespace. "+
"Check that Gloo has been installed properly and is running with 'kubectl get pod -n gloo-system'",
proxyName, proxyNamespace)
}
var svcPort *v1.ServicePort
switch len(svc.Spec.Ports) {
case 0:
return "", errors.Errorf("service %v is missing ports", proxyName)
case 1:
svcPort = &svc.Spec.Ports[0]
default:
for _, p := range svc.Spec.Ports {
if p.Name == proxyPort {
svcPort = &p
break
}
}
if svcPort == nil {
return "", errors.Errorf("named port %v not found on service %v", proxyPort, proxyName)
}
}
var host, port string
serviceType := svc.Spec.Type
if localCluster {
serviceType = v1.ServiceTypeNodePort
}
switch serviceType {
case v1.ServiceTypeClusterIP:
// There are a few edge cases where glooctl could be run in an environment where this is not a fatal error
// However the service type ClusterIP does not accept incoming traffic which doesnt work as a ingress
logger := GetLogger()
logger.Write([]byte("Warning: Potentially invalid proxy configuration, proxy may not accepting incoming connections"))
host = svc.Spec.ClusterIP
port = fmt.Sprintf("%v", svcPort.Port)
case v1.ServiceTypeNodePort:
// TODO: support more types of NodePort services
host, err = getNodeIp(ctx, svc, kube, clusterName)
if err != nil {
return "", errors.Wrapf(err, "")
}
port = fmt.Sprintf("%v", svcPort.NodePort)
case v1.ServiceTypeLoadBalancer:
if len(svc.Status.LoadBalancer.Ingress) == 0 {
return "", errors.Errorf("load balancer ingress not found on service %v", proxyName)
}
host = svc.Status.LoadBalancer.Ingress[0].Hostname
if host == "" {
host = svc.Status.LoadBalancer.Ingress[0].IP
}
port = fmt.Sprintf("%v", svcPort.Port)
}
return host + ":" + port, nil
}
func getNodeIp(ctx context.Context, svc *v1.Service, kube kubernetes.Interface, clusterName string) (string, error) {
// pick a node where one of our pods is running
pods, err := kube.CoreV1().Pods(svc.Namespace).List(ctx, metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(svc.Spec.Selector).String(),
})
if err != nil {
return "", err
}
var nodeName string
for _, pod := range pods.Items {
if pod.Spec.NodeName != "" {
nodeName = pod.Spec.NodeName
break
}
}
if nodeName == "" {
return "", errors.Errorf("no node found for %v's pods. ensure at least one pod has been deployed "+
"for the %v service", svc.Name, svc.Name)
}
// special case for minikube
// we run `minikube ip` which avoids an issue where
// we get a NAT network IP when the minikube provider is virtualbox
if nodeName == "minikube" {
return minikubeIp(clusterName)
}
node, err := kube.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{})
if err != nil {
return "", err
}
for _, addr := range node.Status.Addresses {
return addr.Address, nil
}
return "", errors.Errorf("no active addresses found for node %v", node.Name)
}
func minikubeIp(clusterName string) (string, error) {
minikubeCmd := exec.Command("minikube", "ip", "-p", clusterName)
hostname := &bytes.Buffer{}
minikubeCmd.Stdout = hostname
minikubeCmd.Stderr = os.Stderr
err := minikubeCmd.Run()
return strings.TrimSuffix(hostname.String(), "\n"), err
}
// PortForward call kubectl port-forward. Callers are expected to clean up the returned portFwd *exec.cmd after the port-forward is no longer needed.
func PortForward(namespace string, resource string, localPort string, kubePort string, verbose bool) (*exec.Cmd, error) {
/** port-forward command **/
portFwd := exec.Command("kubectl", "port-forward", "-n", namespace,
resource, fmt.Sprintf("%s:%s", localPort, kubePort))
err := Initialize()
if err != nil {
return nil, err
}
logger := GetLogger()
portFwd.Stderr = io.MultiWriter(logger, os.Stderr)
if verbose {
portFwd.Stdout = io.MultiWriter(logger, os.Stdout)
} else {
portFwd.Stdout = logger
}
if err := portFwd.Start(); err != nil {
return nil, err
}
return portFwd, nil
}
// PortForwardGet call kubectl port-forward and make a GET request.
// Callers are expected to clean up the returned portFwd *exec.cmd after the port-forward is no longer needed.
func PortForwardGet(ctx context.Context, namespace string, resource string, localPort string, kubePort string, verbose bool, getPath string) (string, *exec.Cmd, error) {
/** port-forward command **/
portFwd, err := PortForward(namespace, resource, localPort, kubePort, verbose)
if err != nil {
return "", nil, err
}
localCtx, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()
// wait for port-forward to be ready
retryInterval := time.Millisecond * 250
result := make(chan string)
errs := make(chan error)
go func() {
for {
select {
case <-localCtx.Done():
return
default:
}
res, err := http.Get("http://localhost:" + localPort + getPath)
if err != nil {
errs <- err
time.Sleep(retryInterval)
continue
}
if res.StatusCode != 200 {
errs <- errors.Errorf("invalid status code: %v %v", res.StatusCode, res.Status)
time.Sleep(retryInterval)
continue
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
errs <- err
time.Sleep(retryInterval)
continue
}
res.Body.Close()
result <- string(b)
return
}
}()
var multiErr *multierror.Error
for {
select {
case err := <-errs:
multiErr = multierror.Append(multiErr, err)
case res := <-result:
return res, portFwd, nil
case <-localCtx.Done():
if portFwd.Process != nil {
portFwd.Process.Kill()
portFwd.Process.Release()
}
return "", nil, errors.Errorf("timed out trying to connect to localhost during port-forward, errors: %v", multiErr)
}
}
}
func GetFreePort() (int, error) {
l, err := net.Listen("tcp", ":0")
if err != nil {
return 0, err
}
defer l.Close()
tcpAddr, ok := l.Addr().(*net.TCPAddr)
if !ok {
return 0, errors.Errorf("Error occurred looking for an open tcp port")
}
return tcpAddr.Port, nil
}