-
Notifications
You must be signed in to change notification settings - Fork 294
/
listener.go
262 lines (230 loc) · 8.33 KB
/
listener.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
// 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 (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/ir"
"github.com/envoyproxy/gateway/internal/utils"
"github.com/envoyproxy/gateway/internal/utils/naming"
)
var _ ListenersTranslator = (*Translator)(nil)
type ListenersTranslator interface {
ProcessListeners(gateways []*GatewayContext, xdsIR XdsIRMap, infraIR InfraIRMap, resources *Resources)
}
func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR XdsIRMap, infraIR InfraIRMap, resources *Resources) {
// Infra IR proxy ports must be unique.
foundPorts := make(map[string][]*protocolPort)
t.validateConflictedLayer7Listeners(gateways)
t.validateConflictedLayer4Listeners(gateways, gwapiv1.TCPProtocolType, gwapiv1.TLSProtocolType)
t.validateConflictedLayer4Listeners(gateways, gwapiv1.UDPProtocolType)
if t.MergeGateways {
t.validateConflictedMergedListeners(gateways)
}
// Iterate through all listeners to validate spec
// and compute status for each, and add valid ones
// to the Xds IR.
for _, gateway := range gateways {
irKey := t.getIRKey(gateway.Gateway)
if resources.EnvoyProxy != nil {
infraIR[irKey].Proxy.Config = resources.EnvoyProxy
}
xdsIR[irKey].AccessLog = processAccessLog(infraIR[irKey].Proxy.Config)
xdsIR[irKey].Tracing = processTracing(gateway.Gateway, infraIR[irKey].Proxy.Config)
xdsIR[irKey].Metrics = processMetrics(infraIR[irKey].Proxy.Config)
for _, listener := range gateway.listeners {
// Process protocol & supported kinds
switch listener.Protocol {
case gwapiv1.TLSProtocolType:
if listener.TLS != nil {
switch *listener.TLS.Mode {
case gwapiv1.TLSModePassthrough:
t.validateAllowedRoutes(listener, KindTLSRoute)
case gwapiv1.TLSModeTerminate:
t.validateAllowedRoutes(listener, KindTCPRoute)
default:
t.validateAllowedRoutes(listener, KindTCPRoute, KindTLSRoute)
}
} else {
t.validateAllowedRoutes(listener, KindTCPRoute, KindTLSRoute)
}
case gwapiv1.HTTPProtocolType, gwapiv1.HTTPSProtocolType:
t.validateAllowedRoutes(listener, KindHTTPRoute, KindGRPCRoute)
case gwapiv1.TCPProtocolType:
t.validateAllowedRoutes(listener, KindTCPRoute)
case gwapiv1.UDPProtocolType:
t.validateAllowedRoutes(listener, KindUDPRoute)
default:
listener.SetCondition(
gwapiv1.ListenerConditionAccepted,
metav1.ConditionFalse,
gwapiv1.ListenerReasonUnsupportedProtocol,
fmt.Sprintf("Protocol %s is unsupported, must be %s, %s, %s or %s.", listener.Protocol,
gwapiv1.HTTPProtocolType, gwapiv1.HTTPSProtocolType, gwapiv1.TCPProtocolType, gwapiv1.UDPProtocolType),
)
}
// Validate allowed namespaces
t.validateAllowedNamespaces(listener)
// Process TLS configuration
t.validateTLSConfiguration(listener, resources)
// Process Hostname configuration
t.validateHostName(listener)
// Process conditions and check if the listener is ready
isReady := t.validateListenerConditions(listener)
if !isReady {
continue
}
// Add the listener to the Xds IR
servicePort := &protocolPort{protocol: listener.Protocol, port: int32(listener.Port)}
containerPort := servicePortToContainerPort(servicePort.port)
switch listener.Protocol {
case gwapiv1.HTTPProtocolType, gwapiv1.HTTPSProtocolType:
irListener := &ir.HTTPListener{
Name: irHTTPListenerName(listener),
Address: "0.0.0.0",
Port: uint32(containerPort),
TLS: irTLSConfigs(listener.tlsSecrets),
Path: ir.PathSettings{
MergeSlashes: true,
EscapedSlashesAction: ir.UnescapeAndRedirect,
},
}
if listener.Hostname != nil {
irListener.Hostnames = append(irListener.Hostnames, string(*listener.Hostname))
} else {
// Hostname specifies the virtual hostname to match for protocol types that define this concept.
// When unspecified, all hostnames are matched. This field is ignored for protocols that don’t require hostname based matching.
// see more https://gateway-api.sigs.k8s.io/references/spec/#gateway.networking.k8s.io/gwapiv1.Listener.
irListener.Hostnames = append(irListener.Hostnames, "*")
}
xdsIR[irKey].HTTP = append(xdsIR[irKey].HTTP, irListener)
}
// Add the listener to the Infra IR. Infra IR ports must have a unique port number per layer-4 protocol
// (TCP or UDP).
if !containsPort(foundPorts[irKey], servicePort) {
t.processInfraIRListener(listener, infraIR, irKey, servicePort)
foundPorts[irKey] = append(foundPorts[irKey], servicePort)
}
}
}
}
func (t *Translator) processInfraIRListener(listener *ListenerContext, infraIR InfraIRMap, irKey string, servicePort *protocolPort) {
var proto ir.ProtocolType
switch listener.Protocol {
case gwapiv1.HTTPProtocolType:
proto = ir.HTTPProtocolType
case gwapiv1.HTTPSProtocolType:
proto = ir.HTTPSProtocolType
case gwapiv1.TLSProtocolType:
proto = ir.TLSProtocolType
case gwapiv1.TCPProtocolType:
proto = ir.TCPProtocolType
case gwapiv1.UDPProtocolType:
proto = ir.UDPProtocolType
}
infraPortName := string(listener.Name)
if t.MergeGateways {
infraPortName = irHTTPListenerName(listener)
}
infraPort := ir.ListenerPort{
Name: infraPortName,
Protocol: proto,
ServicePort: servicePort.port,
ContainerPort: servicePortToContainerPort(servicePort.port),
}
proxyListener := &ir.ProxyListener{
Name: irHTTPListenerName(listener),
Ports: []ir.ListenerPort{infraPort},
}
infraIR[irKey].Proxy.Listeners = append(infraIR[irKey].Proxy.Listeners, proxyListener)
}
func processAccessLog(envoyproxy *egv1a1.EnvoyProxy) *ir.AccessLog {
if envoyproxy == nil ||
envoyproxy.Spec.Telemetry == nil ||
envoyproxy.Spec.Telemetry.AccessLog == nil ||
(!envoyproxy.Spec.Telemetry.AccessLog.Disable && len(envoyproxy.Spec.Telemetry.AccessLog.Settings) == 0) {
// use the default access log
return &ir.AccessLog{
Text: []*ir.TextAccessLog{
{
Path: "/dev/stdout",
},
},
}
}
if envoyproxy.Spec.Telemetry.AccessLog.Disable {
return nil
}
irAccessLog := &ir.AccessLog{}
// translate the access log configuration to the IR
for _, accessLog := range envoyproxy.Spec.Telemetry.AccessLog.Settings {
for _, sink := range accessLog.Sinks {
switch sink.Type {
case egv1a1.ProxyAccessLogSinkTypeFile:
if sink.File == nil {
continue
}
switch accessLog.Format.Type {
case egv1a1.ProxyAccessLogFormatTypeText:
al := &ir.TextAccessLog{
Format: accessLog.Format.Text,
Path: sink.File.Path,
}
irAccessLog.Text = append(irAccessLog.Text, al)
case egv1a1.ProxyAccessLogFormatTypeJSON:
if len(accessLog.Format.JSON) == 0 {
// TODO: use a default JSON format if not specified?
continue
}
al := &ir.JSONAccessLog{
JSON: accessLog.Format.JSON,
Path: sink.File.Path,
}
irAccessLog.JSON = append(irAccessLog.JSON, al)
}
case egv1a1.ProxyAccessLogSinkTypeOpenTelemetry:
if sink.OpenTelemetry == nil {
continue
}
al := &ir.OpenTelemetryAccessLog{
Port: uint32(sink.OpenTelemetry.Port),
Host: sink.OpenTelemetry.Host,
Resources: sink.OpenTelemetry.Resources,
}
switch accessLog.Format.Type {
case egv1a1.ProxyAccessLogFormatTypeJSON:
al.Attributes = accessLog.Format.JSON
case egv1a1.ProxyAccessLogFormatTypeText:
al.Text = accessLog.Format.Text
}
irAccessLog.OpenTelemetry = append(irAccessLog.OpenTelemetry, al)
}
}
}
return irAccessLog
}
func processTracing(gw *gwapiv1.Gateway, envoyproxy *egv1a1.EnvoyProxy) *ir.Tracing {
if envoyproxy == nil ||
envoyproxy.Spec.Telemetry == nil ||
envoyproxy.Spec.Telemetry.Tracing == nil {
return nil
}
return &ir.Tracing{
ServiceName: naming.ServiceName(utils.NamespacedName(gw)),
ProxyTracing: *envoyproxy.Spec.Telemetry.Tracing,
}
}
func processMetrics(envoyproxy *egv1a1.EnvoyProxy) *ir.Metrics {
if envoyproxy == nil ||
envoyproxy.Spec.Telemetry == nil ||
envoyproxy.Spec.Telemetry.Metrics == nil {
return nil
}
return &ir.Metrics{
EnableVirtualHostStats: envoyproxy.Spec.Telemetry.Metrics.EnableVirtualHostStats,
}
}