generated from cloudwego/.github
/
rds.go
216 lines (196 loc) · 5.96 KB
/
rds.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
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xdsresource
import (
"encoding/json"
"fmt"
"time"
v3routepb "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
"github.com/golang/protobuf/ptypes/any"
"google.golang.org/protobuf/proto"
)
// RouteConfigResource is used for routing
// HTTPRouteConfig is the native http route config, which consists of a list of virtual hosts.
// ThriftRouteConfig is converted from the routeConfiguration in thrift proxy, which can only be configured in the listener filter
// For details of thrift_proxy, please refer to: https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/filters/network/thrift_proxy/v3/route.proto#envoy-v3-api-msg-extensions-filters-network-thrift-proxy-v3-routeconfiguration
type RouteConfigResource struct {
HTTPRouteConfig *HTTPRouteConfig
ThriftRouteConfig *ThriftRouteConfig
}
type HTTPRouteConfig struct {
VirtualHosts []*VirtualHost
}
type ThriftRouteConfig struct {
Routes []*Route
}
type VirtualHost struct {
Name string
Routes []*Route
}
type WeightedCluster struct {
Name string
Weight uint32
}
type Route struct {
Match RouteMatch
WeightedClusters []*WeightedCluster
Timeout time.Duration
}
type RouteMatch interface {
MatchPath(path string) bool
// default use headers to match the meta.
MatchMeta(map[string]string) bool
}
type HTTPRouteMatch struct {
Path string
Prefix string
Headers Matchers
}
type ThriftRouteMatch struct {
Method string
ServiceName string
Tags Matchers
}
func (tm *ThriftRouteMatch) MatchPath(path string) bool {
if tm.Method != "" && tm.Method != path {
return false
}
return true
}
func (tm *ThriftRouteMatch) MatchMeta(md map[string]string) bool {
return tm.Tags.Match(md)
}
func (rm *HTTPRouteMatch) MatchPath(path string) bool {
if rm.Path != "" {
return rm.Path == path
}
// default prefix
return rm.Prefix == "/"
}
func (rm *HTTPRouteMatch) MatchMeta(md map[string]string) bool {
return rm.Headers.Match(md)
}
func (r *Route) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Match RouteMatch `json:"Match"`
WeightedClusters []*WeightedCluster `json:"Weighted_Clusters"`
Timeout string `json:"Timeout"`
}{
Match: r.Match,
WeightedClusters: r.WeightedClusters,
Timeout: r.Timeout.String(),
})
}
func unmarshalRoutes(rs []*v3routepb.Route) ([]*Route, error) {
routes := make([]*Route, len(rs))
for i, r := range rs {
route := &Route{}
routeMatch := &HTTPRouteMatch{}
match := r.GetMatch()
if match == nil {
return nil, fmt.Errorf("no match in route %s", r.GetName())
}
// path match
pathSpecifier := match.GetPathSpecifier()
// only support exact match for path
switch p := pathSpecifier.(type) {
// default route if virtual service is not configured
case *v3routepb.RouteMatch_Prefix:
routeMatch.Prefix = p.Prefix
case *v3routepb.RouteMatch_Path:
routeMatch.Path = p.Path
// default:
// return nil, fmt.Errorf("only support path match")
}
routeMatch.Headers = BuildMatchers(match.GetHeaders())
route.Match = routeMatch
// action
action := r.GetAction()
if action == nil {
return nil, fmt.Errorf("no action in route %s", r.GetName())
}
switch a := action.(type) {
case *v3routepb.Route_Route:
switch cs := a.Route.GetClusterSpecifier().(type) {
case *v3routepb.RouteAction_Cluster:
route.WeightedClusters = []*WeightedCluster{
{Name: cs.Cluster, Weight: 1},
}
case *v3routepb.RouteAction_WeightedClusters:
wcs := cs.WeightedClusters
clusters := make([]*WeightedCluster, len(wcs.Clusters))
for j, wc := range wcs.GetClusters() {
clusters[j] = &WeightedCluster{
Name: wc.GetName(),
Weight: wc.GetWeight().GetValue(),
}
}
route.WeightedClusters = clusters
}
route.Timeout = a.Route.GetTimeout().AsDuration()
}
routes[i] = route
}
return routes, nil
}
// unmarshalRouteConfig unmarshalls the RouteConfigResource, which only support HTTP route.
func unmarshalRouteConfig(routeConfig *v3routepb.RouteConfiguration) (*RouteConfigResource, error) {
vhs := routeConfig.GetVirtualHosts()
virtualHosts := make([]*VirtualHost, len(vhs))
for i := 0; i < len(vhs); i++ {
rs := vhs[i].GetRoutes()
routes, err := unmarshalRoutes(rs)
if err != nil {
return nil, fmt.Errorf("processing route in virtual host %s failed: %s", vhs[i].GetName(), err)
}
virtualHost := &VirtualHost{
Name: vhs[i].GetName(),
Routes: routes,
}
virtualHosts[i] = virtualHost
}
return &RouteConfigResource{
HTTPRouteConfig: &HTTPRouteConfig{virtualHosts},
}, nil
}
func UnmarshalRDS(rawResources []*any.Any) (map[string]Resource, error) {
ret := make(map[string]Resource, len(rawResources))
errMap := make(map[string]error)
var errSlice []error
for _, r := range rawResources {
if r.GetTypeUrl() != RouteTypeURL {
err := fmt.Errorf("invalid route config resource type: %s", r.GetTypeUrl())
errSlice = append(errSlice, err)
continue
}
rcfg := &v3routepb.RouteConfiguration{}
if err := proto.Unmarshal(r.GetValue(), rcfg); err != nil {
err = fmt.Errorf("unmarshal failed: %s", err)
errSlice = append(errSlice, err)
continue
}
res, err := unmarshalRouteConfig(rcfg)
if err != nil {
errMap[rcfg.Name] = err
continue
}
ret[rcfg.Name] = res
}
if len(errMap) == 0 && len(errSlice) == 0 {
return ret, nil
}
return ret, processUnmarshalErrors(errSlice, errMap)
}