Skip to content

Commit

Permalink
Add route HTTP methods option via ingress annotations (#2543)
Browse files Browse the repository at this point in the history
* Add route HTTP methods option via ingress annotations

* Fixed annotation value format and test assets
  • Loading branch information
specialforest authored Jul 23, 2024
1 parent 445cd75 commit 12e924f
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 0 deletions.
14 changes: 14 additions & 0 deletions samples/KubernetesIngress.Sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ metadata:
- another-queryparameters-value
Mode: Contains
IsCaseSensitive: false
yarp.ingress.kubernetes.io/route-methods: |
- GET
- POST
spec:
rules:
- http:
Expand Down Expand Up @@ -99,6 +102,7 @@ The table below lists the available annotations.
|yarp.ingress.kubernetes.io/route-headers|List<[RouteHeader](https://microsoft.github.io/reverse-proxy/api/Yarp.ReverseProxy.Configuration.RouteHeader.html)>|
|yarp.ingress.kubernetes.io/route-queryparameters|List<[RouteQueryParameter](https://microsoft.github.io/reverse-proxy/api/Yarp.ReverseProxy.Configuration.RouteQueryParameter.html)>|
|yarp.ingress.kubernetes.io/route-order|int|
|yarp.ingress.kubernetes.io/route-methods|List<string>|

#### Authorization Policy

Expand Down Expand Up @@ -252,3 +256,13 @@ See https://microsoft.github.io/reverse-proxy/api/Yarp.ReverseProxy.Configuratio
```
yarp.ingress.kubernetes.io/route-order: '10'
```

#### Route Methods

See https://microsoft.github.io/reverse-proxy/api/Yarp.ReverseProxy.Configuration.RouteConfig.html#methods.

```
yarp.ingress.kubernetes.io/route-methods: |
- GET
- POST
```
1 change: 1 addition & 0 deletions src/Kubernetes.Controller/Converters/YarpIngressOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal sealed class YarpIngressOptions
public List<RouteHeader> RouteHeaders { get; set; }
public List<RouteQueryParameter> RouteQueryParameters { get; set; }
public int? RouteOrder { get; set; }
public List<string> RouteMethods { get; set; }
}

internal sealed class RouteHeaderWrapper
Expand Down
5 changes: 5 additions & 0 deletions src/Kubernetes.Controller/Converters/YarpParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ private static RouteConfig CreateRoute(YarpIngressContext ingressContext, V1HTTP
{
Match = new RouteMatch()
{
Methods = ingressContext.Options.RouteMethods,
Hosts = host is not null ? new[] { host } : Array.Empty<string>(),
Path = pathMatch,
Headers = ingressContext.Options.RouteHeaders,
Expand Down Expand Up @@ -289,6 +290,10 @@ private static YarpIngressOptions HandleAnnotations(YarpIngressContext context,
{
options.RouteOrder = int.Parse(routeOrder, CultureInfo.InvariantCulture);
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/route-methods", out var routeMethods))
{
options.RouteMethods = YamlDeserializer.Deserialize<List<string>>(routeMethods);
}
// metadata to support:
// rewrite target
// auth
Expand Down
1 change: 1 addition & 0 deletions test/Kubernetes.Tests/IngressConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public IngressConversionTests()
[InlineData("route-queryparameters")]
[InlineData("route-headers")]
[InlineData("route-order")]
[InlineData("route-methods")]
[InlineData("missing-svc")]
[InlineData("port-diff-name")]
[InlineData("external-name-ingress")]
Expand Down
18 changes: 18 additions & 0 deletions test/Kubernetes.Tests/testassets/route-methods/clusters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"ClusterId": "frontend.default:80",
"LoadBalancingPolicy": null,
"SessionAffinity": null,
"HealthCheck": null,
"HttpClient": null,
"HttpRequest": null,
"Destinations": {
"http://10.244.2.38:80": {
"Address": "http://10.244.2.38:80",
"Health": null,
"Metadata": null
}
},
"Metadata": null
}
]
67 changes: 67 additions & 0 deletions test/Kubernetes.Tests/testassets/route-methods/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: first-ingress
namespace: default
annotations:
yarp.ingress.kubernetes.io/route-methods: |
- GET
spec:
rules:
- http:
paths:
- path: /foo
pathType: Prefix
backend:
service:
name: frontend
port:
number: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: second-ingress
namespace: default
annotations:
yarp.ingress.kubernetes.io/route-methods: |
- POST
- PUT
spec:
rules:
- http:
paths:
- path: /foo
pathType: Prefix
backend:
service:
name: frontend
port:
number: 80
---
apiVersion: v1
kind: Service
metadata:
name: frontend
namespace: default
spec:
selector:
app: frontend
ports:
- name: https
port: 80
targetPort: 80
type: ClusterIP
---
apiVersion: v1
kind: Endpoints
metadata:
name: frontend
namespace: default
subsets:
- addresses:
- ip: 10.244.2.38
ports:
- name: https
port: 80
protocol: TCP
34 changes: 34 additions & 0 deletions test/Kubernetes.Tests/testassets/route-methods/routes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{
"RouteId": "first-ingress.default:/foo",
"Match": {
"Methods": ["GET"],
"Hosts": [],
"Path": "/foo/{**catch-all}",
"Headers": null,
"QueryParameters": null
},
"ClusterId": "frontend.default:80",
"AuthorizationPolicy": null,
"RateLimiterPolicy": null,
"CorsPolicy": null,
"Metadata": null,
"Transforms": null
},
{
"RouteId": "second-ingress.default:/foo",
"Match": {
"Methods": ["POST", "PUT"],
"Hosts": [],
"Path": "/foo/{**catch-all}",
"Headers": null,
"QueryParameters": null
},
"ClusterId": "frontend.default:80",
"AuthorizationPolicy": null,
"RateLimiterPolicy": null,
"CorsPolicy": null,
"Metadata": null,
"Transforms": null
}
]

0 comments on commit 12e924f

Please sign in to comment.