This repository has been archived by the owner on Jun 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
/
router.go
358 lines (311 loc) · 8.84 KB
/
router.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
package vsfactory
import (
"time"
"github.com/rancher/rio/modules/istio/controller/pkg"
"github.com/gogo/protobuf/types"
riov1 "github.com/rancher/rio/pkg/apis/rio.cattle.io/v1"
name2 "github.com/rancher/wrangler/pkg/name"
gatewayv1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1"
solov1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1/kube/apis/gateway.solo.io/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/gloo/projects/gloo/pkg/api/v1/options/faultinjection"
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1/options/headers"
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1/options/retries"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (f *VirtualServiceFactory) ForRouter(router *riov1.Router) ([]*solov1.VirtualService, error) {
vs, err := vsForRouter(router)
if err != nil {
return nil, err
}
result := []*solov1.VirtualService{
vs,
}
tls, err := f.FindTLS(router.Namespace, router.Name, "", vs.Spec.VirtualHost.Domains)
if err != nil {
return nil, err
}
for hostname, tls := range tls {
result = append(result, tlsCopy(hostname, f.systemNamespace, tls, vs))
}
configureHTTPSRedirect(router, result)
f.InjectACME(vs)
return result, nil
}
// Have to reconfigure HTTPS redirect since we only want to set redirect on http virtualservice
func configureHTTPSRedirect(router *riov1.Router, vss []*solov1.VirtualService) {
for _, vs := range vss {
if vs.Spec.SslConfig == nil {
for i, r := range router.Spec.Routes {
if vs.Spec.VirtualHost != nil && len(vs.Spec.VirtualHost.Routes) <= i {
continue
}
if r.Redirect != nil && r.Redirect.ToHTTPS {
addRedirect(r, vs.Spec.VirtualHost.Routes[i])
}
}
}
}
}
func vsForRouter(router *riov1.Router) (*solov1.VirtualService, error) {
vs := &solov1.VirtualService{
ObjectMeta: metav1.ObjectMeta{
Namespace: router.Namespace,
Name: router.Name,
},
Spec: gatewayv1.VirtualService{
VirtualHost: &gatewayv1.VirtualHost{},
},
}
domains, err := pkg.Domains(router)
if err != nil {
return nil, err
}
vs.Spec.VirtualHost.Domains = domains
for _, route := range router.Spec.Routes {
vs.Spec.VirtualHost.Routes = append(vs.Spec.VirtualHost.Routes, routeToRoute(router.Namespace, route))
}
return vs, nil
}
func headersToHeaders(hs *riov1.HeaderOperations) *headers.HeaderManipulation {
result := &headers.HeaderManipulation{}
for _, add := range hs.Add {
header := &headers.HeaderValueOption{
Header: &headers.HeaderValue{
Key: add.Name,
Value: add.Value,
},
Append: &types.BoolValue{
Value: true,
},
}
result.RequestHeadersToAdd = append(result.RequestHeadersToAdd, header)
}
for _, add := range hs.Set {
header := &headers.HeaderValueOption{
Header: &headers.HeaderValue{
Key: add.Name,
Value: add.Value,
},
}
result.RequestHeadersToAdd = append(result.RequestHeadersToAdd, header)
}
result.RequestHeadersToRemove = hs.Remove
return result
}
func routeToRoute(namespace string, route riov1.RouteSpec) (result *gatewayv1.Route) {
result = &gatewayv1.Route{
Options: &gloov1.RouteOptions{},
Matchers: []*matchers.Matcher{matchToMatch(route.Match)},
}
if route.Headers != nil {
result.Options = &gloov1.RouteOptions{
HeaderManipulation: headersToHeaders(route.Headers),
}
}
if route.Fault != nil {
addFault(route, result)
}
if route.Rewrite != nil {
addRewrite(route, result)
}
if route.Redirect != nil {
addRedirect(route, result)
}
if route.Mirror != nil {
addMirror(namespace, route, result)
}
if route.Retry != nil {
addRetry(route, result)
}
if len(route.To) == 1 {
addSingleDestination(namespace, route, result)
}
if len(route.To) > 1 {
addMultiDestination(namespace, route, result)
}
if route.TimeoutSeconds != nil {
addTimeout(route, result)
}
return
}
func addMultiDestination(namespace string, route riov1.RouteSpec, gatewayRoute *gatewayv1.Route) {
md := &gloov1.MultiDestination{}
for _, to := range route.To {
port := to.Port
if port == 0 {
port = 80
}
md.Destinations = append(md.Destinations, &gloov1.WeightedDestination{
Destination: &gloov1.Destination{
DestinationType: &gloov1.Destination_Kube{
Kube: &gloov1.KubernetesServiceDestination{
Ref: *destinationToRef(namespace, &to.Destination),
Port: port,
},
},
},
Weight: uint32(to.Weight),
})
}
gatewayRoute.Action = &gatewayv1.Route_RouteAction{
RouteAction: &gloov1.RouteAction{
Destination: &gloov1.RouteAction_Multi{
Multi: md,
},
},
}
}
func addSingleDestination(namespace string, route riov1.RouteSpec, gatewayRoute *gatewayv1.Route) {
port := route.To[0].Port
if port == 0 {
port = 80
}
gatewayRoute.Action = &gatewayv1.Route_RouteAction{
RouteAction: &gloov1.RouteAction{
Destination: &gloov1.RouteAction_Single{
Single: &gloov1.Destination{
DestinationType: &gloov1.Destination_Kube{
Kube: &gloov1.KubernetesServiceDestination{
Ref: *destinationToRef(namespace, &route.To[0].Destination),
Port: port,
},
},
},
},
},
}
}
func addMirror(namespace string, route riov1.RouteSpec, gatewayRoute *gatewayv1.Route) {
gatewayRoute.Action = &gatewayv1.Route_DelegateAction{
DelegateAction: destinationToAction(namespace, route.Mirror),
}
}
func addRedirect(route riov1.RouteSpec, gatewayRoute *gatewayv1.Route) {
ra := &gatewayv1.Route_RedirectAction{
RedirectAction: &gloov1.RedirectAction{
HostRedirect: route.Redirect.Host,
HttpsRedirect: route.Redirect.ToHTTPS,
},
}
if route.Redirect.Path != "" {
ra.RedirectAction.PathRewriteSpecifier = &gloov1.RedirectAction_PathRedirect{
PathRedirect: route.Redirect.Path,
}
}
gatewayRoute.Action = ra
}
func addRewrite(route riov1.RouteSpec, gatewayRoute *gatewayv1.Route) {
if route.Rewrite.Path != "" {
gatewayRoute.Options.PrefixRewrite = &types.StringValue{
Value: route.Rewrite.Path,
}
}
if route.Rewrite.Host != "" {
gatewayRoute.Options.HostRewriteType = &gloov1.RouteOptions_HostRewrite{
HostRewrite: route.Rewrite.Host,
}
}
}
func addTimeout(route riov1.RouteSpec, gatewayRoute *gatewayv1.Route) {
t := time.Duration(int64(*route.TimeoutSeconds)) * time.Second
gatewayRoute.Options.Timeout = &t
}
func addFault(route riov1.RouteSpec, gatewayRoute *gatewayv1.Route) {
gatewayRoute.Options.Faults = faultToFault(route.Fault)
}
func addRetry(route riov1.RouteSpec, gatewayRoute *gatewayv1.Route) {
var t time.Duration
if route.TimeoutSeconds != nil {
t = time.Duration(*route.TimeoutSeconds) * time.Second
}
gatewayRoute.Options.Retries = &retries.RetryPolicy{
NumRetries: uint32(route.Retry.Attempts),
PerTryTimeout: &t,
}
}
func destinationToAction(namespace string, dest *riov1.Destination) *gatewayv1.DelegateAction {
name := dest.App
if dest.Version != "" {
name = name2.SafeConcatName(name + "-" + dest.Version)
}
return &gatewayv1.DelegateAction{
Name: name,
Namespace: namespace,
}
}
func destinationToRef(namespace string, dest *riov1.Destination) *core.ResourceRef {
name := dest.App
if dest.Version != "" {
name = name2.SafeConcatName(name + "-" + dest.Version)
}
return &core.ResourceRef{
Name: name,
Namespace: namespace,
}
}
func faultToFault(fault *riov1.Fault) *faultinjection.RouteFaults {
if fault == nil {
return nil
}
result := &faultinjection.RouteFaults{}
if fault.AbortHTTPStatus > 0 {
result.Abort = &faultinjection.RouteAbort{
Percentage: float32(fault.Percentage),
HttpStatus: uint32(fault.AbortHTTPStatus),
}
}
if fault.DelayMillis > 0 {
delay := time.Duration(fault.DelayMillis) * time.Millisecond
result.Delay = &faultinjection.RouteDelay{
Percentage: float32(fault.Percentage),
FixedDelay: &delay,
}
}
return result
}
func matchToMatch(match riov1.Match) (result *matchers.Matcher) {
result = &matchers.Matcher{}
if match.Path != nil {
switch {
case match.Path.Regexp != "":
result.PathSpecifier = &matchers.Matcher_Regex{
Regex: match.Path.Regexp,
}
case match.Path.Prefix != "":
result.PathSpecifier = &matchers.Matcher_Prefix{
Prefix: match.Path.Prefix,
}
case match.Path.Exact != "":
result.PathSpecifier = &matchers.Matcher_Exact{
Exact: match.Path.Exact,
}
}
} else {
result.PathSpecifier = &matchers.Matcher_Prefix{
Prefix: "/",
}
}
for _, match := range match.Headers {
m := &matchers.HeaderMatcher{
Name: match.Name,
}
if match.Value != nil {
switch {
case match.Value.Regexp != "":
m.Regex = true
m.Value = match.Value.Regexp
case match.Value.Prefix != "":
m.Regex = true
m.Value = "^" + match.Value.Prefix + ".*"
case match.Value.Exact != "":
m.Value = match.Value.Exact
}
}
result.Headers = append(result.Headers, m)
}
result.Methods = match.Methods
return
}