1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

detect log follow requests

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2021-09-24 13:23:55 +03:00
parent 17e97f16c9
commit 7cd8d5e20e
3 changed files with 78 additions and 5 deletions

View File

@ -0,0 +1,56 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { isLongRunningRequest } from "../lens-proxy";
describe("isLongRunningRequest", () => {
it("returns true on watches", () => {
["watch=true", "watch=1"].forEach((param) => {
expect(
isLongRunningRequest(`/api/v1/namespaces/default/pods?${param}`)
).toBeTruthy();
});
});
it("returns false on disabled watches", () => {
["watch=false", "watch=0", "watch", ""].forEach((param) => {
expect(
isLongRunningRequest(`/api/v1/namespaces/default/pods?${param}`)
).toBeFalsy();
});
});
it("returns true on follows", () => {
["follow=true", "follow=1"].forEach((param) => {
expect(
isLongRunningRequest(`/api/v1/namespaces/default/pods/foo/log?${param}`)
).toBeTruthy();
});
});
it("returns false on disabled follows", () => {
["follow=false", "follow=0", "follow", ""].forEach((param) => {
expect(
isLongRunningRequest(`/api/v1/namespaces/default/pods/foo/log?${param}`)
).toBeFalsy();
});
});
});

View File

@ -117,10 +117,10 @@ export class ContextHandler {
return `http://127.0.0.1:${this.kubeAuthProxy.port}${path}`;
}
async getApiTarget(isWatchRequest = false): Promise<httpProxy.ServerOptions> {
const timeout = isWatchRequest ? 4 * 60 * 60 * 1000 : 30000; // 4 hours for watch request, 30 seconds for the rest
async getApiTarget(isLongRunningRequest = false): Promise<httpProxy.ServerOptions> {
const timeout = isLongRunningRequest ? 4 * 60 * 60_000 : 30_000; // 4 hours for long running request, 30 seconds for the rest
if (isWatchRequest) {
if (isLongRunningRequest) {
return this.newApiTarget(timeout);
}

View File

@ -40,6 +40,24 @@ export interface LensProxyFunctions {
kubeApiRequest: (args: ProxyApiRequestArgs) => void | Promise<void>;
}
const truthyParams = ["1", "true"];
const watchParam = "watch";
const followParam = "follow";
export function isLongRunningRequest(reqUrl: string) {
const url = new URL(reqUrl, "http://localhost");
if (url.searchParams.has(watchParam) && truthyParams.includes(url.searchParams.get(watchParam)) ) {
return true;
}
if (url.searchParams.has(followParam) && truthyParams.includes(url.searchParams.get(followParam)) ) {
return true;
}
return false;
}
export class LensProxy extends Singleton {
protected origin: string;
protected proxyServer: http.Server;
@ -174,9 +192,8 @@ export class LensProxy extends Singleton {
if (req.url.startsWith(apiKubePrefix)) {
delete req.headers.authorization;
req.url = req.url.replace(apiKubePrefix, "");
const isWatchRequest = req.url.includes("watch=");
return contextHandler.getApiTarget(isWatchRequest);
return contextHandler.getApiTarget(isLongRunningRequest(req.url));
}
}