/
hosts.go
64 lines (54 loc) · 1.28 KB
/
hosts.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
package kubernetes
import (
"fmt"
"strings"
"github.com/zalando/skipper/eskip"
)
func rxDots(h string) string {
return strings.Replace(h, ".", "[.]", -1)
}
func createHostRx(h ...string) string {
if len(h) == 0 {
return ""
}
hrx := make([]string, len(h))
for i := range h {
hrx[i] = rxDots(h[i])
}
return fmt.Sprintf("^(%s)$", strings.Join(hrx, "|"))
}
// hostCatchAllRoutes creates catch-all routes for those hosts that only have routes with
// a Host predicate and at least one additional predicate.
//
// currently only used for RouteGroups
func hostCatchAllRoutes(hostRoutes map[string][]*eskip.Route, createID func(string) string) []*eskip.Route {
var catchAll []*eskip.Route
for h, r := range hostRoutes {
var hasHostOnlyRoute bool
for _, ri := range r {
ct := eskip.Canonical(ri)
var hasNonHostPredicate bool
for _, p := range ct.Predicates {
if p.Name != "Host" {
hasNonHostPredicate = true
break
}
}
if !hasNonHostPredicate {
hasHostOnlyRoute = true
break
}
}
if !hasHostOnlyRoute {
catchAll = append(catchAll, &eskip.Route{
Id: createID(h),
Predicates: []*eskip.Predicate{{
Name: "Host",
Args: []interface{}{createHostRx(h)},
}},
BackendType: eskip.ShuntBackend,
})
}
}
return catchAll
}