forked from kgateway-dev/kgateway
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroutes.go
150 lines (130 loc) · 3.67 KB
/
routes.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
package helpers
import (
"github.com/golang/protobuf/proto"
gatewayv1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1"
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"
)
const (
ExactPath = iota
PrefixPath
RegexPath
)
func MakeMultiMatcherRoute(pathType1, length1, pathType2, length2 int) *gloov1.Route {
return &gloov1.Route{Matchers: []*matchers.Matcher{
MakeMatcher(pathType1, length1),
MakeMatcher(pathType2, length2),
}}
}
func MakeRoute(pathType, length int) *gloov1.Route {
return &gloov1.Route{Matchers: []*matchers.Matcher{MakeMatcher(pathType, length)}}
}
func MakeGatewayRoute(pathType, length int) *gatewayv1.Route {
return &gatewayv1.Route{Matchers: []*matchers.Matcher{MakeMatcher(pathType, length)}}
}
func MakeMatcher(pathType, length int) *matchers.Matcher {
pathStr := "/"
for i := 0; i < length; i++ {
pathStr += "s/"
}
m := &matchers.Matcher{}
switch pathType {
case ExactPath:
m.PathSpecifier = &matchers.Matcher_Exact{pathStr}
case PrefixPath:
m.PathSpecifier = &matchers.Matcher_Prefix{pathStr}
case RegexPath:
m.PathSpecifier = &matchers.Matcher_Regex{pathStr}
default:
panic("bad test")
}
return m
}
// RouteBuilder simplifies the process of generating Routes in tests
type RouteBuilder struct {
name string
matchers []*matchers.Matcher
routeOptions *gloov1.RouteOptions
routeAction *gloov1.RouteAction
}
// BuilderFromRoute creates a new RouteBuilder from an existing Route
func BuilderFromRoute(r *gatewayv1.Route) *RouteBuilder {
builder := &RouteBuilder{
name: r.GetName(),
routeOptions: r.GetOptions(),
routeAction: r.GetRouteAction(),
matchers: make([]*matchers.Matcher, len(r.GetMatchers())),
}
for _, m := range r.GetMatchers() {
builder.WithMatcher(m)
}
return builder
}
// NewRouteBuilder creates an empty RouteBuilder
func NewRouteBuilder() *RouteBuilder {
return &RouteBuilder{
matchers: make([]*matchers.Matcher, 0),
}
}
func (b *RouteBuilder) WithName(name string) *RouteBuilder {
b.name = name
return b
}
func (b *RouteBuilder) WithMatcher(matcher *matchers.Matcher) *RouteBuilder {
b.matchers = append(b.matchers, matcher)
return b
}
func (b *RouteBuilder) WithPrefixMatcher(prefix string) *RouteBuilder {
prefixMatch := &matchers.Matcher{
PathSpecifier: &matchers.Matcher_Prefix{Prefix: prefix},
}
b.matchers = append(b.matchers, prefixMatch)
return b
}
func (b *RouteBuilder) WithRouteOptions(opts *gloov1.RouteOptions) *RouteBuilder {
b.routeOptions = opts
return b
}
func (b *RouteBuilder) WithRouteAction(routeAction *gloov1.RouteAction) *RouteBuilder {
b.routeAction = routeAction
return b
}
func (b *RouteBuilder) WithRouteActionToUpstreamRef(ref *core.ResourceRef) *RouteBuilder {
b.routeAction = &gloov1.RouteAction{
Destination: &gloov1.RouteAction_Single{
Single: &gloov1.Destination{
DestinationType: &gloov1.Destination_Upstream{
Upstream: ref,
},
},
},
}
return b
}
func (b *RouteBuilder) Clone() *RouteBuilder {
if b == nil {
return nil
}
clone := new(RouteBuilder)
clone.name = b.name
clone.routeOptions = b.routeOptions
clone.routeAction = b.routeAction
clone.matchers = make([]*matchers.Matcher, len(b.matchers))
for i, m := range b.matchers {
clone.matchers[i] = m.Clone().(*matchers.Matcher)
}
return clone
}
func (b *RouteBuilder) Build() *gatewayv1.Route {
action := &gatewayv1.Route_RouteAction{
RouteAction: b.routeAction,
}
rt := &gatewayv1.Route{
Name: b.name,
Matchers: b.matchers,
Options: b.routeOptions,
Action: action,
}
return proto.Clone(rt).(*gatewayv1.Route)
}