-
Notifications
You must be signed in to change notification settings - Fork 101
/
ingress-wrapper.go
198 lines (164 loc) · 4.92 KB
/
ingress-wrapper.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
package wrappers
import (
"log"
"net/url"
"path"
"reflect"
"strings"
"github.com/stakater/IngressMonitorController/pkg/constants"
"k8s.io/api/extensions/v1beta1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
)
type IngressWrapper struct {
Ingress *v1beta1.Ingress
Namespace string
KubeClient kubernetes.Interface
}
func (iw *IngressWrapper) supportsTLS() bool {
if iw.Ingress.Spec.TLS != nil && len(iw.Ingress.Spec.TLS) > 0 {
return true
}
return false
}
func (iw *IngressWrapper) tryGetTLSHost() (string, bool) {
if iw.supportsTLS() {
return "https://" + iw.Ingress.Spec.TLS[0].Hosts[0], true
}
annotations := iw.Ingress.GetAnnotations()
if value, ok := annotations[constants.ForceHTTPSAnnotation]; ok {
if value == "true" {
// Annotation exists and is enabled
return "https://" + iw.Ingress.Spec.Rules[0].Host, true
}
}
return "", false
}
func (iw *IngressWrapper) getHost() string {
return "http://" + iw.Ingress.Spec.Rules[0].Host
}
func (iw *IngressWrapper) rulesExist() bool {
if iw.Ingress.Spec.Rules != nil && len(iw.Ingress.Spec.Rules) > 0 {
return true
}
return false
}
func (iw *IngressWrapper) getIngressSubPathWithPort() string {
port := iw.getIngressPort()
subPath := iw.getIngressSubPath()
if port != nil {
if subPath != nil {
return port + subPath
} else {
return port + "/"
}
} else {
if subPath != nil {
return "80" + subPath
} else {
return "80/"
}
}
}
func (iw *IngressWrapper) getIngressPort() string {
rule := iw.Ingress.Spec.Rules[0]
if rule.HTTP != nil {
if rule.HTTP.Paths != nil && len(rule.HTTP.Paths) > 0 {
if reflect.TypeOf(rule.HTTP.Paths[0].Backend.ServicePort).Kind() == reflect.String {
return "80"
} else {
return rule.HTTP.Paths[0].Backend.ServicePort.StrVal
}
}
}
return ""
}
func (iw *IngressWrapper) getIngressSubPath() string {
rule := iw.Ingress.Spec.Rules[0]
if rule.HTTP != nil {
if rule.HTTP.Paths != nil && len(rule.HTTP.Paths) > 0 {
if strings.ContainsAny(rule.HTTP.Paths[0].Path, "*") {
return strings.TrimRight(rule.HTTP.Paths[0].Path, "*")
} else {
return rule.HTTP.Paths[0].Path
}
}
}
return ""
}
func (iw *IngressWrapper) GetURL() string {
if !iw.rulesExist() {
log.Println("No rules exist in ingress: " + iw.Ingress.GetName())
return ""
}
var URL string
if host, exists := iw.tryGetTLSHost(); exists { // Get TLS Host if it exists
URL = host
} else {
URL = iw.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 := iw.Ingress.GetAnnotations()
if value, ok := annotations[constants.OverridePathAnnotation]; ok {
u.Path = value
} else {
// Append port + ingressSubPath
ingressSubPathWithPort := iw.getIngressSubPathWithPort()
u.Path = strings.TripSuffix(u.Path, "/") + ":" + strings.Split(ingressSubPathWithPort, "/")[0]
u.Path = path.Join(u.Path, strings.Join(strings.Split(iw.getIngressSubPathWithPort(), "/")[1:], "/"))
// Find pod by backtracking ingress -> service -> pod
healthEndpoint, exists := iw.tryGetHealthEndpointFromIngress()
// 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()
}
func (iw *IngressWrapper) hasService() (string, bool) {
ingress := iw.Ingress
if ingress.Spec.Rules[0].HTTP != nil &&
ingress.Spec.Rules[0].HTTP.Paths != nil &&
len(ingress.Spec.Rules[0].HTTP.Paths) > 0 &&
ingress.Spec.Rules[0].HTTP.Paths[0].Backend.ServiceName != "" {
return ingress.Spec.Rules[0].HTTP.Paths[0].Backend.ServiceName, true
}
return "", false
}
func (iw *IngressWrapper) tryGetHealthEndpointFromIngress() (string, bool) {
serviceName, exists := iw.hasService()
if !exists {
return "", false
}
service, err := iw.KubeClient.Core().Services(iw.Ingress.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 := iw.KubeClient.Core().Pods(iw.Ingress.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
}