-
Notifications
You must be signed in to change notification settings - Fork 297
/
translator.go
171 lines (137 loc) · 5.35 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
// 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 (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/gateway-api/apis/v1beta1"
)
const (
KindEnvoyProxy = "EnvoyProxy"
KindGateway = "Gateway"
KindGatewayClass = "GatewayClass"
KindGRPCRoute = "GRPCRoute"
KindHTTPRoute = "HTTPRoute"
KindNamespace = "Namespace"
KindTLSRoute = "TLSRoute"
KindTCPRoute = "TCPRoute"
KindUDPRoute = "UDPRoute"
KindService = "Service"
KindSecret = "Secret"
// 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"
// 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 *Resources) *TranslateResult
GetRelevantGateways(gateways []*v1beta1.Gateway) []*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 v1beta1.ObjectName
// GlobalRateLimitEnabled is true when global
// ratelimiting has been configured by the admin.
GlobalRateLimitEnabled 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
}
type TranslateResult struct {
Resources
XdsIR XdsIRMap `json:"xdsIR" yaml:"xdsIR"`
InfraIR InfraIRMap `json:"infraIR" yaml:"infraIR"`
}
func newTranslateResult(gateways []*GatewayContext,
httpRoutes []*HTTPRouteContext,
grpcRoutes []*GRPCRouteContext,
tlsRoutes []*TLSRouteContext,
tcpRoutes []*TCPRouteContext,
udpRoutes []*UDPRouteContext,
xdsIR XdsIRMap, infraIR 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)
}
return translateResult
}
func (t *Translator) Translate(resources *Resources) *TranslateResult {
xdsIR := make(XdsIRMap)
infraIR := make(InfraIRMap)
// Get Gateways belonging to our GatewayClass.
gateways := t.GetRelevantGateways(resources.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, resources)
// 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)
// Sort xdsIR based on the Gateway API spec
sortXdsIRMap(xdsIR)
return newTranslateResult(gateways, httpRoutes, grpcRoutes, tlsRoutes, tcpRoutes, udpRoutes, xdsIR, infraIR)
}
// GetRelevantGateways returns GatewayContexts, containing a copy of the original
// Gateway with the Listener statuses reset.
func (t *Translator) GetRelevantGateways(gateways []*v1beta1.Gateway) []*GatewayContext {
var relevant []*GatewayContext
for _, gateway := range gateways {
if gateway == nil {
panic("received nil gateway")
}
if gateway.Spec.GatewayClassName == t.GatewayClassName {
gc := &GatewayContext{
Gateway: gateway.DeepCopy(),
}
gc.ResetListeners()
relevant = append(relevant, gc)
}
}
return relevant
}