Skip to content

Commit 5d565e3

Browse files
Do not duplicate rules when endpoint has multiple ports (#2825)
1 parent 7c46ec2 commit 5d565e3

File tree

5 files changed

+96
-4
lines changed

5 files changed

+96
-4
lines changed

src/Kubernetes.Controller/Converters/YarpParser.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,21 @@ private static void HandleIngressRulePath(YarpIngressContext ingressContext, V1S
9595
// make sure cluster is present
9696
foreach (var subset in subsets ?? Enumerable.Empty<V1EndpointSubset>())
9797
{
98+
var isRoutePresent = false;
9899
foreach (var port in subset.Ports ?? Enumerable.Empty<Corev1EndpointPort>())
99100
{
100101
if (!MatchesPort(port, servicePort))
101102
{
102103
continue;
103104
}
104105

105-
var pathMatch = FixupPathMatch(path);
106-
var host = rule.Host;
107-
108-
routes.Add(CreateRoute(ingressContext, path, cluster, pathMatch, host));
106+
if (!isRoutePresent)
107+
{
108+
var pathMatch = FixupPathMatch(path);
109+
var host = rule.Host;
110+
routes.Add(CreateRoute(ingressContext, path, cluster, pathMatch, host));
111+
isRoutePresent = true;
112+
}
109113

110114
// Add destination for every endpoint address
111115
foreach (var address in subset.Addresses ?? Enumerable.Empty<V1EndpointAddress>())

test/Kubernetes.Tests/IngressConversionTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public IngressConversionTests()
4040
[Theory]
4141
[InlineData("basic-ingress")]
4242
[InlineData("multiple-endpoints-ports")]
43+
[InlineData("multiple-endpoints-same-port")]
4344
[InlineData("https")]
4445
[InlineData("exact-match")]
4546
[InlineData("annotations")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[
2+
{
3+
"ClusterId": "frontend.default:80",
4+
"LoadBalancingPolicy": null,
5+
"SessionAffinity": null,
6+
"HealthCheck": null,
7+
"HttpClient": null,
8+
"HttpRequest": null,
9+
"Destinations": {
10+
"http://10.244.2.38:80": {
11+
"Address": "http://10.244.2.38:80",
12+
"Health": null,
13+
"Metadata": null
14+
}
15+
},
16+
"Metadata": null
17+
}
18+
]
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: Ingress
3+
metadata:
4+
name: minimal-ingress
5+
namespace: default
6+
spec:
7+
rules:
8+
- http:
9+
paths:
10+
- path: /foo
11+
pathType: Prefix
12+
backend:
13+
service:
14+
name: frontend
15+
port:
16+
number: 80
17+
---
18+
apiVersion: v1
19+
kind: Service
20+
metadata:
21+
name: frontend
22+
namespace: default
23+
spec:
24+
selector:
25+
app: frontend
26+
ports:
27+
- name: http
28+
port: 80
29+
targetPort: 80
30+
- name: other_http
31+
port: 8080
32+
targetPort: 80
33+
type: ClusterIP
34+
---
35+
apiVersion: v1
36+
kind: Endpoints
37+
metadata:
38+
name: frontend
39+
namespace: default
40+
subsets:
41+
- addresses:
42+
- ip: 10.244.2.38
43+
ports:
44+
- name: http
45+
port: 80
46+
protocol: TCP
47+
- name: other_http
48+
port: 80
49+
protocol: TCP
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[
2+
{
3+
"RouteId": "minimal-ingress.default:/foo",
4+
"Match": {
5+
"Methods": null,
6+
"Hosts": [],
7+
"Path": "/foo/{**catch-all}",
8+
"Headers": null,
9+
"QueryParameters": null
10+
},
11+
"Order": null,
12+
"ClusterId": "frontend.default:80",
13+
"AuthorizationPolicy": null,
14+
"RateLimiterPolicy": null,
15+
"CorsPolicy": null,
16+
"Metadata": null,
17+
"Transforms": null
18+
}
19+
]

0 commit comments

Comments
 (0)