-
Notifications
You must be signed in to change notification settings - Fork 444
/
route_tables.go
123 lines (108 loc) · 3.06 KB
/
route_tables.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
package samples
import (
"fmt"
v1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1"
"github.com/solo-io/gloo/projects/gateway/pkg/defaults"
gloov1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1/core/matchers"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
)
func LinkedRouteTablesWithVirtualService(vsName, namespace string) (*v1.VirtualService, v1.RouteTableList) {
return LenLinkedRouteTablesWithVirtualService(3, vsName, namespace)
}
func LenLinkedRouteTablesWithVirtualService(lengthOfChain int, vsName, namespace string) (*v1.VirtualService, v1.RouteTableList) {
root := "/root"
prefix := root + "/0"
makeRt := func(i int) *v1.RouteTable {
return &v1.RouteTable{
Metadata: &core.Metadata{Name: fmt.Sprintf("node-%d", i), Namespace: namespace},
Routes: []*v1.Route{{
Name: "testRouteName",
Matchers: []*matchers.Matcher{{
PathSpecifier: &matchers.Matcher_Prefix{
Prefix: prefix,
},
}},
},
}}
}
routeTables := v1.RouteTableList{
makeRt(0),
}
// append a chain of route tables
for i := 1; i < lengthOfChain; i++ {
prefix += fmt.Sprintf("/%d", i)
routeTables = append(routeTables, makeRt(i))
// set delegate of previous to what we appended
ref := routeTables[i].GetMetadata().Ref()
routeTables[i-1].GetRoutes()[0].Action = &v1.Route_DelegateAction{
DelegateAction: &v1.DelegateAction{
DelegationType: &v1.DelegateAction_Ref{
Ref: ref,
},
},
}
}
// append the leaf
leaf := &v1.RouteTable{
Metadata: &core.Metadata{Name: "leaf", Namespace: namespace},
Routes: []*v1.Route{
{
Matchers: []*matchers.Matcher{{
PathSpecifier: &matchers.Matcher_Exact{
Exact: prefix + "/exact",
},
}},
Action: &v1.Route_DirectResponseAction{DirectResponseAction: &gloov1.DirectResponseAction{}},
},
},
}
leafRef := leaf.GetMetadata().Ref()
routeTables[lengthOfChain-1].GetRoutes()[0].Action = &v1.Route_DelegateAction{
DelegateAction: &v1.DelegateAction{
DelegationType: &v1.DelegateAction_Ref{
Ref: leafRef,
},
},
}
routeTables = append(routeTables, leaf)
ref := routeTables[0].GetMetadata().Ref()
vs := defaults.DefaultVirtualService(namespace, vsName)
vs.GetVirtualHost().Routes = []*v1.Route{{
Matchers: []*matchers.Matcher{{
PathSpecifier: &matchers.Matcher_Prefix{
Prefix: root,
},
}},
Action: &v1.Route_DelegateAction{
DelegateAction: &v1.DelegateAction{
DelegationType: &v1.DelegateAction_Ref{
Ref: ref,
},
},
},
}}
return vs, routeTables
}
func RouteTableWithLabelsAndPrefix(name, namespace, prefix string, labels map[string]string) *v1.RouteTable {
return &v1.RouteTable{
Metadata: &core.Metadata{
Name: name,
Namespace: namespace,
Labels: labels,
},
Routes: []*v1.Route{
{
Matchers: []*matchers.Matcher{
{
PathSpecifier: &matchers.Matcher_Prefix{
Prefix: prefix,
},
},
},
Action: &v1.Route_DirectResponseAction{
DirectResponseAction: &gloov1.DirectResponseAction{Status: 200}},
},
},
}
}