Skip to content

Commit

Permalink
Fix KubeApi watch retry on timeout (lensapp#6640)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
jakolehm and Nokel81 authored Nov 24, 2022
1 parent 95cee3b commit 245e132
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/common/k8s-api/__tests__/kube-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ describe("KubeApi", () => {

it("requests the watch", () => {
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: {
"content-type": "application/json",
Expand All @@ -606,7 +606,7 @@ describe("KubeApi", () => {
beforeEach(async () => {
await fetchMock.resolveSpecific(
([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) {
init?.signal?.addEventListener("abort", () => {
Expand All @@ -616,7 +616,7 @@ describe("KubeApi", () => {

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),
);
});

Expand Down Expand Up @@ -688,7 +688,7 @@ describe("KubeApi", () => {

it("requests the watch", () => {
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: {
"content-type": "application/json",
Expand All @@ -702,7 +702,7 @@ describe("KubeApi", () => {
beforeEach(async () => {
await fetchMock.resolveSpecific(
([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) {
init?.signal?.addEventListener("abort", () => {
Expand All @@ -712,7 +712,7 @@ describe("KubeApi", () => {

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),
);
});

Expand Down
12 changes: 6 additions & 6 deletions src/common/k8s-api/kube-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import byline from "byline";
import type { IKubeWatchEvent } from "./kube-watch-event";
import type { KubeJsonApiData, KubeJsonApi } from "./kube-json-api";
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 { Patch } from "rfc6902";
import assert from "assert";
Expand Down Expand Up @@ -639,7 +639,7 @@ export class KubeApi<
namespace,
callback = noop as KubeApiWatchCallback<Data>,
retry = false,
timeout,
timeout = 600,
watchId = `${this.kind.toLowerCase()}-${this.watchId++}`,
} = opts ?? {};

Expand All @@ -651,8 +651,6 @@ export class KubeApi<
clearTimeout(timedRetry);
});

setTimeoutFor(abortController, 600 * 1000);

const requestParams = timeout ? { query: { timeoutSeconds: timeout }} : {};
const watchUrl = this.getWatchUrl(namespace);
const responsePromise = this.request.getResponse(watchUrl, requestParams, {
Expand Down Expand Up @@ -695,8 +693,10 @@ export class KubeApi<
}, timeout * 1000 * 1.1);
}

if (!response.body) {
this.dependencies.logger.error(`[KUBE-API]: watch (${watchId}) did not return a body`);
if (!response.body || !response.body.readable) {
if (!response.body) {
this.dependencies.logger.warn(`[KUBE-API]: watch (${watchId}) did not return a body`);
}
requestRetried = true;

clearTimeout(timedRetry);
Expand Down

0 comments on commit 245e132

Please sign in to comment.