-
Notifications
You must be signed in to change notification settings - Fork 297
/
listener.go
232 lines (204 loc) · 7.65 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
// 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"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/gateway-api/apis/v1beta1"
configv1a1 "github.com/envoyproxy/gateway/api/config/v1alpha1"
"github.com/envoyproxy/gateway/internal/ir"
"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) {
t.validateConflictedLayer7Listeners(gateways)
t.validateConflictedLayer4Listeners(gateways, v1beta1.TCPProtocolType, v1beta1.TLSProtocolType)
t.validateConflictedLayer4Listeners(gateways, v1beta1.UDPProtocolType)
// Iterate through all listeners to validate spec
// and compute status for each, and add valid ones
// to the Xds IR.
for _, gateway := range gateways {
// init IR per gateway
irKey := irStringKey(gateway.Gateway.Namespace, gateway.Gateway.Name)
gwXdsIR := &ir.Xds{}
gwInfraIR := ir.NewInfra()
gwInfraIR.Proxy.Name = irKey
gwInfraIR.Proxy.GetProxyMetadata().Labels = GatewayOwnerLabels(gateway.Namespace, gateway.Name)
if resources.EnvoyProxy != nil {
gwInfraIR.Proxy.Config = resources.EnvoyProxy
}
// save the IR references in the map before the translation starts
xdsIR[irKey] = gwXdsIR
infraIR[irKey] = gwInfraIR
// Infra IR proxy ports must be unique.
var foundPorts []*protocolPort
gwXdsIR.AccessLog = processAccessLog(gwInfraIR.Proxy.Config)
gwXdsIR.Tracing = processTracing(gateway.Gateway, gwInfraIR.Proxy.Config)
for _, listener := range gateway.listeners {
// Process protocol & supported kinds
switch listener.Protocol {
case v1beta1.TLSProtocolType:
if listener.TLS != nil {
switch *listener.TLS.Mode {
case v1beta1.TLSModePassthrough:
t.validateAllowedRoutes(listener, KindTLSRoute)
case v1beta1.TLSModeTerminate:
t.validateAllowedRoutes(listener, KindTCPRoute)
default:
t.validateAllowedRoutes(listener, KindTCPRoute, KindTLSRoute)
}
} else {
t.validateAllowedRoutes(listener, KindTCPRoute, KindTLSRoute)
}
case v1beta1.HTTPProtocolType, v1beta1.HTTPSProtocolType:
t.validateAllowedRoutes(listener, KindHTTPRoute, KindGRPCRoute)
case v1beta1.TCPProtocolType:
t.validateAllowedRoutes(listener, KindTCPRoute)
case v1beta1.UDPProtocolType:
t.validateAllowedRoutes(listener, KindUDPRoute)
default:
listener.SetCondition(
v1beta1.ListenerConditionAccepted,
metav1.ConditionFalse,
v1beta1.ListenerReasonUnsupportedProtocol,
fmt.Sprintf("Protocol %s is unsupported, must be %s, %s, %s or %s.", listener.Protocol,
v1beta1.HTTPProtocolType, v1beta1.HTTPSProtocolType, v1beta1.TCPProtocolType, v1beta1.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 v1beta1.HTTPProtocolType, v1beta1.HTTPSProtocolType:
irListener := &ir.HTTPListener{
Name: irHTTPListenerName(listener),
Address: "0.0.0.0",
Port: uint32(containerPort),
TLS: irTLSConfigs(listener.tlsSecrets),
}
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/v1beta1.Listener.
irListener.Hostnames = append(irListener.Hostnames, "*")
}
gwXdsIR.HTTP = append(gwXdsIR.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, servicePort) {
foundPorts = append(foundPorts, servicePort)
var proto ir.ProtocolType
switch listener.Protocol {
case v1beta1.HTTPProtocolType:
proto = ir.HTTPProtocolType
case v1beta1.HTTPSProtocolType:
proto = ir.HTTPSProtocolType
case v1beta1.TLSProtocolType:
proto = ir.TLSProtocolType
case v1beta1.TCPProtocolType:
proto = ir.TCPProtocolType
case v1beta1.UDPProtocolType:
proto = ir.UDPProtocolType
}
infraPort := ir.ListenerPort{
Name: string(listener.Name),
Protocol: proto,
ServicePort: servicePort.port,
ContainerPort: containerPort,
}
// Only 1 listener is supported.
gwInfraIR.Proxy.Listeners[0].Ports = append(gwInfraIR.Proxy.Listeners[0].Ports, infraPort)
}
}
}
}
func processAccessLog(envoyproxy *configv1a1.EnvoyProxy) *ir.AccessLog {
if envoyproxy == 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 configv1a1.ProxyAccessLogSinkTypeFile:
switch accessLog.Format.Type {
case configv1a1.ProxyAccessLogFormatTypeText:
al := &ir.TextAccessLog{
Format: accessLog.Format.Text,
Path: sink.File.Path,
}
irAccessLog.Text = append(irAccessLog.Text, al)
case configv1a1.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 configv1a1.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 configv1a1.ProxyAccessLogFormatTypeJSON:
al.Attributes = accessLog.Format.JSON
case configv1a1.ProxyAccessLogFormatTypeText:
al.Text = accessLog.Format.Text
}
irAccessLog.OpenTelemetry = append(irAccessLog.OpenTelemetry, al)
}
}
}
return irAccessLog
}
func processTracing(gw *v1beta1.Gateway, envoyproxy *configv1a1.EnvoyProxy) *ir.Tracing {
if envoyproxy == nil || envoyproxy.Spec.Telemetry.Tracing == nil {
return nil
}
return &ir.Tracing{
ServiceName: naming.ServiceName(types.NamespacedName{Name: gw.Name, Namespace: gw.Namespace}),
ProxyTracing: *envoyproxy.Spec.Telemetry.Tracing,
}
}