This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 277
/
config.go
278 lines (245 loc) · 9.87 KB
/
config.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
package route
import (
"fmt"
"sort"
"strings"
set "github.com/deckarep/golang-set"
xds_route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
xds_matcher "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/openservicemesh/osm/pkg/constants"
"github.com/openservicemesh/osm/pkg/envoy"
"github.com/openservicemesh/osm/pkg/service"
"github.com/openservicemesh/osm/pkg/trafficpolicy"
)
// Direction is a type to signify the direction associated with a route
type Direction int
const (
// OutboundRoute is the direction for an outbound route
OutboundRoute Direction = iota
// InboundRoute is the direction for an inbound route
InboundRoute
)
const (
//InboundRouteConfigName is the name of the route config that the envoy will identify
InboundRouteConfigName = "RDS_Inbound"
//OutboundRouteConfigName is the name of the route config that the envoy will identify
OutboundRouteConfigName = "RDS_Outbound"
// inboundVirtualHost is the name of the virtual host on the inbound route configuration
inboundVirtualHost = "inbound_virtualHost"
// outboundVirtualHost is the name of the virtual host on the outbound route configuration
outboundVirtualHost = "outbound_virtualHost"
// MethodHeaderKey is the key of the header for HTTP methods
MethodHeaderKey = ":method"
// httpHostHeader is the name of the HTTP host header
httpHostHeader = "host"
)
//UpdateRouteConfiguration consrtucts the Envoy construct necessary for TrafficTarget implementation
func UpdateRouteConfiguration(domainRoutesMap map[string]map[string]trafficpolicy.RouteWeightedClusters, routeConfig *xds_route.RouteConfiguration, direction Direction) {
log.Trace().Msgf("[RDS] Updating Route Configuration")
var virtualHostPrefix string
switch direction {
case OutboundRoute:
virtualHostPrefix = outboundVirtualHost
case InboundRoute:
virtualHostPrefix = inboundVirtualHost
default:
log.Error().Msgf("Invalid route direction: %v", direction)
return
}
for host, routePolicyWeightedClustersMap := range domainRoutesMap {
domains := getDistinctDomains(routePolicyWeightedClustersMap)
virtualHost := createVirtualHostStub(virtualHostPrefix, host, domains)
virtualHost.Routes = createRoutes(routePolicyWeightedClustersMap, direction)
routeConfig.VirtualHosts = append(routeConfig.VirtualHosts, virtualHost)
}
}
func createVirtualHostStub(namePrefix string, host string, domains set.Set) *xds_route.VirtualHost {
var domainsSlice []string
for domainIntf := range domains.Iter() {
domainsSlice = append(domainsSlice, strings.TrimSpace(domainIntf.(string)))
}
name := fmt.Sprintf("%s|%s", namePrefix, host)
virtualHost := xds_route.VirtualHost{
Name: name,
Domains: domainsSlice,
}
return &virtualHost
}
func createRoutes(routePolicyWeightedClustersMap map[string]trafficpolicy.RouteWeightedClusters, direction Direction) []*xds_route.Route {
var routes []*xds_route.Route
if direction == OutboundRoute {
// For a source service, configure a wildcard route match (without any headers) with weighted routes to upstream clusters based on traffic split policies
weightedClusters := getDistinctWeightedClusters(routePolicyWeightedClustersMap)
totalClustersWeight := getTotalWeightForClusters(weightedClusters)
emptyHeaders := make(map[string]string)
route := getRoute(constants.RegexMatchAll, constants.WildcardHTTPMethod, emptyHeaders, weightedClusters, totalClustersWeight, OutboundRoute)
routes = append(routes, route)
return routes
}
for _, routePolicyWeightedClusters := range routePolicyWeightedClustersMap {
// For a given route path, sanitize the methods in case there
// is wildcard or if there are duplicates
allowedMethods := sanitizeHTTPMethods(routePolicyWeightedClusters.HTTPRoute.Methods)
for _, method := range allowedMethods {
route := getRoute(routePolicyWeightedClusters.HTTPRoute.PathRegex, method, routePolicyWeightedClusters.HTTPRoute.Headers, routePolicyWeightedClusters.WeightedClusters, 100, direction)
routes = append(routes, route)
}
}
return routes
}
func getRoute(pathRegex string, method string, headersMap map[string]string, weightedClusters set.Set, totalClustersWeight int, direction Direction) *xds_route.Route {
route := xds_route.Route{
Match: &xds_route.RouteMatch{
PathSpecifier: &xds_route.RouteMatch_SafeRegex{
SafeRegex: &xds_matcher.RegexMatcher{
EngineType: &xds_matcher.RegexMatcher_GoogleRe2{GoogleRe2: &xds_matcher.RegexMatcher_GoogleRE2{}},
Regex: pathRegex,
},
},
Headers: getHeadersForRoute(method, headersMap),
},
Action: &xds_route.Route_Route{
Route: &xds_route.RouteAction{
ClusterSpecifier: &xds_route.RouteAction_WeightedClusters{
WeightedClusters: getWeightedCluster(weightedClusters, totalClustersWeight, direction),
},
},
},
}
return &route
}
func getHeadersForRoute(method string, headersMap map[string]string) []*xds_route.HeaderMatcher {
var headers []*xds_route.HeaderMatcher
// add methods header
methodsHeader := xds_route.HeaderMatcher{
Name: MethodHeaderKey,
HeaderMatchSpecifier: &xds_route.HeaderMatcher_SafeRegexMatch{
SafeRegexMatch: &xds_matcher.RegexMatcher{
EngineType: &xds_matcher.RegexMatcher_GoogleRe2{GoogleRe2: &xds_matcher.RegexMatcher_GoogleRE2{}},
Regex: getRegexForMethod(method),
},
},
}
headers = append(headers, &methodsHeader)
// add all other custom headers
for headerKey, headerValue := range headersMap {
// omit the host header as we have already configured this
if headerKey == httpHostHeader {
continue
}
header := xds_route.HeaderMatcher{
Name: headerKey,
HeaderMatchSpecifier: &xds_route.HeaderMatcher_SafeRegexMatch{
SafeRegexMatch: &xds_matcher.RegexMatcher{
EngineType: &xds_matcher.RegexMatcher_GoogleRe2{GoogleRe2: &xds_matcher.RegexMatcher_GoogleRE2{}},
Regex: headerValue,
},
},
}
headers = append(headers, &header)
}
return headers
}
func getWeightedCluster(weightedClusters set.Set, totalClustersWeight int, direction Direction) *xds_route.WeightedCluster {
var wc xds_route.WeightedCluster
var total int
for clusterInterface := range weightedClusters.Iter() {
cluster := clusterInterface.(service.WeightedCluster)
clusterName := string(cluster.ClusterName)
total += cluster.Weight
if direction == InboundRoute {
// An inbound route is associated with a local cluster. The inbound route is applied
// on the destination cluster, and the destination clusters that accept inbound
// traffic have the name of the form 'someClusterName-local`.
clusterName += envoy.LocalClusterSuffix
}
wc.Clusters = append(wc.Clusters, &xds_route.WeightedCluster_ClusterWeight{
Name: clusterName,
Weight: &wrappers.UInt32Value{Value: uint32(cluster.Weight)},
})
}
if direction == OutboundRoute {
// For an outbound route from the source, the pre-computed total weight based on the weights defined in
// the traffic split policies are used.
total = totalClustersWeight
}
wc.TotalWeight = &wrappers.UInt32Value{Value: uint32(total)}
sort.Stable(clusterWeightByName(wc.Clusters))
return &wc
}
// This method gets a list of all the distinct upstream clusters for a domain
// needed to configure source service's weighted routes
func getDistinctWeightedClusters(routePolicyWeightedClustersMap map[string]trafficpolicy.RouteWeightedClusters) set.Set {
weightedClusters := set.NewSet()
for _, perRouteWeightedClusters := range routePolicyWeightedClustersMap {
if weightedClusters.Cardinality() == 0 {
weightedClusters = perRouteWeightedClusters.WeightedClusters
}
weightedClusters.Union(perRouteWeightedClusters.WeightedClusters)
}
return weightedClusters
}
// This method gets a list of all the distinct domains for a host
// needed to configure virtual hosts
func getDistinctDomains(routePolicyWeightedClustersMap map[string]trafficpolicy.RouteWeightedClusters) set.Set {
domains := set.NewSet()
for _, perRouteWeightedClusters := range routePolicyWeightedClustersMap {
if domains.Cardinality() == 0 {
domains = perRouteWeightedClusters.Hostnames
}
domains.Union(perRouteWeightedClusters.Hostnames)
}
return domains
}
func getTotalWeightForClusters(weightedClusters set.Set) int {
var totalWeight int
for clusterInterface := range weightedClusters.Iter() {
cluster := clusterInterface.(service.WeightedCluster)
totalWeight += cluster.Weight
}
return totalWeight
}
type clusterWeightByName []*xds_route.WeightedCluster_ClusterWeight
func (c clusterWeightByName) Len() int { return len(c) }
func (c clusterWeightByName) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c clusterWeightByName) Less(i, j int) bool {
if c[i].Name == c[j].Name {
return c[i].Weight.Value < c[j].Weight.Value
}
return c[i].Name < c[j].Name
}
// sanitizeHTTPMethods takes in a list of HTTP methods including a wildcard (*) and returns a wildcard if any of
// the methods is a wildcard or sanitizes the input list to avoid duplicates.
func sanitizeHTTPMethods(allowedMethods []string) []string {
var newAllowedMethods []string
keys := make(map[string]interface{})
for _, method := range allowedMethods {
if method != "" {
if method == constants.WildcardHTTPMethod {
newAllowedMethods = []string{constants.WildcardHTTPMethod}
return newAllowedMethods
}
if _, value := keys[method]; !value {
keys[method] = nil
newAllowedMethods = append(newAllowedMethods, method)
}
}
}
return newAllowedMethods
}
//NewRouteConfigurationStub creates the route configuration placeholder
func NewRouteConfigurationStub(routeConfigName string) *xds_route.RouteConfiguration {
routeConfiguration := xds_route.RouteConfiguration{
Name: routeConfigName,
ValidateClusters: &wrappers.BoolValue{Value: true},
}
return &routeConfiguration
}
func getRegexForMethod(httpMethod string) string {
methodRegex := httpMethod
if httpMethod == constants.WildcardHTTPMethod {
methodRegex = constants.RegexMatchAll
}
return methodRegex
}