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

Fix KubeApi watch retry on timeout (#6640)

* fix KubeApi watch retry on timeout

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* Fix tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
Signed-off-by: Sebastian Malton <sebastian@malton.name>
Co-authored-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Jari Kolehmainen 2022-11-24 15:44:35 +02:00 committed by GitHub
parent 95cee3bcc8
commit 245e132ada
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 12 deletions

View File

@ -592,7 +592,7 @@ describe("KubeApi", () => {
it("requests the watch", () => { it("requests the watch", () => {
expect(fetchMock.mock.lastCall).toMatchObject([ expect(fetchMock.mock.lastCall).toMatchObject([
"http://127.0.0.1:9999/api-kube/api/v1/namespaces/kube-system/pods?watch=1&resourceVersion=", "http://127.0.0.1:9999/api-kube/api/v1/namespaces/kube-system/pods?watch=1&resourceVersion=&timeoutSeconds=600",
{ {
headers: { headers: {
"content-type": "application/json", "content-type": "application/json",
@ -606,7 +606,7 @@ describe("KubeApi", () => {
beforeEach(async () => { beforeEach(async () => {
await fetchMock.resolveSpecific( await fetchMock.resolveSpecific(
([url, init]) => { ([url, init]) => {
const isMatch = url === "http://127.0.0.1:9999/api-kube/api/v1/namespaces/kube-system/pods?watch=1&resourceVersion="; const isMatch = url === "http://127.0.0.1:9999/api-kube/api/v1/namespaces/kube-system/pods?watch=1&resourceVersion=&timeoutSeconds=600";
if (isMatch) { if (isMatch) {
init?.signal?.addEventListener("abort", () => { init?.signal?.addEventListener("abort", () => {
@ -616,7 +616,7 @@ describe("KubeApi", () => {
return isMatch; return isMatch;
}, },
createMockResponseFromStream("http://127.0.0.1:9999/api-kube/api/v1/namespaces/kube-system/pods?watch=1&resourceVersion=", stream), createMockResponseFromStream("http://127.0.0.1:9999/api-kube/api/v1/namespaces/kube-system/pods?watch=1&resourceVersion=&timeoutSeconds=600", stream),
); );
}); });
@ -688,7 +688,7 @@ describe("KubeApi", () => {
it("requests the watch", () => { it("requests the watch", () => {
expect(fetchMock.mock.lastCall).toMatchObject([ expect(fetchMock.mock.lastCall).toMatchObject([
"http://127.0.0.1:9999/api-kube/api/v1/namespaces/kube-system/pods?watch=1&resourceVersion=", "http://127.0.0.1:9999/api-kube/api/v1/namespaces/kube-system/pods?watch=1&resourceVersion=&timeoutSeconds=600",
{ {
headers: { headers: {
"content-type": "application/json", "content-type": "application/json",
@ -702,7 +702,7 @@ describe("KubeApi", () => {
beforeEach(async () => { beforeEach(async () => {
await fetchMock.resolveSpecific( await fetchMock.resolveSpecific(
([url, init]) => { ([url, init]) => {
const isMatch = url === "http://127.0.0.1:9999/api-kube/api/v1/namespaces/kube-system/pods?watch=1&resourceVersion="; const isMatch = url === "http://127.0.0.1:9999/api-kube/api/v1/namespaces/kube-system/pods?watch=1&resourceVersion=&timeoutSeconds=600";
if (isMatch) { if (isMatch) {
init?.signal?.addEventListener("abort", () => { init?.signal?.addEventListener("abort", () => {
@ -712,7 +712,7 @@ describe("KubeApi", () => {
return isMatch; return isMatch;
}, },
createMockResponseFromStream("http://127.0.0.1:9999/api-kube/api/v1/namespaces/kube-system/pods?watch=1&resourceVersion=", stream), createMockResponseFromStream("http://127.0.0.1:9999/api-kube/api/v1/namespaces/kube-system/pods?watch=1&resourceVersion=&timeoutSeconds=600", stream),
); );
}); });

View File

@ -14,7 +14,7 @@ import byline from "byline";
import type { IKubeWatchEvent } from "./kube-watch-event"; import type { IKubeWatchEvent } from "./kube-watch-event";
import type { KubeJsonApiData, KubeJsonApi } from "./kube-json-api"; import type { KubeJsonApiData, KubeJsonApi } from "./kube-json-api";
import type { Disposer } from "../utils"; import type { Disposer } from "../utils";
import { setTimeoutFor, isDefined, noop, WrappedAbortController } from "../utils"; import { isDefined, noop, WrappedAbortController } from "../utils";
import type { RequestInit, Response } from "node-fetch"; import type { RequestInit, Response } from "node-fetch";
import type { Patch } from "rfc6902"; import type { Patch } from "rfc6902";
import assert from "assert"; import assert from "assert";
@ -639,7 +639,7 @@ export class KubeApi<
namespace, namespace,
callback = noop as KubeApiWatchCallback<Data>, callback = noop as KubeApiWatchCallback<Data>,
retry = false, retry = false,
timeout, timeout = 600,
watchId = `${this.kind.toLowerCase()}-${this.watchId++}`, watchId = `${this.kind.toLowerCase()}-${this.watchId++}`,
} = opts ?? {}; } = opts ?? {};
@ -651,8 +651,6 @@ export class KubeApi<
clearTimeout(timedRetry); clearTimeout(timedRetry);
}); });
setTimeoutFor(abortController, 600 * 1000);
const requestParams = timeout ? { query: { timeoutSeconds: timeout }} : {}; const requestParams = timeout ? { query: { timeoutSeconds: timeout }} : {};
const watchUrl = this.getWatchUrl(namespace); const watchUrl = this.getWatchUrl(namespace);
const responsePromise = this.request.getResponse(watchUrl, requestParams, { const responsePromise = this.request.getResponse(watchUrl, requestParams, {
@ -695,8 +693,10 @@ export class KubeApi<
}, timeout * 1000 * 1.1); }, timeout * 1000 * 1.1);
} }
if (!response.body) { if (!response.body || !response.body.readable) {
this.dependencies.logger.error(`[KUBE-API]: watch (${watchId}) did not return a body`); if (!response.body) {
this.dependencies.logger.warn(`[KUBE-API]: watch (${watchId}) did not return a body`);
}
requestRetried = true; requestRetried = true;
clearTimeout(timedRetry); clearTimeout(timedRetry);