forked from k8sgateway/k8sgateway
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uri.go
304 lines (272 loc) · 8.5 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package cliutil
import (
"bytes"
"context"
"fmt"
"io"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/solo-io/gloo/pkg/utils/kubeutils"
"github.com/avast/retry-go/v4"
"github.com/solo-io/gloo/pkg/utils/kubeutils/portforward"
"github.com/hashicorp/go-multierror"
errors "github.com/rotisserie/eris"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
)
const (
defaultTimeout = 30 * time.Second
)
var (
defaultRetryOptions = []retry.Option{
retry.LastErrorOnly(true),
retry.Delay(100 * time.Millisecond),
retry.DelayType(retry.BackOffDelay),
retry.Attempts(5),
}
)
// 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 {
resp.Body.Close()
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.GetRestConfigWithKubeContext("")
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 *corev1.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 {
pDurable := p
svcPort = &pDurable
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 = corev1.ServiceTypeNodePort
}
switch serviceType {
case corev1.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 corev1.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 corev1.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 *corev1.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.
// Deprecated: Prefer portforward.NewPortForwarder
func PortForward(ctx context.Context, namespace string, resource string, localPort string, kubePort string, verbose bool) (portforward.PortForwarder, error) {
err := Initialize()
if err != nil {
return nil, err
}
logger := GetLogger()
outWriter := logger
errWriter := io.MultiWriter(logger, os.Stderr)
if verbose {
outWriter = io.MultiWriter(logger, os.Stdout)
}
resourceTypeName := strings.Split(resource, "/") // ie. deployment/gloo
localPortInt, err := strconv.Atoi(localPort)
if err != nil {
return nil, err
}
remotePortInt, err := strconv.Atoi(kubePort)
if err != nil {
return nil, err
}
portForwarder := portforward.NewPortForwarder(
portforward.WithResource(resourceTypeName[1], namespace, resourceTypeName[0]),
portforward.WithPorts(localPortInt, remotePortInt),
portforward.WithWriters(outWriter, errWriter),
)
err = portForwarder.Start(ctx, defaultRetryOptions...)
if err != nil {
return nil, err
}
return portForwarder, nil
}
// PortForwardGet call kubectl port-forward and make a GET request.
// Callers are expected to clean up the returned portforward.PortForwarder after the port-forward is no longer needed.
// Deprecated: Prefer portforward.NewPortForwarder
func PortForwardGet(ctx context.Context, namespace string, resource string, localPort string, kubePort string, verbose bool, getPath string) (string, portforward.PortForwarder, error) {
/** port-forward command **/
portForwarder, err := PortForward(ctx, namespace, resource, localPort, kubePort, verbose)
if err != nil {
return "", nil, err
}
localCtx, cancel := context.WithTimeout(ctx, defaultTimeout)
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(fmt.Sprintf("http://%s/%s", portForwarder.Address(), strings.TrimPrefix(getPath, "/")))
if err != nil {
errs <- err
time.Sleep(retryInterval)
continue
}
if res.StatusCode != http.StatusOK {
errs <- errors.Errorf("invalid status code: %v %v", res.StatusCode, res.Status)
time.Sleep(retryInterval)
continue
}
b, err := io.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, portForwarder, nil
case <-localCtx.Done():
return "", portForwarder, 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
}