-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
ingress.go
360 lines (302 loc) · 10.4 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
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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package ingestion
import (
"sort"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
"github.com/cilium/cilium/operator/pkg/ingress/annotations"
"github.com/cilium/cilium/operator/pkg/model"
"github.com/cilium/cilium/pkg/logging/logfields"
)
// Ingress translates an Ingress resource to a HTTPListener.
// This function does not check IngressClass (via field or annotation).
// It's expected that only relevant Ingresses will have this function called on them.
func Ingress(ing networkingv1.Ingress, defaultSecretNamespace, defaultSecretName string) []model.HTTPListener {
// First, we make a map of HTTPListeners, with the hostname
// as the key, so that we can make sure we match up any
// TLS config with rules that match it.
// This is to approximate a set, keyed by hostname, so we can
// coalesce the config from a single Ingress.
// Coalescing the config from multiple Ingress resources is left for
// the transform component that takes a model and outputs CiliumEnvoyConfig
// or other resources.
insecureListenerMap := make(map[string]model.HTTPListener)
sourceResource := model.FullyQualifiedResource{
Name: ing.Name,
Namespace: ing.Namespace,
Group: "",
Version: "v1",
Kind: "Ingress",
UID: string(ing.UID),
}
if ing.Spec.DefaultBackend != nil {
// There's a default backend set up
// get the details for the default backend
backend := model.Backend{}
backend.Name = ing.Spec.DefaultBackend.Service.Name
backend.Namespace = ing.Namespace
backend.Port = &model.BackendPort{}
if ing.Spec.DefaultBackend.Service.Port.Name != "" {
backend.Port.Name = ing.Spec.DefaultBackend.Service.Port.Name
}
if ing.Spec.DefaultBackend.Service.Port.Number != 0 {
backend.Port.Port = uint32(ing.Spec.DefaultBackend.Service.Port.Number)
}
l := model.HTTPListener{
Hostname: "*",
Routes: []model.HTTPRoute{
{
Backends: []model.Backend{
backend,
},
},
},
Port: 80,
Service: getService(ing),
}
l.Sources = model.AddSource(l.Sources, sourceResource)
insecureListenerMap["*"] = l
}
// Now, we range across the rules, adding them in as listeners.
for _, rule := range ing.Spec.Rules {
host := "*"
if rule.Host != "" {
host = rule.Host
}
l, ok := insecureListenerMap[host]
l.Port = 80
l.Sources = model.AddSource(l.Sources, sourceResource)
if !ok {
l.Name = "ing-" + ing.Name + "-" + ing.Namespace + "-" + host
}
l.Hostname = host
if rule.HTTP == nil {
log.WithField(logfields.Ingress, ing.Namespace+"/"+ing.Name).
Warn("Invalid Ingress rule without spec.rules.HTTP defined, skipping rule")
continue
}
for _, path := range rule.HTTP.Paths {
route := model.HTTPRoute{}
switch *path.PathType {
case networkingv1.PathTypeExact:
route.PathMatch.Exact = path.Path
case networkingv1.PathTypePrefix:
route.PathMatch.Prefix = path.Path
case networkingv1.PathTypeImplementationSpecific:
route.PathMatch.Regex = path.Path
}
backend := model.Backend{
Name: path.Backend.Service.Name,
Namespace: ing.Namespace,
}
if path.Backend.Service != nil {
backend.Port = &model.BackendPort{}
if path.Backend.Service.Port.Name != "" {
backend.Port.Name = path.Backend.Service.Port.Name
}
if path.Backend.Service.Port.Number != 0 {
backend.Port.Port = uint32(path.Backend.Service.Port.Number)
}
}
route.Backends = append(route.Backends, backend)
l.Routes = append(l.Routes, route)
l.Service = getService(ing)
}
insecureListenerMap[host] = l
}
secureListenerMap := make(map[string]model.HTTPListener)
// First, we check for TLS config, and set them up with Listeners to return.
for _, tlsConfig := range ing.Spec.TLS {
for _, host := range tlsConfig.Hosts {
l, ok := secureListenerMap[host]
if !ok {
l, ok = insecureListenerMap[host]
if !ok {
l, ok = insecureListenerMap["*"]
if !ok {
continue
}
}
}
if tlsConfig.SecretName != "" {
l.TLS = []model.TLSSecret{
{
Name: tlsConfig.SecretName,
// Secret has to be in the same namespace as the Ingress.
Namespace: ing.Namespace,
},
}
} else if defaultSecretNamespace != "" && defaultSecretName != "" {
l.TLS = []model.TLSSecret{
{
Name: defaultSecretName,
Namespace: defaultSecretNamespace,
},
}
}
l.Port = 443
l.Hostname = host
l.Service = getService(ing)
secureListenerMap[host] = l
defaultListener, ok := insecureListenerMap["*"]
if ok {
// A default listener already exists, each Host in TLSConfig.Hosts
// needs to have a Listener configured that's a copy of it.
if tlsConfig.SecretName != "" {
defaultListener.TLS = []model.TLSSecret{
{
Name: tlsConfig.SecretName,
// Secret has to be in the same namespace as the Ingress.
Namespace: ing.Namespace,
},
}
} else if defaultSecretNamespace != "" && defaultSecretName != "" {
defaultListener.TLS = []model.TLSSecret{
{
Name: defaultSecretName,
Namespace: defaultSecretNamespace,
},
}
}
defaultListener.Hostname = host
defaultListener.Port = 443
secureListenerMap[host] = defaultListener
}
}
}
listenerSlice := make([]model.HTTPListener, 0, len(insecureListenerMap)+len(secureListenerMap))
listenerSlice = appendValuesInKeyOrder(insecureListenerMap, listenerSlice)
listenerSlice = appendValuesInKeyOrder(secureListenerMap, listenerSlice)
return listenerSlice
}
// IngressPassthrough translates an Ingress resource with the tls-passthrough annotation to a TLSListener.
// This function does not check IngressClass (via field or annotation).
// It's expected that only relevant Ingresses will have this function called on them.
//
// Ingress objects with SSL Passthrough enabled have the following properties:
//
// * must have a host set
// * rules with paths other than '/' are ignored
// * default backends are ignored
func IngressPassthrough(ing networkingv1.Ingress, defaultSecretNamespace, defaultSecretName string) []model.TLSListener {
// First, we make a map of TLSListeners, with the hostname
// as the key, so that we can make sure we match up any
// TLS config with rules that match it.
// This is to approximate a set, keyed by hostname, so we can
// coalesce the config from a single Ingress.
// Coalescing the config from multiple Ingress resources is left for
// the transform component that takes a model and outputs CiliumEnvoyConfig
// or other resources.
tlsListenerMap := make(map[string]model.TLSListener)
sourceResource := model.FullyQualifiedResource{
Name: ing.Name,
Namespace: ing.Namespace,
Group: "",
Version: "v1",
Kind: "Ingress",
UID: string(ing.UID),
}
// Note that there's no support for default backends in SSL Passthrough
// mode.
if ing.Spec.DefaultBackend != nil {
log.WithField(logfields.Ingress, ing.Namespace+"/"+ing.Name).
Warn("Invalid SSL Passthrough Ingress rule with a default backend, skipping default backend config")
}
// Now, we range across the rules, adding them in as listeners.
for _, rule := range ing.Spec.Rules {
// SSL Passthrough Ingress objects must have a host set.
if rule.Host == "" {
log.WithField(logfields.Ingress, ing.Namespace+"/"+ing.Name).
Warn("Invalid SSL Passthrough Ingress rule without spec.rules.host defined, skipping rule")
continue
}
host := rule.Host
l, ok := tlsListenerMap[host]
l.Port = 443
l.Sources = model.AddSource(l.Sources, sourceResource)
if !ok {
l.Name = "ing-" + ing.Name + "-" + ing.Namespace + "-" + host
}
l.Hostname = host
if rule.HTTP == nil {
log.WithField(logfields.Ingress, ing.Namespace+"/"+ing.Name).
Warn("Invalid SSL Passthrough Ingress rule without spec.rules.HTTP defined, skipping rule")
continue
}
for _, path := range rule.HTTP.Paths {
// SSL Passthrough objects must only have path of '/'
if path.Path != "/" {
log.WithField(logfields.Ingress, ing.Namespace+"/"+ing.Name).
Warn("Invalid SSL Passthrough Ingress rule with path not equal to '/', skipping rule")
continue
}
route := model.TLSRoute{
Hostnames: []string{
host,
},
}
backend := model.Backend{
Name: path.Backend.Service.Name,
Namespace: ing.Namespace,
}
if path.Backend.Service != nil {
backend.Port = &model.BackendPort{}
if path.Backend.Service.Port.Name != "" {
backend.Port.Name = path.Backend.Service.Port.Name
}
if path.Backend.Service.Port.Number != 0 {
backend.Port.Port = uint32(path.Backend.Service.Port.Number)
}
}
route.Backends = append(route.Backends, backend)
l.Routes = append(l.Routes, route)
l.Service = getService(ing)
}
// If there aren't any routes, then don't add the Listener
if len(l.Routes) == 0 {
log.WithField(logfields.Ingress, ing.Namespace+"/"+ing.Name).
Warn("Invalid SSL Passthrough Ingress with no valid rules, skipping")
continue
}
tlsListenerMap[host] = l
}
listenerSlice := make([]model.TLSListener, 0, len(tlsListenerMap))
listenerSlice = appendValuesInKeyOrder(tlsListenerMap, listenerSlice)
return listenerSlice
}
func getService(ing networkingv1.Ingress) *model.Service {
if annotations.GetAnnotationServiceType(&ing) != string(corev1.ServiceTypeNodePort) {
return nil
}
m := &model.Service{
Type: string(corev1.ServiceTypeNodePort),
}
scopedLog := log.WithField(logfields.Ingress, ing.Namespace+"/"+ing.Name)
secureNodePort, err := annotations.GetAnnotationSecureNodePort(&ing)
if err != nil {
scopedLog.WithError(err).Warn("Invalid secure node port annotation, random port will be used")
} else {
m.SecureNodePort = secureNodePort
}
insureNodePort, err := annotations.GetAnnotationInsecureNodePort(&ing)
if err != nil {
scopedLog.WithError(err).Warn("Invalid insecure node port annotation, random port will be used")
} else {
m.InsecureNodePort = insureNodePort
}
return m
}
// appendValuesInKeyOrder ensures that the slice of listeners is stably sorted by
// appending the values of the map in order of the keys to the appendSlice.
func appendValuesInKeyOrder[T model.HTTPListener | model.TLSListener](listenerMap map[string]T, appendSlice []T) []T {
var keys []string
for key := range listenerMap {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
appendSlice = append(appendSlice, listenerMap[key])
}
return appendSlice
}