-
Notifications
You must be signed in to change notification settings - Fork 682
/
gw_transforms.go
238 lines (212 loc) · 7.21 KB
/
gw_transforms.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
package gateway
import (
// standard library
"fmt"
// third-party libraries
"github.com/pkg/errors"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/wrapperspb"
gw "sigs.k8s.io/gateway-api/apis/v1alpha1"
// envoy api v2
apiv2 "github.com/datawire/ambassador/v2/pkg/api/envoy/api/v2"
apiv2_core "github.com/datawire/ambassador/v2/pkg/api/envoy/api/v2/core"
apiv2_listener "github.com/datawire/ambassador/v2/pkg/api/envoy/api/v2/listener"
apiv2_route "github.com/datawire/ambassador/v2/pkg/api/envoy/api/v2/route"
apiv2_httpman "github.com/datawire/ambassador/v2/pkg/api/envoy/config/filter/network/http_connection_manager/v2"
api_matcher "github.com/datawire/ambassador/v2/pkg/api/envoy/type/matcher"
// envoy control plane
ecp_wellknown "github.com/datawire/ambassador/v2/pkg/envoy-control-plane/wellknown"
// first-party libraries
"github.com/datawire/ambassador/v2/pkg/kates"
)
func Compile_Gateway(gateway *gw.Gateway) (*CompiledConfig, error) {
src := SourceFromResource(gateway)
var listeners []*CompiledListener
for idx, l := range gateway.Spec.Listeners {
name := fmt.Sprintf("%s-%d", getName(gateway), idx)
listener, err := Compile_Listener(src, l, name)
if err != nil {
return nil, err
}
listeners = append(listeners, listener)
}
return &CompiledConfig{
CompiledItem: NewCompiledItem(src),
Listeners: listeners,
}, nil
}
func Compile_Listener(parent Source, lst gw.Listener, name string) (*CompiledListener, error) {
hcm := &apiv2_httpman.HttpConnectionManager{
StatPrefix: name,
HttpFilters: []*apiv2_httpman.HttpFilter{
{Name: ecp_wellknown.CORS},
{Name: ecp_wellknown.Router},
},
RouteSpecifier: &apiv2_httpman.HttpConnectionManager_Rds{
Rds: &apiv2_httpman.Rds{
ConfigSource: &apiv2_core.ConfigSource{
ConfigSourceSpecifier: &apiv2_core.ConfigSource_Ads{
Ads: &apiv2_core.AggregatedConfigSource{},
},
},
RouteConfigName: name,
},
},
}
hcmAny, err := anypb.New(hcm)
if err != nil {
return nil, err
}
return &CompiledListener{
CompiledItem: NewCompiledItem(Sourcef("listener %s", lst.Hostname)),
Listener: &apiv2.Listener{
Name: name,
Address: &apiv2_core.Address{Address: &apiv2_core.Address_SocketAddress{SocketAddress: &apiv2_core.SocketAddress{
Address: "0.0.0.0",
PortSpecifier: &apiv2_core.SocketAddress_PortValue{PortValue: uint32(lst.Port)},
}}},
FilterChains: []*apiv2_listener.FilterChain{
{
Filters: []*apiv2_listener.Filter{
{
Name: ecp_wellknown.HTTPConnectionManager,
ConfigType: &apiv2_listener.Filter_TypedConfig{TypedConfig: hcmAny},
},
},
},
},
},
Predicate: func(route *CompiledRoute) bool {
return true
},
Domains: []string{"*"},
}, nil
}
func Compile_HTTPRoute(httpRoute *gw.HTTPRoute) (*CompiledConfig, error) {
src := SourceFromResource(httpRoute)
clusterRefs := []*ClusterRef{}
var routes []*apiv2_route.Route
for idx, rule := range httpRoute.Spec.Rules {
s := Sourcef("rule %d in %s", idx, src)
_routes, err := Compile_HTTPRouteRule(s, rule, httpRoute.Namespace, &clusterRefs)
if err != nil {
return nil, err
}
routes = append(routes, _routes...)
}
return &CompiledConfig{
CompiledItem: NewCompiledItem(src),
Routes: []*CompiledRoute{
{
CompiledItem: CompiledItem{Source: src, Namespace: httpRoute.Namespace},
HTTPRoute: httpRoute,
Routes: routes,
ClusterRefs: clusterRefs,
},
},
}, nil
}
func Compile_HTTPRouteRule(src Source, rule gw.HTTPRouteRule, namespace string, clusterRefs *[]*ClusterRef) ([]*apiv2_route.Route, error) {
var clusters []*apiv2_route.WeightedCluster_ClusterWeight
for idx, fwd := range rule.ForwardTo {
s := Sourcef("forwardTo %d in %s", idx, src)
clusters = append(clusters, Compile_HTTPRouteForwardTo(s, fwd, namespace, clusterRefs))
}
wc := &apiv2_route.WeightedCluster{Clusters: clusters}
matches, err := Compile_HTTPRouteMatches(rule.Matches)
if err != nil {
return nil, err
}
var result []*apiv2_route.Route
for _, match := range matches {
result = append(result, &apiv2_route.Route{
Match: match,
Action: &apiv2_route.Route_Route{Route: &apiv2_route.RouteAction{
ClusterSpecifier: &apiv2_route.RouteAction_WeightedClusters{WeightedClusters: wc},
}},
})
}
return result, err
}
func Compile_HTTPRouteForwardTo(src Source, forward gw.HTTPRouteForwardTo, namespace string, clusterRefs *[]*ClusterRef) *apiv2_route.WeightedCluster_ClusterWeight {
suffix := ""
clusterName := *forward.ServiceName
if forward.Port != nil {
suffix = fmt.Sprintf("/%d", *forward.Port)
clusterName = fmt.Sprintf("%s_%d", *forward.ServiceName, *forward.Port)
}
*clusterRefs = append(*clusterRefs, &ClusterRef{
CompiledItem: NewCompiledItem(src),
Name: clusterName,
EndpointPath: fmt.Sprintf("k8s/%s/%s%s", namespace, *forward.ServiceName, suffix),
})
return &apiv2_route.WeightedCluster_ClusterWeight{
Name: clusterName,
Weight: &wrapperspb.UInt32Value{Value: uint32(forward.Weight)},
}
}
func Compile_HTTPRouteMatches(matches []gw.HTTPRouteMatch) ([]*apiv2_route.RouteMatch, error) {
var result []*apiv2_route.RouteMatch
for _, match := range matches {
item, err := Compile_HTTPRouteMatch(match)
if err != nil {
return nil, err
}
result = append(result, item)
}
return result, nil
}
func Compile_HTTPRouteMatch(match gw.HTTPRouteMatch) (*apiv2_route.RouteMatch, error) {
headers, err := Compile_HTTPHeaderMatch(match.Headers)
if err != nil {
return nil, err
}
result := &apiv2_route.RouteMatch{
Headers: headers,
}
switch match.Path.Type {
case gw.PathMatchExact:
result.PathSpecifier = &apiv2_route.RouteMatch_Path{Path: match.Path.Value}
case gw.PathMatchPrefix:
result.PathSpecifier = &apiv2_route.RouteMatch_Prefix{Prefix: match.Path.Value}
case gw.PathMatchRegularExpression:
result.PathSpecifier = &apiv2_route.RouteMatch_SafeRegex{SafeRegex: regexMatcher(match.Path.Value)}
case "":
// no path match, but PathSpecifier is required
result.PathSpecifier = &apiv2_route.RouteMatch_Prefix{}
default:
return nil, errors.Errorf("unknown path match type: %q", match.Path.Type)
}
return result, nil
}
func Compile_HTTPHeaderMatch(headerMatch *gw.HTTPHeaderMatch) ([]*apiv2_route.HeaderMatcher, error) {
if headerMatch == nil {
return nil, nil
}
var result []*apiv2_route.HeaderMatcher
for hdr, pattern := range headerMatch.Values {
hm := &apiv2_route.HeaderMatcher{
Name: hdr,
InvertMatch: false,
}
switch headerMatch.Type {
case gw.HeaderMatchExact:
hm.HeaderMatchSpecifier = &apiv2_route.HeaderMatcher_ExactMatch{ExactMatch: pattern}
case gw.HeaderMatchRegularExpression:
hm.HeaderMatchSpecifier = &apiv2_route.HeaderMatcher_SafeRegexMatch{SafeRegexMatch: regexMatcher(pattern)}
default:
return nil, errors.Errorf("unknown header match type: %s", headerMatch.Type)
}
result = append(result, hm)
}
return result, nil
}
func regexMatcher(pattern string) *api_matcher.RegexMatcher {
return &api_matcher.RegexMatcher{
EngineType: &api_matcher.RegexMatcher_GoogleRe2{GoogleRe2: &api_matcher.RegexMatcher_GoogleRE2{}},
Regex: pattern,
}
}
func getName(resource kates.Object) string {
return fmt.Sprintf("%s-%s", resource.GetNamespace(), resource.GetName())
}