-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathingress.go
144 lines (120 loc) · 3.95 KB
/
ingress.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
package wrappers
import (
"net/url"
"strings"
"github.com/toboshii/hajimari/internal/annotations"
utilStrings "github.com/toboshii/hajimari/internal/util/strings"
v1 "k8s.io/api/networking/v1"
)
// IngressWrapper struct wraps a kubernetes ingress object
type IngressWrapper struct {
ingress *v1.Ingress
}
// NewIngressWrapper func creates an instance of IngressWrapper
func NewIngressWrapper(ingress *v1.Ingress) *IngressWrapper {
return &IngressWrapper{
ingress: ingress,
}
}
// GetAnnotationValue extracts an annotation's value present on the ingress wrapped by the object
func (iw *IngressWrapper) GetAnnotationValue(annotationKey string) string {
if value, ok := iw.ingress.Annotations[annotationKey]; ok {
return value
}
return ""
}
// GetName func extracts name of the ingress wrapped by the object
func (iw *IngressWrapper) GetName() string {
if nameFromAnnotation := iw.GetAnnotationValue(annotations.HajimariAppNameAnnotation); nameFromAnnotation != "" {
return nameFromAnnotation
}
return iw.ingress.ObjectMeta.Name
}
// GetNamespace func extracts namespace of the ingress wrapped by the object
func (iw *IngressWrapper) GetNamespace() string {
return iw.ingress.ObjectMeta.Namespace
}
// GetGroup func extracts group name from the ingress
func (iw *IngressWrapper) GetGroup() string {
if groupFromAnnotation := iw.GetAnnotationValue(annotations.HajimariGroupAnnotation); groupFromAnnotation != "" {
return groupFromAnnotation
}
return iw.GetNamespace()
}
// GetGroup func extracts group name from the ingress
func (iw *IngressWrapper) GetInfo() string {
if infoFromAnnotation := iw.GetAnnotationValue(annotations.HajimariInfoAnnotation); infoFromAnnotation != "" {
return infoFromAnnotation
}
return ""
}
// GetStatusCheckEnabled func extracts statusCheck feature gate from the ingress
// @default true
func (iw *IngressWrapper) GetStatusCheckEnabled() bool {
if statusCheckEnabledFromAnnotation := iw.GetAnnotationValue(annotations.HajimariStatusCheckEnabledAnnotation); statusCheckEnabledFromAnnotation != "" {
return utilStrings.ParseBool(statusCheckEnabledFromAnnotation)
}
return true
}
// GetTargetBlank func extracts open in new window feature gate from the ingress
// @default false
func (iw *IngressWrapper) GetTargetBlank() bool {
if targetBlankFromAnnotation := iw.GetAnnotationValue(annotations.HajimariTargetBlankAnnotation); targetBlankFromAnnotation != "" {
return utilStrings.ParseBool(targetBlankFromAnnotation)
}
return false
}
// GetURL func extracts url of the ingress wrapped by the object
func (iw *IngressWrapper) GetURL() string {
if urlFromAnnotation := iw.GetAnnotationValue(annotations.HajimariURLAnnotation); urlFromAnnotation != "" {
parsedURL, err := url.ParseRequestURI(urlFromAnnotation)
if err != nil {
logger.Warn(err)
return ""
}
return parsedURL.String()
}
if !iw.rulesExist() {
logger.Warn("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
}
// Append port + ingressSubPath
url += iw.getIngressSubPath()
return url
}
func (iw *IngressWrapper) rulesExist() bool {
if iw.ingress.Spec.Rules != nil && len(iw.ingress.Spec.Rules) > 0 {
return true
}
return false
}
func (iw *IngressWrapper) tryGetTLSHost() (string, bool) {
if iw.supportsTLS() {
return "https://" + iw.ingress.Spec.TLS[0].Hosts[0], true
}
return "", false
}
func (iw *IngressWrapper) supportsTLS() bool {
if iw.ingress.Spec.TLS != nil && len(iw.ingress.Spec.TLS) > 0 {
return true
}
return false
}
func (iw *IngressWrapper) getHost() string {
return "http://" + iw.ingress.Spec.Rules[0].Host
}
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 {
return strings.TrimRight(rule.HTTP.Paths[0].Path, "/")
}
}
return ""
}