/
ingress.go
783 lines (708 loc) · 24 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
package kubernetes
import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/dataclients/kubernetes/definitions"
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/predicates/primitive"
"github.com/zalando/skipper/predicates/traffic"
)
const (
ingressRouteIDPrefix = "kube"
backendWeightsAnnotationKey = "zalando.org/backend-weights"
ratelimitAnnotationKey = "zalando.org/ratelimit"
skipperfilterAnnotationKey = "zalando.org/skipper-filter"
skipperpredicateAnnotationKey = "zalando.org/skipper-predicate"
skipperRoutesAnnotationKey = "zalando.org/skipper-routes"
skipperLoadBalancerAnnotationKey = "zalando.org/skipper-loadbalancer"
skipperBackendProtocolAnnotationKey = "zalando.org/skipper-backend-protocol"
pathModeAnnotationKey = "zalando.org/skipper-ingress-path-mode"
ingressOriginName = "ingress"
)
type ingressContext struct {
state *clusterState
ingress *definitions.IngressItem
logger *log.Entry
annotationFilters []*eskip.Filter
annotationPredicate string
extraRoutes []*eskip.Route
backendWeights map[string]float64
pathMode PathMode
redirect *redirectInfo
hostRoutes map[string][]*eskip.Route
defaultFilters defaultFilters
}
type ingress struct {
provideHTTPSRedirect bool
httpsRedirectCode int
pathMode PathMode
kubernetesEnableEastWest bool
eastWestDomainRegexpPostfix string
eastWestRangeDomains []string
eastWestRangePredicates []*eskip.Predicate
}
var nonWord = regexp.MustCompile(`\W`)
func (ic *ingressContext) addHostRoute(host string, route *eskip.Route) {
ic.hostRoutes[host] = append(ic.hostRoutes[host], route)
}
func newIngress(o Options) *ingress {
var ewPostfix string
if o.KubernetesEnableEastWest {
ewPostfix = "[.]" + strings.Replace(o.KubernetesEastWestDomain, ".", "[.]", -1)
}
return &ingress{
provideHTTPSRedirect: o.ProvideHTTPSRedirect,
httpsRedirectCode: o.HTTPSRedirectCode,
pathMode: o.PathMode,
kubernetesEnableEastWest: o.KubernetesEnableEastWest,
eastWestDomainRegexpPostfix: ewPostfix,
eastWestRangeDomains: o.KubernetesEastWestRangeDomains,
eastWestRangePredicates: o.KubernetesEastWestRangePredicates,
}
}
func getLoadBalancerAlgorithm(m *definitions.Metadata) string {
algorithm := defaultLoadBalancerAlgorithm
if algorithmAnnotationValue, ok := m.Annotations[skipperLoadBalancerAnnotationKey]; ok {
algorithm = algorithmAnnotationValue
}
return algorithm
}
// TODO: find a nicer way to autogenerate route IDs
func routeID(namespace, name, host, path, backend string) string {
namespace = nonWord.ReplaceAllString(namespace, "_")
name = nonWord.ReplaceAllString(name, "_")
host = nonWord.ReplaceAllString(host, "_")
path = nonWord.ReplaceAllString(path, "_")
backend = nonWord.ReplaceAllString(backend, "_")
return fmt.Sprintf("%s_%s__%s__%s__%s__%s", ingressRouteIDPrefix, namespace, name, host, path, backend)
}
// routeIDForCustom generates a route id for a custom route of an ingress
// resource.
func routeIDForCustom(namespace, name, id, host string, index int) string {
name = name + "_" + id + "_" + strconv.Itoa(index)
return routeID(namespace, name, host, "", "")
}
func setPath(m PathMode, r *eskip.Route, p string) {
if p == "" {
return
}
switch m {
case PathPrefix:
r.Predicates = append(r.Predicates, &eskip.Predicate{
Name: "PathSubtree",
Args: []interface{}{p},
})
case PathRegexp:
r.PathRegexps = []string{p}
default:
if p == "/" {
r.PathRegexps = []string{"^/"}
} else {
r.PathRegexps = []string{"^(" + p + ")"}
}
}
}
func convertPathRule(
state *clusterState,
metadata *definitions.Metadata,
host string,
prule *definitions.PathRule,
pathMode PathMode,
) (*eskip.Route, error) {
ns := metadata.Namespace
name := metadata.Name
if prule.Backend == nil {
return nil, fmt.Errorf("invalid path rule, missing backend in: %s/%s/%s", ns, name, host)
}
var (
eps []string
err error
svc *service
)
var hostRegexp []string
if host != "" {
hostRegexp = []string{"^" + strings.Replace(host, ".", "[.]", -1) + "$"}
}
svcPort := prule.Backend.ServicePort
svcName := prule.Backend.ServiceName
svc, err = state.getService(ns, svcName)
if err != nil {
log.Errorf("convertPathRule: Failed to get service %s, %s, %s", ns, svcName, svcPort)
return nil, err
}
servicePort, err := svc.getServicePort(svcPort)
if err != nil {
// service definition is wrong or no pods
err = nil
if len(eps) > 0 {
// should never happen
log.Errorf("convertPathRule: Failed to find target port for service %s, but %d endpoints exist. Kubernetes has inconsistent data", svcName, len(eps))
}
} else if svc.Spec.Type == "ExternalName" {
scheme := "https"
if n, _ := servicePort.TargetPort.Number(); n != 443 {
scheme = "http"
}
u := fmt.Sprintf("%s://%s:%s", scheme, svc.Spec.ExternalName, servicePort.TargetPort)
f, e := eskip.ParseFilters(fmt.Sprintf(`setRequestHeader("Host", "%s")`, svc.Spec.ExternalName))
if e != nil {
return nil, e
}
return &eskip.Route{
Id: routeID(ns, name, host, "", svc.Spec.ExternalName),
BackendType: eskip.NetworkBackend,
Backend: u,
Filters: f,
HostRegexps: hostRegexp,
}, nil
} else {
protocol := "http"
if p, ok := metadata.Annotations[skipperBackendProtocolAnnotationKey]; ok {
protocol = p
}
eps = state.getEndpointsByService(ns, svcName, protocol, servicePort)
log.Debugf("convertPathRule: Found %d endpoints %s for %s", len(eps), servicePort, svcName)
}
if len(eps) == 0 {
// add shunt route https://github.com/zalando/skipper/issues/1525
log.Debugf("convertPathRule: add shuntroute to return 502 for ingress %s/%s service %s with %d endpoints", ns, name, svcName, len(eps))
r := &eskip.Route{
Id: routeID(ns, name, host, prule.Path, svcName),
HostRegexps: hostRegexp,
}
setPath(pathMode, r, prule.Path)
setTraffic(r, svcName, prule.Backend.Traffic, prule.Backend.NoopCount)
shuntRoute(r)
return r, nil
}
log.Debugf("convertPathRule: %d routes for %s/%s/%s", len(eps), ns, svcName, svcPort)
if len(eps) == 1 {
r := &eskip.Route{
Id: routeID(ns, name, host, prule.Path, svcName),
Backend: eps[0],
HostRegexps: hostRegexp,
}
setPath(pathMode, r, prule.Path)
setTraffic(r, svcName, prule.Backend.Traffic, prule.Backend.NoopCount)
return r, nil
}
r := &eskip.Route{
Id: routeID(ns, name, host, prule.Path, prule.Backend.ServiceName),
BackendType: eskip.LBBackend,
LBEndpoints: eps,
LBAlgorithm: getLoadBalancerAlgorithm(metadata),
HostRegexps: hostRegexp,
}
setPath(pathMode, r, prule.Path)
setTraffic(r, svcName, prule.Backend.Traffic, prule.Backend.NoopCount)
return r, nil
}
func setTraffic(r *eskip.Route, svcName string, weight float64, noopCount int) {
// add traffic predicate if traffic weight is between 0.0 and 1.0
if 0.0 < weight && weight < 1.0 {
r.Predicates = append([]*eskip.Predicate{{
Name: traffic.PredicateName,
Args: []interface{}{weight},
}}, r.Predicates...)
log.Debugf("Traffic weight %.2f for backend '%s'", weight, svcName)
}
for i := 0; i < noopCount; i++ {
r.Predicates = append([]*eskip.Predicate{{
Name: primitive.NameTrue,
Args: []interface{}{},
}}, r.Predicates...)
}
}
func applyAnnotationPredicates(m PathMode, r *eskip.Route, annotation string) error {
if annotation == "" {
return nil
}
predicates, err := eskip.ParsePredicates(annotation)
if err != nil {
return err
}
// to avoid conflict, give precedence for those predicates that come
// from annotations
if m == PathPrefix {
for _, p := range predicates {
if p.Name != "Path" && p.Name != "PathSubtree" {
continue
}
r.Path = ""
for i, p := range r.Predicates {
if p.Name != "PathSubtree" && p.Name != "Path" {
continue
}
copy(r.Predicates[i:], r.Predicates[i+1:])
r.Predicates[len(r.Predicates)-1] = nil
r.Predicates = r.Predicates[:len(r.Predicates)-1]
break
}
}
}
r.Predicates = append(r.Predicates, predicates...)
return nil
}
func (ing *ingress) addEndpointsRule(ic ingressContext, host string, prule *definitions.PathRule) error {
meta := ic.ingress.Metadata
endpointsRoute, err := convertPathRule(ic.state, meta, host, prule, ic.pathMode)
if err != nil {
// if the service is not found the route should be removed
if err == errServiceNotFound || err == errResourceNotFound {
return nil
}
// Ingress status field does not support errors
return fmt.Errorf("error while getting service: %v", err)
}
// safe prepend, see: https://play.golang.org/p/zg5aGKJpRyK
filters := make([]*eskip.Filter, len(endpointsRoute.Filters)+len(ic.annotationFilters))
copy(filters, ic.annotationFilters)
copy(filters[len(ic.annotationFilters):], endpointsRoute.Filters)
endpointsRoute.Filters = filters
// add pre-configured default filters
df, err := ic.defaultFilters.getNamed(meta.Namespace, prule.Backend.ServiceName)
if err != nil {
ic.logger.Errorf("Failed to retrieve default filters: %v.", err)
} else {
// it's safe to prepend, because type defaultFilters copies the slice during get()
endpointsRoute.Filters = append(df, endpointsRoute.Filters...)
}
err = applyAnnotationPredicates(ic.pathMode, endpointsRoute, ic.annotationPredicate)
if err != nil {
ic.logger.Errorf("failed to apply annotation predicates: %v", err)
}
ic.addHostRoute(host, endpointsRoute)
redirect := ic.redirect
if redirect.enable || redirect.override {
ic.addHostRoute(host, createIngressEnableHTTPSRedirect(endpointsRoute, redirect.code))
redirect.setHost(host)
}
if redirect.disable {
ic.addHostRoute(host, createIngressDisableHTTPSRedirect(endpointsRoute))
redirect.setHostDisabled(host)
}
if ing.kubernetesEnableEastWest {
ewRoute := createEastWestRouteIng(ing.eastWestDomainRegexpPostfix, meta.Name, meta.Namespace, endpointsRoute)
ewHost := fmt.Sprintf("%s.%s.%s", meta.Name, meta.Namespace, ing.eastWestDomainRegexpPostfix)
ic.addHostRoute(ewHost, ewRoute)
}
return nil
}
func addExtraRoutes(ic ingressContext, hosts []string, host, path, eastWestDomainRegexpPostfix string, enableEastWest bool) {
// add extra routes from optional annotation
for extraIndex, r := range ic.extraRoutes {
name := ic.ingress.Metadata.Name
ns := ic.ingress.Metadata.Namespace
route := *r
route.HostRegexps = hosts
route.Id = routeIDForCustom(
ns,
name,
route.Id,
host+strings.Replace(path, "/", "_", -1),
extraIndex)
setPath(ic.pathMode, &route, path)
if n := countPathRoutes(&route); n <= 1 {
ic.addHostRoute(host, &route)
ic.redirect.updateHost(host)
} else {
log.Errorf("Failed to add route having %d path routes: %v", n, r)
}
if enableEastWest {
ewRoute := createEastWestRouteIng(eastWestDomainRegexpPostfix, name, ns, &route)
ewHost := fmt.Sprintf("%s.%s.%s", name, ns, eastWestDomainRegexpPostfix)
ic.addHostRoute(ewHost, ewRoute)
}
}
}
// computeBackendWeights computes and sets the backend traffic weights on the
// rule backends.
// The traffic is calculated based on the following rules:
//
// * if no weight is defined for a backend it will get weight 0.
// * if no weights are specified for all backends of a path, then traffic will
// be distributed equally.
//
// Each traffic weight is relative to the number of backends per path. If there
// are multiple backends per path the weight will be relative to the number of
// remaining backends for the path e.g. if the weight is specified as
//
// backend-1: 0.2
// backend-2: 0.6
// backend-3: 0.2
//
// then the weight will be calculated to:
//
// backend-1: 0.2
// backend-2: 0.75
// backend-3: 1.0
//
// where for a weight of 1.0 no Traffic predicate will be generated.
func computeBackendWeights(backendWeights map[string]float64, rule *definitions.Rule) {
type pathInfo struct {
sum float64
lastActive *definitions.Backend
count int
weightsCount int
}
// get backend weight sum and count of backends for all paths
pathInfos := make(map[string]*pathInfo)
for _, path := range rule.Http.Paths {
sc, ok := pathInfos[path.Path]
if !ok {
sc = &pathInfo{}
pathInfos[path.Path] = sc
}
if weight, ok := backendWeights[path.Backend.ServiceName]; ok {
sc.sum += weight
if weight > 0 {
sc.lastActive = path.Backend
sc.weightsCount++
}
} else {
sc.count++
}
}
// calculate traffic weight for each backend
for _, path := range rule.Http.Paths {
if sc, ok := pathInfos[path.Path]; ok {
if weight, ok := backendWeights[path.Backend.ServiceName]; ok {
// force a weight of 1.0 for the last backend with a non-zero weight to avoid rounding issues
if sc.lastActive == path.Backend {
path.Backend.Traffic = 1.0
continue
}
path.Backend.Traffic = weight / sc.sum
// subtract weight from the sum in order to
// give subsequent backends a higher relative
// weight.
sc.sum -= weight
// noops are required to make sure that routes are in order selected by
// routing tree
if sc.weightsCount > 2 {
path.Backend.NoopCount = sc.weightsCount - 2
}
sc.weightsCount--
} else if sc.sum == 0 && sc.count > 0 {
path.Backend.Traffic = 1.0 / float64(sc.count)
}
// reduce count by one in order to give subsequent
// backends for the path a higher relative weight.
sc.count--
}
}
}
// TODO: default filters not applied to 'extra' routes from the custom route annotations. Is it on purpose?
// https://github.com/zalando/skipper/issues/1287
func (ing *ingress) addSpecRule(ic ingressContext, ru *definitions.Rule) error {
if ru.Http == nil {
ic.logger.Warn("invalid ingress item: rule missing http definitions")
return nil
}
// it is a regexp, would be better to have exact host, needs to be added in skipper
// this wrapping is temporary and escaping is not the right thing to do
// currently handled as mandatory
host := []string{"^" + strings.Replace(ru.Host, ".", "[.]", -1) + "$"}
// update Traffic field for each backend
computeBackendWeights(ic.backendWeights, ru)
for _, prule := range ru.Http.Paths {
addExtraRoutes(ic, host, ru.Host, prule.Path, ing.eastWestDomainRegexpPostfix, ing.kubernetesEnableEastWest)
if prule.Backend.Traffic > 0 {
err := ing.addEndpointsRule(ic, ru.Host, prule)
if err != nil {
return err
}
}
}
return nil
}
// converts the default backend if any
func (ing *ingress) convertDefaultBackend(state *clusterState, i *definitions.IngressItem) (*eskip.Route, bool, error) {
// the usage of the default backend depends on what we want
// we can generate a hostname out of it based on shared rules
// and instructions in annotations, if there are no rules defined
// this is a flaw in the ingress API design, because it is not on the hosts' level, but the spec
// tells to match if no rule matches. This means that there is no matching rule on this ingress
// and if there are multiple ingress items, then there is a race between them.
if i.Spec.DefaultBackend == nil {
return nil, false, nil
}
var (
eps []string
err error
ns = i.Metadata.Namespace
name = i.Metadata.Name
svcName = i.Spec.DefaultBackend.ServiceName
svcPort = i.Spec.DefaultBackend.ServicePort
)
svc, err := state.getService(ns, svcName)
if err != nil {
log.Errorf("convertDefaultBackend: Failed to get service %s, %s, %s", ns, svcName, svcPort)
return nil, false, err
}
servicePort, err := svc.getServicePort(svcPort)
if err != nil {
log.Errorf("convertDefaultBackend: Failed to find target port %v, %s, for ingress %s/%s and service %s add shuntroute: %v", svc.Spec.Ports, svcPort, ns, name, svcName, err)
err = nil
} else if svc.Spec.Type == "ExternalName" {
scheme := "https"
if n, _ := servicePort.TargetPort.Number(); n != 443 {
scheme = "http"
}
u := fmt.Sprintf("%s://%s:%s", scheme, svc.Spec.ExternalName, servicePort.TargetPort)
f, err := eskip.ParseFilters(fmt.Sprintf(`setRequestHeader("Host", "%s")`, svc.Spec.ExternalName))
if err != nil {
return nil, false, err
}
return &eskip.Route{
Id: routeID(ns, name, "default", "", svc.Spec.ExternalName),
BackendType: eskip.NetworkBackend,
Backend: u,
Filters: f,
}, true, nil
} else {
log.Debugf("convertDefaultBackend: Found target port %v, for service %s", servicePort.TargetPort, svcName)
protocol := "http"
if p, ok := i.Metadata.Annotations[skipperBackendProtocolAnnotationKey]; ok {
protocol = p
}
eps = state.getEndpointsByService(
ns,
svcName,
protocol,
servicePort,
)
log.Debugf("convertDefaultBackend: Found %d endpoints for %s: %v", len(eps), svcName, err)
}
if len(eps) == 0 {
// add shunt route https://github.com/zalando/skipper/issues/1525
log.Debugf("convertDefaultBackend: add shuntroute to return 502 for ingress %s/%s service %s with %d endpoints", ns, name, svcName, len(eps))
r := &eskip.Route{
Id: routeID(ns, name, "", "", ""),
}
shuntRoute(r)
return r, true, nil
} else if len(eps) == 1 {
return &eskip.Route{
Id: routeID(ns, name, "", "", ""),
Backend: eps[0],
}, true, nil
}
return &eskip.Route{
Id: routeID(ns, name, "", "", ""),
BackendType: eskip.LBBackend,
LBEndpoints: eps,
LBAlgorithm: getLoadBalancerAlgorithm(i.Metadata),
}, true, nil
}
func countPathRoutes(r *eskip.Route) int {
i := 0
for _, p := range r.Predicates {
if p.Name == "PathSubtree" || p.Name == "Path" {
i++
}
}
if r.Path != "" {
i++
}
return i
}
// parse filter and ratelimit annotation
func annotationFilter(i *definitions.IngressItem, logger *log.Entry) []*eskip.Filter {
var annotationFilter string
if ratelimitAnnotationValue, ok := i.Metadata.Annotations[ratelimitAnnotationKey]; ok {
annotationFilter = ratelimitAnnotationValue
}
if val, ok := i.Metadata.Annotations[skipperfilterAnnotationKey]; ok {
if annotationFilter != "" {
annotationFilter += " -> "
}
annotationFilter += val
}
if annotationFilter != "" {
annotationFilters, err := eskip.ParseFilters(annotationFilter)
if err == nil {
return annotationFilters
}
logger.Errorf("Can not parse annotation filters: %v", err)
}
return nil
}
// parse predicate annotation
func annotationPredicate(i *definitions.IngressItem) string {
var annotationPredicate string
if val, ok := i.Metadata.Annotations[skipperpredicateAnnotationKey]; ok {
annotationPredicate = val
}
return annotationPredicate
}
// parse routes annotation
func extraRoutes(i *definitions.IngressItem, logger *log.Entry) []*eskip.Route {
var extraRoutes []*eskip.Route
annotationRoutes := i.Metadata.Annotations[skipperRoutesAnnotationKey]
if annotationRoutes != "" {
var err error
extraRoutes, err = eskip.Parse(annotationRoutes)
if err != nil {
logger.Errorf("failed to parse routes from %s, skipping: %v", skipperRoutesAnnotationKey, err)
}
}
return extraRoutes
}
// parse backend-weights annotation if it exists
func backendWeights(i *definitions.IngressItem, logger *log.Entry) map[string]float64 {
var backendWeights map[string]float64
if backends, ok := i.Metadata.Annotations[backendWeightsAnnotationKey]; ok {
err := json.Unmarshal([]byte(backends), &backendWeights)
if err != nil {
logger.Errorf("error while parsing backend-weights annotation: %v", err)
}
}
return backendWeights
}
// parse pathmode from annotation or fallback to global default
func pathMode(i *definitions.IngressItem, globalDefault PathMode) PathMode {
pathMode := globalDefault
if pathModeString, ok := i.Metadata.Annotations[pathModeAnnotationKey]; ok {
if p, err := ParsePathMode(pathModeString); err != nil {
log.Errorf("Failed to get path mode for ingress %s/%s: %v", i.Metadata.Namespace, i.Metadata.Name, err)
} else {
log.Debugf("Set pathMode to %s", p)
pathMode = p
}
}
return pathMode
}
func (ing *ingress) ingressRoute(
i *definitions.IngressItem,
redirect *redirectInfo,
state *clusterState,
hostRoutes map[string][]*eskip.Route,
df defaultFilters,
) (*eskip.Route, error) {
if i.Metadata == nil || i.Metadata.Namespace == "" || i.Metadata.Name == "" ||
i.Spec == nil {
log.Error("invalid ingress item: missing Metadata")
return nil, nil
}
logger := log.WithFields(log.Fields{
"ingress": fmt.Sprintf("%s/%s", i.Metadata.Namespace, i.Metadata.Name),
})
redirect.initCurrent(i.Metadata)
ic := ingressContext{
state: state,
ingress: i,
logger: logger,
annotationFilters: annotationFilter(i, logger),
annotationPredicate: annotationPredicate(i),
extraRoutes: extraRoutes(i, logger),
backendWeights: backendWeights(i, logger),
pathMode: pathMode(i, ing.pathMode),
redirect: redirect,
hostRoutes: hostRoutes,
defaultFilters: df,
}
var route *eskip.Route
if r, ok, err := ing.convertDefaultBackend(state, i); ok {
route = r
} else if err != nil {
ic.logger.Errorf("error while converting default backend: %v", err)
}
for _, rule := range i.Spec.Rules {
err := ing.addSpecRule(ic, rule)
if err != nil {
return nil, err
}
}
return route, nil
}
func (ing *ingress) addCatchAllRoutes(host string, r *eskip.Route, redirect *redirectInfo) []*eskip.Route {
catchAll := &eskip.Route{
Id: routeID("", "catchall", host, "", ""),
HostRegexps: r.HostRegexps,
BackendType: eskip.ShuntBackend,
}
routes := []*eskip.Route{catchAll}
if ing.kubernetesEnableEastWest {
if ew := createEastWestRouteIng(ing.eastWestDomainRegexpPostfix, r.Name, r.Namespace, catchAll); ew != nil {
routes = append(routes, ew)
}
}
if code, ok := redirect.setHostCode[host]; ok {
routes = append(routes, createIngressEnableHTTPSRedirect(catchAll, code))
}
if redirect.disableHost[host] {
routes = append(routes, createIngressDisableHTTPSRedirect(catchAll))
}
return routes
}
// hasCatchAllRoutes returns true if one of the routes in the list has a catchAll
// path expression.
//
// TODO: this should also consider path types exact and subtree
func hasCatchAllRoutes(routes []*eskip.Route) bool {
for _, route := range routes {
if len(route.PathRegexps) == 0 {
return true
}
for _, exp := range route.PathRegexps {
if exp == "^/" {
return true
}
}
}
return false
}
// convert logs if an invalid found, but proceeds with the
// valid ones. Reporting failures in Ingress status is not possible,
// because Ingress status field is v1beta1.LoadBalancerIngress that only
// supports IP and Hostname as string.
func (ing *ingress) convert(state *clusterState, df defaultFilters) ([]*eskip.Route, error) {
var ewIngInfo map[string][]string // r.Id -> {namespace, name}
if ing.kubernetesEnableEastWest {
ewIngInfo = make(map[string][]string)
}
routes := make([]*eskip.Route, 0, len(state.ingresses))
hostRoutes := make(map[string][]*eskip.Route)
redirect := createRedirectInfo(ing.provideHTTPSRedirect, ing.httpsRedirectCode)
for _, i := range state.ingresses {
r, err := ing.ingressRoute(i, redirect, state, hostRoutes, df)
if err != nil {
return nil, err
}
if r != nil {
routes = append(routes, r)
if ing.kubernetesEnableEastWest {
ewIngInfo[r.Id] = []string{i.Metadata.Namespace, i.Metadata.Name}
}
}
}
for host, rs := range hostRoutes {
if len(rs) == 0 {
continue
}
applyEastWestRange(ing.eastWestRangeDomains, ing.eastWestRangePredicates, host, rs)
routes = append(routes, rs...)
// if routes were configured, but there is no catchall route
// defined for the host name, create a route which returns 404
if !hasCatchAllRoutes(rs) {
routes = append(routes, ing.addCatchAllRoutes(host, rs[0], redirect)...)
}
}
if ing.kubernetesEnableEastWest && len(routes) > 0 && len(ewIngInfo) > 0 {
ewroutes := make([]*eskip.Route, 0, len(routes))
for _, r := range routes {
if v, ok := ewIngInfo[r.Id]; ok {
ewroutes = append(ewroutes, createEastWestRouteIng(ing.eastWestDomainRegexpPostfix, v[0], v[1], r))
}
}
l := len(routes)
routes = append(routes, ewroutes...)
log.Infof("enabled east west routes: %d %d %d %d", l, len(routes), len(ewroutes), len(hostRoutes))
}
return routes, nil
}