-
Notifications
You must be signed in to change notification settings - Fork 444
/
virtual_services.go
209 lines (182 loc) · 6.05 KB
/
virtual_services.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
package helpers
import (
"errors"
"github.com/golang/protobuf/proto"
"github.com/onsi/ginkgo"
v1 "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"
)
// virtualServiceBuilder simplifies the process of generating VirtualServices in tests
type virtualServiceBuilder struct {
name string
namespace string
domains []string
routesByName map[string]*v1.Route
sslConfig *gloov1.SslConfig
}
func BuilderFromVirtualService(vs *v1.VirtualService) *virtualServiceBuilder {
builder := &virtualServiceBuilder{
name: vs.GetMetadata().GetName(),
namespace: vs.GetMetadata().GetNamespace(),
domains: vs.GetVirtualHost().GetDomains(),
sslConfig: vs.GetSslConfig(),
routesByName: make(map[string]*v1.Route, 10),
}
for _, r := range vs.GetVirtualHost().GetRoutes() {
builder.WithRoute(r.GetName(), r)
}
return builder
}
func NewVirtualServiceBuilder() *virtualServiceBuilder {
return &virtualServiceBuilder{
routesByName: make(map[string]*v1.Route, 10),
}
}
func (b *virtualServiceBuilder) WithSslConfig(sslConfig *gloov1.SslConfig) *virtualServiceBuilder {
b.sslConfig = sslConfig
return b
}
func (b *virtualServiceBuilder) WithName(name string) *virtualServiceBuilder {
b.name = name
return b
}
func (b *virtualServiceBuilder) WithNamespace(namespace string) *virtualServiceBuilder {
b.namespace = namespace
return b
}
func (b *virtualServiceBuilder) WithDomain(domain string) *virtualServiceBuilder {
b.domains = []string{domain}
return b
}
func (b *virtualServiceBuilder) WithRouteOptions(routeName string, routeOptions *gloov1.RouteOptions) *virtualServiceBuilder {
return b.WithRouteMutation(routeName, func(route *v1.Route) {
route.Options = routeOptions
})
}
func (b *virtualServiceBuilder) WithRoute(routeName string, route *v1.Route) *virtualServiceBuilder {
b.routesByName[routeName] = route
return b
}
func (b *virtualServiceBuilder) getOrDefaultRoute(routeName string) *v1.Route {
route, ok := b.routesByName[routeName]
if !ok {
return &v1.Route{
Name: routeName,
}
}
return route
}
func (b *virtualServiceBuilder) WithRouteActionToUpstream(routeName string, upstream *gloov1.Upstream) *virtualServiceBuilder {
return b.WithRouteActionToUpstreamRef(routeName, upstream.GetMetadata().Ref())
}
func (b *virtualServiceBuilder) WithRouteActionToUpstreamRef(routeName string, upstreamRef *core.ResourceRef) *virtualServiceBuilder {
return b.WithRouteMutation(routeName, func(route *v1.Route) {
route.Action = &v1.Route_RouteAction{
RouteAction: &gloov1.RouteAction{
Destination: &gloov1.RouteAction_Single{
Single: &gloov1.Destination{
DestinationType: &gloov1.Destination_Upstream{
Upstream: upstreamRef,
},
},
},
},
}
})
}
func (b *virtualServiceBuilder) WithRouteDelegateActionRef(routeName string, delegateRef *core.ResourceRef) *virtualServiceBuilder {
return b.WithRouteDelegateAction(routeName,
&v1.DelegateAction{
DelegationType: &v1.DelegateAction_Ref{
Ref: delegateRef,
},
})
}
func (b *virtualServiceBuilder) WithRouteDelegateActionSelector(routeName string, delegateSelector *v1.RouteTableSelector) *virtualServiceBuilder {
return b.WithRouteMutation(routeName, func(route *v1.Route) {
route.Action = &v1.Route_DelegateAction{
DelegateAction: &v1.DelegateAction{
DelegationType: &v1.DelegateAction_Selector{
Selector: delegateSelector,
},
},
}
})
}
func (b *virtualServiceBuilder) WithRouteDelegateAction(routeName string, delegateAction *v1.DelegateAction) *virtualServiceBuilder {
return b.WithRouteMutation(routeName, func(route *v1.Route) {
route.Action = &v1.Route_DelegateAction{
DelegateAction: delegateAction,
}
})
}
func (b *virtualServiceBuilder) WithRouteActionToDestination(routeName string, destination *gloov1.Destination) *virtualServiceBuilder {
return b.WithRouteMutation(routeName, func(route *v1.Route) {
route.Action = &v1.Route_RouteAction{
RouteAction: &gloov1.RouteAction{
Destination: &gloov1.RouteAction_Single{
Single: destination,
},
},
}
})
}
func (b *virtualServiceBuilder) WithRoutePrefixMatcher(routeName string, prefixMatch string) *virtualServiceBuilder {
return b.WithRouteMutation(routeName, func(route *v1.Route) {
route.Matchers = []*matchers.Matcher{{
PathSpecifier: &matchers.Matcher_Prefix{
Prefix: prefixMatch,
},
}}
})
}
func (b *virtualServiceBuilder) WithRouteMatcher(routeName string, matcher *matchers.Matcher) *virtualServiceBuilder {
return b.WithRouteMutation(routeName, func(route *v1.Route) {
route.Matchers = []*matchers.Matcher{matcher}
})
}
func (b *virtualServiceBuilder) WithRouteMutation(routeName string, mutation func(route *v1.Route)) *virtualServiceBuilder {
route := b.getOrDefaultRoute(routeName)
mutation(route)
return b.WithRoute(routeName, route)
}
func (b *virtualServiceBuilder) errorIfInvalid() error {
if len(b.domains) == 0 {
// Unset domains will behave like a wildcard "*", which contributes to test flakes
return errors.New("attempting to not set a VirtualService domain")
}
for _, domain := range b.domains {
if domain == "*" {
// Wildcard domains contribute to test flakes
return errors.New("attempting to set * as a VirtualService domain")
}
}
return nil
}
func (b *virtualServiceBuilder) Build() *v1.VirtualService {
if err := b.errorIfInvalid(); err != nil {
// We error loudly here
// These types of errors are intended to prevent developers from creating resources
// which are semantically correct, but lead to test flakes/confusion
ginkgo.Fail(err.Error())
}
var routes []*v1.Route
for _, route := range b.routesByName {
routes = append(routes, route)
}
vs := &v1.VirtualService{
Metadata: &core.Metadata{
Name: b.name,
Namespace: b.namespace,
},
VirtualHost: &v1.VirtualHost{
Domains: b.domains,
Routes: routes,
Options: nil,
},
SslConfig: b.sslConfig,
}
return proto.Clone(vs).(*v1.VirtualService)
}