-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubernetes.go
371 lines (349 loc) · 11 KB
/
kubernetes.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package netkat
import (
"bytes"
"errors"
"fmt"
"github.com/go-kit/kit/log/level"
"k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
"net"
"net/http"
"net/url"
"strings"
"sync"
)
type (
PodPort struct {
PodName string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
App string
ContainerImage string `json:"image,omitempty"`
ContainerName string `json:"name,omitempty"`
PortName string `json:"name,omitempty"`
HostPort int32 `json:"hostPort,omitempty"`
ContainerPort int32 `json:"containerPort,omitempty"`
Protocol string `json:"protocol,omitempty"`
HostIP net.IP `json:"hostIP,omitempty"`
ServicePort ServicePort
PodStatus string `json:"status,omitempty"`
}
ServicePort struct {
Type string `json:"type,omitempty"`
ClusterIP net.IP `json:"clusterIP,omitempty"`
ServiceName string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
ExternalIP net.IP
AppSelector string
Host string
SourcePortName string `json:"name,omitempty"`
Protocol string `json:"protocol,omitempty"`
SourcePort int32 `json:"port,omitempty"`
NodePort int32 `json:"nodePort,omitempty"`
TargetPort int32 `json:"targetPort,omitempty"`
TargetPortName string `json:"targetPort,omitempty"`
IngressPath IngressPath
PodPort []*PodPort
}
IngressPath struct {
Host string `json:"host,omitempty"`
IpAddress net.IP `json:"ipAddress,omitempty"`
Namespace string `json:"namespace,omitempty"`
IngressName string `json:"name,omitempty"`
Path string `json:"path,omitempty"`
ServiceName string `json:"serviceName,omitempty"`
ServiceIntPort int32 `json:"servicePort,omitempty"`
ServiceStrPort string `json:"servicePort,omitempty"`
Service []*ServicePort
}
KubernetesComponents struct {
IngressPaths []*IngressPath
ServicePorts []*ServicePort
PodPorts []*PodPort
}
)
func (co *KubernetesComponents) FindIngressPathForHost(t *Target) (ingressPath *IngressPath, err error) {
var ingressPaths []*IngressPath
for _, i := range co.IngressPaths {
if t.Host == i.Host && t.Path == i.Path && t.IpAddress.Equal(i.IpAddress) {
ingressPaths = append(ingressPaths, i)
}
}
switch {
case len(ingressPaths) > 1:
err = errors.New("found more than one ingress resource matching the host")
case len(ingressPaths) == 0:
err = errors.New("could not find ingress resource matching the host")
default:
ingressPath = ingressPaths[0]
}
return
}
func (co *KubernetesComponents) FindServicePortForHost(t *Target) (servicePort *ServicePort, err error) {
var servicePorts []*ServicePort
for _, s := range co.ServicePorts {
if t.Host == s.Host && t.Port == s.SourcePort && t.IpAddress.Equal(s.ExternalIP) {
servicePorts = append(servicePorts, s)
}
}
switch {
case len(servicePorts) > 1:
err = errors.New("found more than one service resource matching the host")
case len(servicePorts) == 0:
return
default:
servicePort = servicePorts[0]
}
return
}
func (co *KubernetesComponents) FindServicePortForIngressPath(i *IngressPath) (servicePort *ServicePort, err error) {
var servicePorts []*ServicePort
for _, s := range co.ServicePorts {
if i.Namespace == s.Namespace && i.ServiceName == s.ServiceName && (i.ServiceIntPort == s.SourcePort || i.ServiceStrPort == s.SourcePortName) {
servicePorts = append(servicePorts, s)
}
}
switch {
case len(servicePorts) > 1:
err = errors.New("found more than one service resource matching the ingress path")
case len(servicePorts) == 0:
err = errors.New("could not find service resource matching the ingress path")
default:
servicePort = servicePorts[0]
}
return
}
func (co *KubernetesComponents) FindPodPortForServicePort(s *ServicePort) (podPorts []*PodPort, err error) {
for _, p := range co.PodPorts {
if s.Namespace == p.Namespace && s.AppSelector == p.App && (s.TargetPort == p.ContainerPort || s.TargetPortName == p.PortName) {
podPorts = append(podPorts, p)
}
}
if len(podPorts) == 0 {
err = errors.New("could not find pod port matching the service port")
}
return
}
func (co *KubernetesComponents) FindPodPort(n string) (podPort *PodPort, err error) {
for _, p := range co.PodPorts {
if n == p.PodName {
podPort = p
return
}
}
err = errors.New("could not find pod port matching the specified pod name")
return
}
func (co *KubernetesComponents) FindServicePortForPodPort(p *PodPort) (servicePort *ServicePort, err error) {
var servicePorts []*ServicePort
for _, s := range co.ServicePorts {
if p.Namespace == s.Namespace && p.App == s.AppSelector && (p.ContainerPort == s.TargetPort || p.PortName == s.TargetPortName) {
servicePorts = append(servicePorts, s)
}
}
switch {
case len(servicePorts) > 1:
err = errors.New("found more than one service resource matching the pod port")
case len(servicePorts) == 0:
err = errors.New("could not find service resource matching the pod port")
default:
servicePort = servicePorts[0]
}
return
}
func (co *KubernetesComponents) FindIngressPathForServicePort(s *ServicePort) (ingressPath *IngressPath, err error) {
var ingressPaths []*IngressPath
for _, i := range co.IngressPaths {
if i.Namespace == s.Namespace && i.ServiceName == s.ServiceName && (i.ServiceIntPort == s.SourcePort || i.ServiceStrPort == s.SourcePortName) {
ingressPaths = append(ingressPaths, i)
}
}
switch {
case len(ingressPaths) > 1:
err = errors.New("found more than one ingress resource matching the service")
case len(ingressPaths) == 0:
err = errors.New("could not find ingress resource matching the service")
default:
ingressPath = ingressPaths[0]
}
return
}
func (c *Client) GetComponents() (components *KubernetesComponents) {
pods := c.GetPods()
svcs := c.GetServices()
ings := c.GetIngresses()
components = &KubernetesComponents{
IngressesToIngressPaths(ings),
ServicesToServicePorts(svcs),
PodsToPodPorts(pods),
}
return
}
func (c *Client) GetPods() (apiPods *v1.PodList) {
apiPods, err := c.CoreV1().Pods("").List(metav1.ListOptions{})
if err != nil {
_ = level.Error(Logger).Log("msg", err)
}
return
}
func PodsToPodPorts(apiPods *v1.PodList) (podPorts []*PodPort) {
for _, pod := range apiPods.Items {
for _, container := range pod.Spec.Containers {
for _, port := range container.Ports {
appLabel, ok := pod.ObjectMeta.Labels["app"]
if !ok {
appLabel = ""
}
podPorts = append(
podPorts,
&PodPort{
PortName: port.Name,
HostPort: port.HostPort,
ContainerPort: port.ContainerPort,
Protocol: string(port.Protocol),
HostIP: net.ParseIP(port.HostIP),
ContainerName: container.Name,
ContainerImage: container.Image,
PodName: pod.ObjectMeta.Name,
Namespace: pod.ObjectMeta.Namespace,
App: appLabel,
PodStatus: string(pod.Status.Phase),
},
)
}
}
}
return
}
func (c *Client) GetServices() (apiServices *v1.ServiceList) {
apiServices, err := c.CoreV1().Services("").List(metav1.ListOptions{})
if err != nil {
_ = level.Error(Logger).Log("msg", err)
}
return
}
func ServicesToServicePorts(apiServices *v1.ServiceList) (servicePorts []*ServicePort) {
for _, service := range apiServices.Items {
for _, port := range service.Spec.Ports {
hostName, ok := service.ObjectMeta.Annotations["external-dns.alpha.kubernetes.io/hostname"]
if !ok {
hostName = ""
}
var ip net.IP
if len(service.Status.LoadBalancer.Ingress) > 0 {
ip = net.ParseIP(service.Status.LoadBalancer.Ingress[0].IP)
}
appSelector, ok := service.Spec.Selector["app"]
if !ok {
appSelector = ""
}
var targetIntPort int32
if port.TargetPort.IntVal == 0 && port.TargetPort.StrVal == "" {
targetIntPort = port.Port
} else {
targetIntPort = port.TargetPort.IntVal
}
servicePorts = append(
servicePorts,
&ServicePort{
ServiceName: service.ObjectMeta.Name,
AppSelector: appSelector,
Type: string(service.Spec.Type),
ClusterIP: net.ParseIP(service.Spec.ClusterIP),
ExternalIP: ip,
Host: hostName,
Namespace: service.ObjectMeta.Namespace,
Protocol: string(port.Protocol),
SourcePortName: port.Name,
SourcePort: port.Port,
NodePort: port.NodePort,
TargetPort: targetIntPort,
TargetPortName: port.TargetPort.StrVal,
},
)
}
}
return
}
func (c *Client) GetIngresses() (apiIngresses *v1beta1.IngressList) {
apiIngresses, err := c.ExtensionsV1beta1().Ingresses("").List(metav1.ListOptions{})
if err != nil {
_ = level.Error(Logger).Log("msg", err)
}
return
}
func IngressesToIngressPaths(apiIngresses *v1beta1.IngressList) (ingressPaths []*IngressPath) {
for _, ingressResource := range apiIngresses.Items {
for _, ingress := range ingressResource.Spec.Rules {
for _, path := range ingress.IngressRuleValue.HTTP.Paths {
ingressPaths = append(
ingressPaths,
&IngressPath{
Path: path.Path,
ServiceName: path.Backend.ServiceName,
ServiceIntPort: path.Backend.ServicePort.IntVal,
ServiceStrPort: path.Backend.ServicePort.StrVal,
IngressName: ingressResource.ObjectMeta.Name,
IpAddress: net.ParseIP(ingressResource.Status.LoadBalancer.Ingress[0].IP),
Namespace: ingressResource.ObjectMeta.Namespace,
Host: ingress.Host,
},
)
}
}
}
return
}
func (c *Client) IsPodListening(p *PodPort) (result bool) {
roundTripper, upgrader, err := spdy.RoundTripperFor(c.Config)
if err != nil {
_ = level.Error(Logger).Log("msg", err)
return
}
path := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", p.Namespace, p.PodName)
hostIP := strings.TrimLeft(c.Config.Host, "htps:/")
serverURL := url.URL{Scheme: "https", Path: path, Host: hostIP}
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: roundTripper}, http.MethodPost, &serverURL)
stopChan, readyChan := make(chan struct{}, 1), make(chan struct{}, 1)
out, errOut := new(bytes.Buffer), new(bytes.Buffer)
forwarder, err := portforward.New(dialer, []string{fmt.Sprintf("%v", p.ContainerPort)}, stopChan, readyChan, out, errOut)
if err != nil {
_ = level.Error(Logger).Log("msg", err)
return
}
wait := sync.WaitGroup{}
wait.Add(1)
go func() {
go func() {
for range readyChan {
}
if len(errOut.String()) != 0 {
_ = level.Error(Logger).Log("msg", errOut.String())
fmt.Println(errOut.String())
wait.Done()
return
} else if len(out.String()) != 0 {
fmt.Println(out.String())
}
wait.Done()
}()
if err = forwarder.ForwardPorts(); err != nil {
_ = level.Error(Logger).Log("msg", err)
wait.Done()
return
}
}()
wait.Wait()
pfUrl := fmt.Sprintf("http://127.0.0.1:%v", p.ContainerPort)
_, err = http.Get(pfUrl)
close(stopChan)
if err != nil {
_ = level.Error(Logger).Log("msg", err)
result = false
} else {
result = true
}
return
}