Skip to content

Commit 060c84d

Browse files
committed
fix ListenerSetProtocolConflict
Signed-off-by: zirain <zirain2009@gmail.com>
1 parent 5ae7a19 commit 060c84d

21 files changed

Lines changed: 960 additions & 223 deletions

internal/cmd/egctl/testdata/translate/in/default-resources.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ spec:
1919
port: 1234
2020
- name: udp
2121
protocol: UDP
22-
port: 1234
22+
port: 2345
2323
- name: tls-passthrough
2424
protocol: TLS
2525
port: 8443

internal/cmd/egctl/testdata/translate/in/from-gateway-api-to-xds.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ spec:
1818
port: 1234
1919
- name: udp
2020
protocol: UDP
21-
port: 1234
21+
port: 2345
2222
- name: tls-passthrough
2323
protocol: TLS
2424
port: 8443

internal/cmd/egctl/testdata/translate/in/valid-envoyproxy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ spec:
3636
port: 1234
3737
- name: udp
3838
protocol: UDP
39-
port: 1234
39+
port: 2345
4040
- name: tls-passthrough
4141
protocol: TLS
4242
port: 8443

internal/cmd/egctl/testdata/translate/out/default-resources.all.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ gateways:
211211
namespaces:
212212
from: Same
213213
name: udp
214-
port: 1234
214+
port: 2345
215215
protocol: UDP
216216
- allowedRoutes:
217217
namespaces:
@@ -1502,7 +1502,7 @@ xds:
15021502
address:
15031503
socketAddress:
15041504
address: 0.0.0.0
1505-
portValue: 1234
1505+
portValue: 2345
15061506
protocol: UDP
15071507
listenerFilters:
15081508
- name: envoy.filters.udp_listener.udp_proxy

internal/cmd/egctl/testdata/translate/out/from-gateway-api-to-xds.all.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@
14461446
"address": {
14471447
"socketAddress": {
14481448
"address": "0.0.0.0",
1449-
"portValue": 1234,
1449+
"portValue": 2345,
14501450
"protocol": "UDP"
14511451
}
14521452
},

internal/cmd/egctl/testdata/translate/out/from-gateway-api-to-xds.all.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ xds:
923923
address:
924924
socketAddress:
925925
address: 0.0.0.0
926-
portValue: 1234
926+
portValue: 2345
927927
protocol: UDP
928928
listenerFilters:
929929
- name: envoy.filters.udp_listener.udp_proxy

internal/cmd/egctl/testdata/translate/out/from-gateway-api-to-xds.listener.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ xds:
473473
address:
474474
socketAddress:
475475
address: 0.0.0.0
476-
portValue: 1234
476+
portValue: 2345
477477
protocol: UDP
478478
listenerFilters:
479479
- name: envoy.filters.udp_listener.udp_proxy

internal/cmd/egctl/testdata/translate/out/valid-envoyproxy.all.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ gateways:
5555
namespaces:
5656
from: Same
5757
name: udp
58-
port: 1234
58+
port: 2345
5959
protocol: UDP
6060
- allowedRoutes:
6161
namespaces:

internal/gatewayapi/helpers.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ import (
3131
)
3232

3333
const (
34-
TCPProtocol = "TCP"
35-
UDPProtocol = "UDP"
36-
37-
L4Protocol = "L4"
38-
L7Protocol = "L7"
34+
HTTPProtocol = "HTTP"
35+
TCPProtocol = "TCP"
36+
UDPProtocol = "UDP"
3937

4038
// CACertKey is the key used in ConfigMaps and Secrets to store CA certificate data
4139
CACertKey = "ca.crt"
@@ -381,26 +379,27 @@ func hostnameMatchesWildcardHostname(hostname, wildcardHostname string) bool {
381379
return len(wildcardMatch) > 0
382380
}
383381

384-
func containsPort(ports []*protocolPort, port *protocolPort) bool {
385-
for _, protocolPort := range ports {
386-
curProtocol, curLevel := layer4Protocol(protocolPort)
387-
myProtocol, myLevel := layer4Protocol(port)
388-
if protocolPort.port == port.port && (curProtocol == myProtocol && curLevel == myLevel) {
389-
return true
382+
func checkPortProtocol(gatewayPorts []*protocolPort, servicePort *protocolPort) (bool, bool) {
383+
for _, gatewayPort := range gatewayPorts {
384+
if gatewayPort.port != servicePort.port {
385+
continue
390386
}
387+
388+
return true, listenerProtocol(gatewayPort) == listenerProtocol(servicePort)
391389
}
392-
return false
390+
391+
return false, true
393392
}
394393

395-
// layer4Protocol returns listener L4 protocol and listen protocol level
396-
func layer4Protocol(protocolPort *protocolPort) (string, string) {
394+
// listenerProtocol returns listener L4 protocol and listen protocol level
395+
func listenerProtocol(protocolPort *protocolPort) string {
397396
switch protocolPort.protocol {
398397
case gwapiv1.HTTPProtocolType, gwapiv1.HTTPSProtocolType, gwapiv1.TLSProtocolType:
399-
return TCPProtocol, L7Protocol
398+
return HTTPProtocol
400399
case gwapiv1.TCPProtocolType:
401-
return TCPProtocol, L4Protocol
400+
return TCPProtocol
402401
default:
403-
return UDPProtocol, L4Protocol
402+
return UDPProtocol
404403
}
405404
}
406405

internal/gatewayapi/listener.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR resource
4545
t.validateConflictedLayer7Listeners(gateways)
4646
t.validateConflictedLayer4Listeners(gateways, gwapiv1.TCPProtocolType)
4747
t.validateConflictedLayer4Listeners(gateways, gwapiv1.UDPProtocolType)
48+
t.validateConflictedProtocolsListeners(gateways)
4849
if t.MergeGateways {
4950
t.validateConflictedMergedListeners(gateways)
5051
}
@@ -104,6 +105,7 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR resource
104105
t.validateHostName(listener)
105106

106107
// Process conditions and check if the listener is ready
108+
<<<<<<< Updated upstream
107109
isReady := t.validateListenerConditions(listener)
108110

109111
if listener.isFromListenerSet() && isReady {
@@ -113,6 +115,12 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR resource
113115
}.String()
114116
gatewayAttachedListenerSets.Insert(lsKey)
115117
}
118+
=======
119+
// TODO: this's skipped in https://github.com/envoyproxy/gateway/pull/8194
120+
// for TLSRouteInvalidNoMatchingListener conformance test, but looks like it's
121+
// a must, consider to skip invalid listener again?
122+
listenerValid := t.validateListenerConditions(listener)
123+
>>>>>>> Stashed changes
116124

117125
address := netutils.IPv4ListenerAddress
118126
ipFamily := getEnvoyIPFamily(gateway.envoyProxy)
@@ -122,7 +130,30 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR resource
122130

123131
// Add the listener to the Xds IR
124132
servicePort := &protocolPort{protocol: listener.Protocol, port: listener.Port}
133+
// Add the listener to the Infra IR.
134+
// Infra IR ports must have a unique port number per layer protocol.
135+
gatewayPortProtocols := foundPorts[irKey]
136+
portExists, protocolValid := checkPortProtocol(gatewayPortProtocols, servicePort)
137+
if !protocolValid {
138+
continue
139+
}
140+
141+
if listener.isFromListenerSet() && listenerValid {
142+
lsKey := types.NamespacedName{
143+
Namespace: listener.listenerSet.Namespace,
144+
Name: listener.listenerSet.Name,
145+
}.String()
146+
gatewayAttachedListenerSets.Insert(lsKey)
147+
}
148+
foundPorts[irKey] = append(foundPorts[irKey], servicePort)
125149
containerPort := t.servicePortToContainerPort(listener.Port, gateway.envoyProxy)
150+
if !portExists {
151+
// Only add to Infra IR if the port number is unique for the layer protocol.
152+
// If the same port number is used in another listener with a different protocol,
153+
// it will be skipped when checking port protocol.
154+
t.processInfraIRListener(listener, infraIR, irKey, servicePort, containerPort)
155+
}
156+
126157
switch listener.Protocol {
127158
case gwapiv1.HTTPProtocolType, gwapiv1.HTTPSProtocolType:
128159
irListener := &ir.HTTPListener{
@@ -184,14 +215,6 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR resource
184215
}
185216
xdsIR[irKey].UDP = append(xdsIR[irKey].UDP, irListener)
186217
}
187-
188-
// Add the listener to the Infra IR. Infra IR ports must have a unique port number per layer-4 protocol
189-
// (TCP or UDP).
190-
if !containsPort(foundPorts[irKey], servicePort) {
191-
t.processInfraIRListener(listener, infraIR, irKey, servicePort, containerPort)
192-
foundPorts[irKey] = append(foundPorts[irKey], servicePort)
193-
}
194-
195218
}
196219
gateway.IncreaseAttachedListenerSets(uint32(len(gatewayAttachedListenerSets)))
197220
}

0 commit comments

Comments
 (0)