forked from stakater/IngressMonitorController
/
routeWrapper.go
149 lines (117 loc) · 3.52 KB
/
routeWrapper.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
package wrappers
import (
"log"
"net/url"
"path"
routev1 "github.com/openshift/api/route/v1"
"github.com/stakater/IngressMonitorController/pkg/constants"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
)
type RouteWrapper struct {
Route *routev1.Route
Namespace string
KubeClient kubernetes.Interface
}
func (rw *RouteWrapper) supportsTLS() bool {
if rw.Route.Spec.TLS != nil {
return true
}
return false
}
func (rw *RouteWrapper) tryGetTLSHost() (string, bool) {
if rw.supportsTLS() {
return "https://" + rw.Route.Spec.Host, true
}
annotations := rw.Route.GetAnnotations()
if value, ok := annotations[constants.ForceHTTPSAnnotation]; ok {
if value == "true" {
// Annotation exists and is enabled
return "https://" + rw.Route.Spec.Host, true
}
}
return "", false
}
func (rw *RouteWrapper) getHost() string {
return "http://" + rw.Route.Spec.Host
}
func (rw *RouteWrapper) getRouteSubPathWithPort() string {
port := rw.getRoutePort()
subPath := rw.getRouteSubPath()
return port + subPath
}
func (rw *RouteWrapper) getRoutePort() string {
if rw.Route.Spec.Port != nil && rw.Route.Spec.Port.TargetPort.String() != "" {
return rw.Route.Spec.Port.TargetPort.String()
}
return ""
}
func (rw *RouteWrapper) getRouteSubPath() string {
return rw.Route.Spec.Path
}
func (rw *RouteWrapper) hasService() (string, bool) {
if rw.Route.Spec.To.Name != "" {
return rw.Route.Spec.To.Name, true
}
return "", false
}
func (rw *RouteWrapper) tryGetHealthEndpointFromRoute() (string, bool) {
serviceName, exists := rw.hasService()
if !exists {
return "", false
}
service, err := rw.KubeClient.Core().Services(rw.Route.Namespace).Get(serviceName, meta_v1.GetOptions{})
if err != nil {
log.Printf("Get service from kubernetes cluster error:%v", err)
return "", false
}
set := labels.Set(service.Spec.Selector)
if pods, err := rw.KubeClient.Core().Pods(rw.Route.Namespace).List(meta_v1.ListOptions{LabelSelector: set.AsSelector().String()}); err != nil {
log.Printf("List Pods of service[%s] error:%v", service.GetName(), err)
} else if len(pods.Items) > 0 {
pod := pods.Items[0]
podContainers := pod.Spec.Containers
if len(podContainers) == 1 {
if podContainers[0].ReadinessProbe != nil && podContainers[0].ReadinessProbe.HTTPGet != nil {
return podContainers[0].ReadinessProbe.HTTPGet.Path, true
}
} else {
log.Printf("Pod has %d containers so skipping health endpoint", len(podContainers))
}
}
return "", false
}
func (rw *RouteWrapper) GetURL() string {
var URL string
if host, exists := rw.tryGetTLSHost(); exists { // Get TLS Host if it exists
URL = host
} else {
URL = rw.getHost() // Fallback for normal Host
}
// Convert url to url object
u, err := url.Parse(URL)
if err != nil {
log.Printf("URL parsing error in getURL() :%v", err)
return ""
}
annotations := rw.Route.GetAnnotations()
if value, ok := annotations[constants.OverridePathAnnotation]; ok {
u.Path = value
} else {
// Append port + path
u.Path = path.Join(u.Path, rw.getRouteSubPathWithPort())
// Find pod by backtracking route -> service -> pod
healthEndpoint, exists := rw.tryGetHealthEndpointFromRoute()
// Health endpoint from pod successful
if exists {
u.Path = path.Join(u.Path, healthEndpoint)
} else { // Try to get annotation and set
// Annotation for health Endpoint exists
if value, ok := annotations[constants.MonitorHealthAnnotation]; ok {
u.Path = path.Join(u.Path, value)
}
}
}
return u.String()
}