-
Notifications
You must be signed in to change notification settings - Fork 70
/
ingress.go
170 lines (154 loc) · 4.64 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
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
package expose_ingress
import (
"context"
"github.com/armory/spinnaker-operator/pkg/apis/spinnaker/interfaces"
"github.com/armory/spinnaker-operator/pkg/util"
"github.com/go-logr/logr"
"k8s.io/api/extensions/v1beta1"
v1beta12 "k8s.io/api/networking/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"net/url"
"sigs.k8s.io/controller-runtime/pkg/client"
"strconv"
)
type ingressExplorer struct {
log logr.Logger
client client.Client
scheme *runtime.Scheme
networkingIngresses []v1beta12.Ingress
extensionIngresses []v1beta1.Ingress
}
func (i *ingressExplorer) loadIngresses(ctx context.Context, ns string) error {
// Already loaded
if i.networkingIngresses != nil || i.extensionIngresses != nil {
return nil
}
var errNet, errExt error
g := wait.Group{}
networkingIngresses := &v1beta12.IngressList{}
extensionIngresses := &v1beta1.IngressList{}
if i.scheme.Recognizes(v1beta12.SchemeGroupVersion.WithKind("Ingress")) {
g.StartWithContext(ctx, func(ctx context.Context) {
errNet = i.client.List(ctx, networkingIngresses, client.InNamespace(ns))
})
}
if i.scheme.Recognizes(v1beta1.SchemeGroupVersion.WithKind("Ingress")) {
g.StartWithContext(ctx, func(ctx context.Context) {
errExt = i.client.List(ctx, extensionIngresses, client.InNamespace(ns))
})
}
g.Wait()
i.networkingIngresses = networkingIngresses.Items
i.extensionIngresses = extensionIngresses.Items
// Return either error
if errExt != nil {
return errExt
}
return errNet
}
func (i *ingressExplorer) getIngressUrl(serviceName string, servicePort int32) *url.URL {
if url := i.getExtensionIngressUrl(serviceName, servicePort); url != nil {
return url
}
return i.getNetworkingIngressUrl(serviceName, servicePort)
}
func (i *ingressExplorer) getExtensionIngressUrl(serviceName string, servicePort int32) *url.URL {
// Find the service name
for _, ing := range i.extensionIngresses {
for _, rule := range ing.Spec.Rules {
if rule.HTTP != nil {
for _, path := range rule.HTTP.Paths {
if path.Backend.ServiceName == serviceName {
// Are we referencing the service name?
if path.Backend.ServicePort.StrVal == "http" || path.Backend.ServicePort.IntVal == servicePort {
host := i.getActualExtensionHost(rule.Host, ing)
if host == "" {
return nil
}
return &url.URL{
Scheme: i.getSchemeFromExtensionIngress(ing, host),
Host: host,
Path: path.Path,
}
}
}
}
}
}
}
return nil
}
func (i *ingressExplorer) getActualExtensionHost(host string, ingress v1beta1.Ingress) string {
if host != "" {
return host
}
if len(ingress.Status.LoadBalancer.Ingress) == 0 {
return ""
}
// Take first host defined for the ingress
return ingress.Status.LoadBalancer.Ingress[0].Hostname
}
func (i *ingressExplorer) getNetworkingIngressUrl(serviceName string, servicePort int32) *url.URL {
// Find the service name
for _, ing := range i.networkingIngresses {
for _, rule := range ing.Spec.Rules {
if rule.HTTP != nil {
for _, path := range rule.HTTP.Paths {
if path.Backend.ServiceName == serviceName {
// Are we referencing the service name?
if path.Backend.ServicePort.StrVal == "http" || path.Backend.ServicePort.IntVal == servicePort {
host := i.getActualNetworkingHost(rule.Host, ing)
if host == "" {
return nil
}
return &url.URL{
Scheme: i.getSchemeFromNetworkingIngress(ing, host),
Host: host,
Path: path.Path,
}
}
}
}
}
}
}
return nil
}
func (i *ingressExplorer) getActualNetworkingHost(host string, ingress v1beta12.Ingress) string {
if host != "" {
return host
}
if len(ingress.Status.LoadBalancer.Ingress) == 0 {
return ""
}
return ingress.Status.LoadBalancer.Ingress[0].Hostname
}
func guessGatePort(ctx context.Context, svc interfaces.SpinnakerService) int32 {
if targetPort, _ := svc.GetSpinnakerConfig().GetServiceConfigPropString(ctx, "gate", "server.port"); targetPort != "" {
if intTargetPort, err := strconv.ParseInt(targetPort, 10, 32); err != nil {
return int32(intTargetPort)
}
}
return util.GateDefaultPort
}
func (i *ingressExplorer) getSchemeFromExtensionIngress(ingress v1beta1.Ingress, host string) string {
for _, tls := range ingress.Spec.TLS {
for _, h := range tls.Hosts {
if h == host {
return "https"
}
}
}
return "http"
}
func (i *ingressExplorer) getSchemeFromNetworkingIngress(ingress v1beta12.Ingress, host string) string {
for _, tls := range ingress.Spec.TLS {
for _, h := range tls.Hosts {
if h == host {
return "https"
}
}
}
return "http"
}