-
Notifications
You must be signed in to change notification settings - Fork 444
/
routematch.go
179 lines (168 loc) · 6.57 KB
/
routematch.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
package gogoutils
import (
envoy_config_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
envoy_config_route_v3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
envoytype "github.com/envoyproxy/go-control-plane/envoy/type/v3"
types "github.com/gogo/protobuf/types"
envoyroute_gloo "github.com/solo-io/gloo/projects/gloo/pkg/api/external/envoy/api/v2/route"
envoytype_gloo "github.com/solo-io/gloo/projects/gloo/pkg/api/external/envoy/type"
envoycore_sk "github.com/solo-io/solo-kit/pkg/api/external/envoy/api/v2/core"
envoytype_sk "github.com/solo-io/solo-kit/pkg/api/external/envoy/type"
)
// Converts between Envoy and Gloo/solokit versions of envoy protos
// This is required because go-control-plane dropped gogoproto in favor of goproto
// in v0.9.0, but solokit depends on gogoproto (and the generated deep equals it creates).
//
// we should work to remove that assumption from solokit and delete this code:
// https://github.com/solo-io/gloo/issues/1793
// todo consider movinng this to solo-projects
// used in enterprise code
func ToGlooRouteMatch(routeMatch *envoy_config_route_v3.RouteMatch) *envoyroute_gloo.RouteMatch {
if routeMatch == nil {
return nil
}
rm := &envoyroute_gloo.RouteMatch{
PathSpecifier: nil, // gets set later in function
CaseSensitive: BoolProtoToGogo(routeMatch.GetCaseSensitive()),
RuntimeFraction: ToGlooRuntimeFractionalPercent(routeMatch.GetRuntimeFraction()),
Headers: ToGlooHeaders(routeMatch.GetHeaders()),
QueryParameters: ToGlooQueryParameterMatchers(routeMatch.GetQueryParameters()),
Grpc: ToGlooGrpc(routeMatch.GetGrpc()),
}
switch typed := routeMatch.GetPathSpecifier().(type) {
case *envoy_config_route_v3.RouteMatch_Prefix:
rm.PathSpecifier = &envoyroute_gloo.RouteMatch_Prefix{
Prefix: typed.Prefix,
}
case *envoy_config_route_v3.RouteMatch_SafeRegex:
rm.PathSpecifier = &envoyroute_gloo.RouteMatch_Regex{
Regex: typed.SafeRegex.GetRegex(),
}
case *envoy_config_route_v3.RouteMatch_Path:
rm.PathSpecifier = &envoyroute_gloo.RouteMatch_Path{
Path: typed.Path,
}
}
return rm
}
func ToGlooRuntimeFractionalPercent(fp *envoy_config_core_v3.RuntimeFractionalPercent) *envoycore_sk.RuntimeFractionalPercent {
if fp == nil {
return nil
}
return &envoycore_sk.RuntimeFractionalPercent{
DefaultValue: ToGlooFractionalPercent(fp.GetDefaultValue()),
RuntimeKey: fp.GetRuntimeKey(),
}
}
func ToGlooFractionalPercent(fp *envoytype.FractionalPercent) *envoytype_sk.FractionalPercent {
if fp == nil {
return nil
}
glooFp := &envoytype_sk.FractionalPercent{
Numerator: fp.GetNumerator(),
Denominator: envoytype_sk.FractionalPercent_HUNDRED, // gets set later in function
}
switch str := fp.GetDenominator().String(); str {
case envoytype.FractionalPercent_DenominatorType_name[int32(envoytype.FractionalPercent_HUNDRED)]:
glooFp.Denominator = envoytype_sk.FractionalPercent_HUNDRED
case envoytype.FractionalPercent_DenominatorType_name[int32(envoytype.FractionalPercent_TEN_THOUSAND)]:
glooFp.Denominator = envoytype_sk.FractionalPercent_TEN_THOUSAND
case envoytype.FractionalPercent_DenominatorType_name[int32(envoytype.FractionalPercent_MILLION)]:
glooFp.Denominator = envoytype_sk.FractionalPercent_MILLION
}
return glooFp
}
func ToGlooHeaders(headers []*envoy_config_route_v3.HeaderMatcher) []*envoyroute_gloo.HeaderMatcher {
if headers == nil {
return nil
}
result := make([]*envoyroute_gloo.HeaderMatcher, len(headers))
for i, v := range headers {
result[i] = ToGlooHeader(v)
}
return result
}
func ToGlooHeader(header *envoy_config_route_v3.HeaderMatcher) *envoyroute_gloo.HeaderMatcher {
if header == nil {
return nil
}
h := &envoyroute_gloo.HeaderMatcher{
Name: header.GetName(),
HeaderMatchSpecifier: nil, // gets set later in function
InvertMatch: header.GetInvertMatch(),
}
switch specificHeaderSpecifier := header.HeaderMatchSpecifier.(type) {
case *envoy_config_route_v3.HeaderMatcher_ExactMatch:
h.HeaderMatchSpecifier = &envoyroute_gloo.HeaderMatcher_ExactMatch{
ExactMatch: specificHeaderSpecifier.ExactMatch,
}
case *envoy_config_route_v3.HeaderMatcher_SafeRegexMatch:
h.HeaderMatchSpecifier = &envoyroute_gloo.HeaderMatcher_RegexMatch{
RegexMatch: specificHeaderSpecifier.SafeRegexMatch.GetRegex(),
}
case *envoy_config_route_v3.HeaderMatcher_RangeMatch:
h.HeaderMatchSpecifier = &envoyroute_gloo.HeaderMatcher_RangeMatch{
RangeMatch: &envoytype_gloo.Int64Range{
Start: specificHeaderSpecifier.RangeMatch.Start,
End: specificHeaderSpecifier.RangeMatch.End,
},
}
case *envoy_config_route_v3.HeaderMatcher_PresentMatch:
h.HeaderMatchSpecifier = &envoyroute_gloo.HeaderMatcher_PresentMatch{
PresentMatch: specificHeaderSpecifier.PresentMatch,
}
case *envoy_config_route_v3.HeaderMatcher_PrefixMatch:
h.HeaderMatchSpecifier = &envoyroute_gloo.HeaderMatcher_PrefixMatch{
PrefixMatch: specificHeaderSpecifier.PrefixMatch,
}
case *envoy_config_route_v3.HeaderMatcher_SuffixMatch:
h.HeaderMatchSpecifier = &envoyroute_gloo.HeaderMatcher_SuffixMatch{
SuffixMatch: specificHeaderSpecifier.SuffixMatch,
}
}
return h
}
func ToGlooQueryParameterMatchers(queryParamMatchers []*envoy_config_route_v3.QueryParameterMatcher) []*envoyroute_gloo.QueryParameterMatcher {
if queryParamMatchers == nil {
return nil
}
result := make([]*envoyroute_gloo.QueryParameterMatcher, len(queryParamMatchers))
for i, v := range queryParamMatchers {
result[i] = ToGlooQueryParameterMatcher(v)
}
return result
}
func ToGlooQueryParameterMatcher(queryParamMatcher *envoy_config_route_v3.QueryParameterMatcher) *envoyroute_gloo.QueryParameterMatcher {
if queryParamMatcher == nil {
return nil
}
value := ""
regex := false
switch {
case queryParamMatcher.GetPresentMatch():
case queryParamMatcher.GetStringMatch().GetExact() != "":
value = queryParamMatcher.GetStringMatch().GetExact()
case queryParamMatcher.GetStringMatch().GetSafeRegex() != nil:
value = queryParamMatcher.GetStringMatch().GetSafeRegex().GetRegex()
regex = true
}
qpm := &envoyroute_gloo.QueryParameterMatcher{
Name: queryParamMatcher.GetName(),
Value: value,
}
if regex {
qpm.Regex = &types.BoolValue{
Value: true,
}
}
return qpm
}
func ToGlooGrpc(grpc *envoy_config_route_v3.RouteMatch_GrpcRouteMatchOptions) *envoyroute_gloo.RouteMatch_GrpcRouteMatchOptions {
if grpc == nil {
return nil
}
return &envoyroute_gloo.RouteMatch_GrpcRouteMatchOptions{
// envoy currently doesn't support any options :/
// all the more reason to worry about future regressions with this code ala https://github.com/solo-io/gloo/issues/1793
}
}