-
Notifications
You must be signed in to change notification settings - Fork 347
/
translator.go
351 lines (288 loc) · 11.6 KB
/
translator.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
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.
package gatewayapi
import (
"sort"
"golang.org/x/exp/maps"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/utils/ptr"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gwapiv1a3 "sigs.k8s.io/gateway-api/apis/v1alpha3"
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/gatewayapi/resource"
"github.com/envoyproxy/gateway/internal/ir"
"github.com/envoyproxy/gateway/internal/wasm"
)
const (
GroupMultiClusterService = "multicluster.x-k8s.io"
// OwningGatewayNamespaceLabel is the owner reference label used for managed infra.
// The value should be the namespace of the accepted Envoy Gateway.
OwningGatewayNamespaceLabel = "gateway.envoyproxy.io/owning-gateway-namespace"
OwningGatewayClassLabel = "gateway.envoyproxy.io/owning-gatewayclass"
// OwningGatewayNameLabel is the owner reference label used for managed infra.
// The value should be the name of the accepted Envoy Gateway.
OwningGatewayNameLabel = "gateway.envoyproxy.io/owning-gateway-name"
// minEphemeralPort is the first port in the ephemeral port range.
minEphemeralPort = 1024
// wellKnownPortShift is the constant added to the well known port (1-1023)
// to convert it into an ephemeral port.
wellKnownPortShift = 10000
)
var _ TranslatorManager = (*Translator)(nil)
type TranslatorManager interface {
Translate(resources *resource.Resources) (*TranslateResult, error)
GetRelevantGateways(resources *resource.Resources) []*GatewayContext
RoutesTranslator
ListenersTranslator
AddressesTranslator
FiltersTranslator
}
// Translator translates Gateway API resources to IRs and computes status
// for Gateway API resources.
type Translator struct {
// GatewayControllerName is the name of the Gateway API controller
GatewayControllerName string
// GatewayClassName is the name of the GatewayClass
// to process Gateways for.
GatewayClassName gwapiv1.ObjectName
// GlobalRateLimitEnabled is true when global
// ratelimiting has been configured by the admin.
GlobalRateLimitEnabled bool
// EndpointRoutingDisabled can be set to true to use
// the Service Cluster IP for routing to the backend
// instead.
EndpointRoutingDisabled bool
// MergeGateways is true when all Gateway Listeners
// should be merged under the parent GatewayClass.
MergeGateways bool
// EnvoyPatchPolicyEnabled when the EnvoyPatchPolicy
// feature is enabled.
EnvoyPatchPolicyEnabled bool
// BackendEnabled when the Backend feature is enabled.
BackendEnabled bool
// ExtensionGroupKinds stores the group/kind for all resources
// introduced by an Extension so that the translator can
// store referenced resources in the IR for later use.
ExtensionGroupKinds []schema.GroupKind
// Namespace is the namespace that Envoy Gateway runs in.
Namespace string
// WasmCache is the cache for Wasm modules.
WasmCache wasm.Cache
}
type TranslateResult struct {
resource.Resources
XdsIR resource.XdsIRMap `json:"xdsIR" yaml:"xdsIR"`
InfraIR resource.InfraIRMap `json:"infraIR" yaml:"infraIR"`
}
func newTranslateResult(gateways []*GatewayContext,
httpRoutes []*HTTPRouteContext,
grpcRoutes []*GRPCRouteContext,
tlsRoutes []*TLSRouteContext,
tcpRoutes []*TCPRouteContext,
udpRoutes []*UDPRouteContext,
clientTrafficPolicies []*egv1a1.ClientTrafficPolicy,
backendTrafficPolicies []*egv1a1.BackendTrafficPolicy,
securityPolicies []*egv1a1.SecurityPolicy,
backendTLSPolicies []*gwapiv1a3.BackendTLSPolicy,
envoyExtensionPolicies []*egv1a1.EnvoyExtensionPolicy,
extPolicies []unstructured.Unstructured,
backends []*egv1a1.Backend,
xdsIR resource.XdsIRMap, infraIR resource.InfraIRMap,
) *TranslateResult {
translateResult := &TranslateResult{
XdsIR: xdsIR,
InfraIR: infraIR,
}
for _, gateway := range gateways {
translateResult.Gateways = append(translateResult.Gateways, gateway.Gateway)
}
for _, httpRoute := range httpRoutes {
translateResult.HTTPRoutes = append(translateResult.HTTPRoutes, httpRoute.HTTPRoute)
}
for _, grpcRoute := range grpcRoutes {
translateResult.GRPCRoutes = append(translateResult.GRPCRoutes, grpcRoute.GRPCRoute)
}
for _, tlsRoute := range tlsRoutes {
translateResult.TLSRoutes = append(translateResult.TLSRoutes, tlsRoute.TLSRoute)
}
for _, tcpRoute := range tcpRoutes {
translateResult.TCPRoutes = append(translateResult.TCPRoutes, tcpRoute.TCPRoute)
}
for _, udpRoute := range udpRoutes {
translateResult.UDPRoutes = append(translateResult.UDPRoutes, udpRoute.UDPRoute)
}
translateResult.ClientTrafficPolicies = append(translateResult.ClientTrafficPolicies, clientTrafficPolicies...)
translateResult.BackendTrafficPolicies = append(translateResult.BackendTrafficPolicies, backendTrafficPolicies...)
translateResult.SecurityPolicies = append(translateResult.SecurityPolicies, securityPolicies...)
translateResult.BackendTLSPolicies = append(translateResult.BackendTLSPolicies, backendTLSPolicies...)
translateResult.EnvoyExtensionPolicies = append(translateResult.EnvoyExtensionPolicies, envoyExtensionPolicies...)
translateResult.ExtensionServerPolicies = append(translateResult.ExtensionServerPolicies, extPolicies...)
translateResult.Backends = append(translateResult.Backends, backends...)
return translateResult
}
func (t *Translator) Translate(resources *resource.Resources) (*TranslateResult, error) {
// Get Gateways belonging to our GatewayClass.
gateways := t.GetRelevantGateways(resources)
// Sort gateways based on timestamp.
sort.Slice(gateways, func(i, j int) bool {
return gateways[i].CreationTimestamp.Before(&(gateways[j].CreationTimestamp))
})
// Build IR maps.
xdsIR, infraIR := t.InitIRs(gateways)
// Process all Listeners for all relevant Gateways.
t.ProcessListeners(gateways, xdsIR, infraIR, resources)
// Process EnvoyPatchPolicies
t.ProcessEnvoyPatchPolicies(resources.EnvoyPatchPolicies, xdsIR)
// Process all Addresses for all relevant Gateways.
t.ProcessAddresses(gateways, xdsIR, infraIR)
// process all Backends
backends := t.ProcessBackends(resources.Backends)
// Process all relevant HTTPRoutes.
httpRoutes := t.ProcessHTTPRoutes(resources.HTTPRoutes, gateways, resources, xdsIR)
// Process all relevant GRPCRoutes.
grpcRoutes := t.ProcessGRPCRoutes(resources.GRPCRoutes, gateways, resources, xdsIR)
// Process all relevant TLSRoutes.
tlsRoutes := t.ProcessTLSRoutes(resources.TLSRoutes, gateways, resources, xdsIR)
// Process all relevant TCPRoutes.
tcpRoutes := t.ProcessTCPRoutes(resources.TCPRoutes, gateways, resources, xdsIR)
// Process all relevant UDPRoutes.
udpRoutes := t.ProcessUDPRoutes(resources.UDPRoutes, gateways, resources, xdsIR)
// Process ClientTrafficPolicies
clientTrafficPolicies := t.ProcessClientTrafficPolicies(resources, gateways, xdsIR, infraIR)
// Process BackendTrafficPolicies
routes := []RouteContext{}
for _, h := range httpRoutes {
routes = append(routes, h)
}
for _, g := range grpcRoutes {
routes = append(routes, g)
}
for _, t := range tlsRoutes {
routes = append(routes, t)
}
for _, t := range tcpRoutes {
routes = append(routes, t)
}
for _, u := range udpRoutes {
routes = append(routes, u)
}
// Process BackendTrafficPolicies
backendTrafficPolicies := t.ProcessBackendTrafficPolicies(
resources, gateways, routes, xdsIR)
// Process SecurityPolicies
securityPolicies := t.ProcessSecurityPolicies(
resources.SecurityPolicies, gateways, routes, resources, xdsIR)
// Process EnvoyExtensionPolicies
envoyExtensionPolicies := t.ProcessEnvoyExtensionPolicies(
resources.EnvoyExtensionPolicies, gateways, routes, resources, xdsIR)
extServerPolicies, translateErrs := t.ProcessExtensionServerPolicies(
resources.ExtensionServerPolicies, gateways, xdsIR)
// Sort xdsIR based on the Gateway API spec
sortXdsIRMap(xdsIR)
// Set custom filter order if EnvoyProxy is set
// The custom filter order will be applied when generating the HTTP filter chain.
for _, gateway := range gateways {
if gateway.envoyProxy != nil {
irKey := t.getIRKey(gateway.Gateway)
xdsIR[irKey].FilterOrder = gateway.envoyProxy.Spec.FilterOrder
}
}
return newTranslateResult(gateways, httpRoutes, grpcRoutes, tlsRoutes,
tcpRoutes, udpRoutes, clientTrafficPolicies, backendTrafficPolicies,
securityPolicies, resources.BackendTLSPolicies, envoyExtensionPolicies,
extServerPolicies, backends, xdsIR, infraIR), translateErrs
}
// GetRelevantGateways returns GatewayContexts, containing a copy of the original
// Gateway with the Listener statuses reset.
func (t *Translator) GetRelevantGateways(resources *resource.Resources) []*GatewayContext {
var relevant []*GatewayContext
for _, gateway := range resources.Gateways {
if gateway == nil {
panic("received nil gateway")
}
if gateway.Spec.GatewayClassName == t.GatewayClassName {
gc := &GatewayContext{
Gateway: gateway.DeepCopy(),
}
gc.ResetListeners(resources)
relevant = append(relevant, gc)
}
}
return relevant
}
// InitIRs checks if mergeGateways is enabled in EnvoyProxy config and initializes XdsIR and InfraIR maps with adequate keys.
func (t *Translator) InitIRs(gateways []*GatewayContext) (map[string]*ir.Xds, map[string]*ir.Infra) {
xdsIR := make(resource.XdsIRMap)
infraIR := make(resource.InfraIRMap)
var irKey string
for _, gateway := range gateways {
gwXdsIR := &ir.Xds{}
gwInfraIR := ir.NewInfra()
labels := infrastructureLabels(gateway.Gateway)
annotations := infrastructureAnnotations(gateway.Gateway)
gwInfraIR.Proxy.GetProxyMetadata().Annotations = annotations
if t.MergeGateways {
irKey = string(t.GatewayClassName)
maps.Copy(labels, GatewayClassOwnerLabel(string(t.GatewayClassName)))
gwInfraIR.Proxy.GetProxyMetadata().Labels = labels
} else {
irKey = irStringKey(gateway.Gateway.Namespace, gateway.Gateway.Name)
maps.Copy(labels, GatewayOwnerLabels(gateway.Namespace, gateway.Name))
gwInfraIR.Proxy.GetProxyMetadata().Labels = labels
}
gwInfraIR.Proxy.Name = irKey
// save the IR references in the map before the translation starts
xdsIR[irKey] = gwXdsIR
infraIR[irKey] = gwInfraIR
}
return xdsIR, infraIR
}
// IsEnvoyServiceRouting returns true if EnvoyProxy.Spec.RoutingType == ServiceRoutingType
// or, alternatively, if Translator.EndpointRoutingDisabled has been explicitly set to true;
// otherwise, it returns false.
func (t *Translator) IsEnvoyServiceRouting(r *egv1a1.EnvoyProxy) bool {
if t.EndpointRoutingDisabled {
return true
}
if r == nil {
return false
}
switch ptr.Deref(r.Spec.RoutingType, egv1a1.EndpointRoutingType) {
case egv1a1.ServiceRoutingType:
return true
case egv1a1.EndpointRoutingType:
return false
default:
return false
}
}
func infrastructureAnnotations(gtw *gwapiv1.Gateway) map[string]string {
if gtw.Spec.Infrastructure != nil && len(gtw.Spec.Infrastructure.Annotations) > 0 {
res := make(map[string]string)
for k, v := range gtw.Spec.Infrastructure.Annotations {
res[string(k)] = string(v)
}
return res
}
return nil
}
func infrastructureLabels(gtw *gwapiv1.Gateway) map[string]string {
res := make(map[string]string)
if gtw.Spec.Infrastructure != nil {
for k, v := range gtw.Spec.Infrastructure.Labels {
res[string(k)] = string(v)
}
}
return res
}
// XdsIR and InfraIR map keys by default are {GatewayNamespace}/{GatewayName}, but if mergeGateways is set, they are merged under {GatewayClassName} key.
func (t *Translator) getIRKey(gateway *gwapiv1.Gateway) string {
irKey := irStringKey(gateway.Namespace, gateway.Name)
if t.MergeGateways {
return string(t.GatewayClassName)
}
return irKey
}