forked from k8sgateway/k8sgateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uri.go
155 lines (138 loc) · 4.07 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
package cliutil
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/solo-io/gloo/projects/gloo/cli/pkg/cmd/options"
"github.com/solo-io/go-utils/kubeutils"
"github.com/solo-io/solo-kit/pkg/errors"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
)
// Get the resource identified by the given URI.
// The URI can either be an 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", resp.StatusCode)
}
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
}
func GetIngressHost(opts *options.Proxy, namespace 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(namespace).Get(opts.Name, 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'",
opts.Name, namespace)
}
var svcPort *v1.ServicePort
switch len(svc.Spec.Ports) {
case 0:
return "", errors.Errorf("service %v is missing ports", opts.Name)
case 1:
svcPort = &svc.Spec.Ports[0]
default:
for _, p := range svc.Spec.Ports {
if p.Name == opts.Port {
svcPort = &p
break
}
}
if svcPort == nil {
return "", errors.Errorf("named port %v not found on service %v", opts.Port, opts.Name)
}
}
var host, port string
// gateway-proxy is an externally load-balanced service
if len(svc.Status.LoadBalancer.Ingress) == 0 || opts.LocalCluster {
// assume nodeport on kubernetes
// TODO: support more types of NodePort services
host, err = getNodeIp(svc, kube)
if err != nil {
return "", errors.Wrapf(err, "")
}
port = fmt.Sprintf("%v", svcPort.NodePort)
} else {
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(svc *v1.Service, kube kubernetes.Interface) (string, error) {
// pick a node where one of our pods is running
pods, err := kube.CoreV1().Pods(svc.Namespace).List(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()
}
node, err := kube.CoreV1().Nodes().Get(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() (string, error) {
minikubeCmd := exec.Command("minikube", "ip")
hostname := &bytes.Buffer{}
minikubeCmd.Stdout = hostname
minikubeCmd.Stderr = os.Stderr
err := minikubeCmd.Run()
return strings.TrimSuffix(hostname.String(), "\n"), err
}