ThreeMammals / Ocelot

.NET API Gateway
https://www.nuget.org/packages/Ocelot
MIT License
8.4k stars 1.64k forks source link

Handle some requests in the gateway (to support the Kubernetes liveness probe) #681

Closed ChrisSwinchatt closed 6 years ago

ChrisSwinchatt commented 6 years ago

Hi there,

I'm not sure if Ocelot can already do this, but I have an endpoint in my API gateway which is supposed to respond 200 whenever it gets a request, proving that the service is alive. We normally configure K8S to hit that endpoint every 30 seconds, but I can't find a way to do this with Ocelot. Any endpoint that doesn't match a reroute responds 404, and if you try to reroute it to itself, it loops until the request times out.

Please can you advise if this is an existing feature (or there's a reasonable workaround), or will be supported in future.

New Feature

Provide a way to expose endpoints without rerouting so that K8S probes can get a response from a specific endpoint. For example, create a new Allow array in ocelot.json which allows requests through without rerouting them. For example, if my endpoint is /probe, ocelot.json could look like this:

{
    "ReRoutes": [
        // ...
    ],
    "Allow": [
        {
            "PathTemplate": "/probe",
            "HttpMethod":  "Get"
        }
    ],
    "GlobalConfiguration": {
        // ...
    }
}

Motivation for New Feature

K8S restarts pods that don't respond to probes for 90 seconds, so the probes have to be disabled which is not ideal.

Steps to Reproduce the Problem

  1. Create a controller with at least one endpoint within the service which is running Ocelot
  2. Either create a reroute for it or don't
  3. When you hit it (e.g. with cURL) it will either return 404 (if there's no reroute specified) or time-out because of an infinite loop (if you try to reroute it to itself)

Specifications

philproctor commented 6 years ago

I'm not part of the project, but this doesn't need to be explicitly supported in Ocelot. The way we work through this is by adding the MVC middleware prior to Ocelot. Because middlewares are processed in the same order as they are added, MVC will handle it first if it is added prior to Ocelot, but will still pass the request through to Ocelot if the request isn't handled.

e.g. in your app configuration...

                    app.UseMvc();
                    app.UseOcelot().Wait();
ChrisSwinchatt commented 6 years ago

@philproctor

Oh, that's neat. Thanks!