diff --git a/packages/main/src/plugin/kubernetes-context-state-constants.ts b/packages/main/src/plugin/kubernetes-context-state-constants.ts new file mode 100644 index 0000000000000..c85a8cedaa8af --- /dev/null +++ b/packages/main/src/plugin/kubernetes-context-state-constants.ts @@ -0,0 +1,22 @@ +/********************************************************************** + * Copyright (C) 2024 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ***********************************************************************/ + +export const connectTimeout = 1000; +export const backoffInitialValue = 1000; +export const backoffLimit = 60_000; +export const backoffJitter = 300; diff --git a/packages/main/src/plugin/kubernetes-context-state.spec.ts b/packages/main/src/plugin/kubernetes-context-state.spec.ts index 90721b2ff660d..e1ffb16b51197 100644 --- a/packages/main/src/plugin/kubernetes-context-state.spec.ts +++ b/packages/main/src/plugin/kubernetes-context-state.spec.ts @@ -117,6 +117,15 @@ vi.mock('@kubernetes/client-node', async importOriginal => { }; }); +vi.mock('./kubernetes-context-state-constants.js', () => { + return { + connectTimeout: 1, + backoffInitialValue: 1000, + backoffLimit: 1000, + backoffJitter: 1, + }; +}); + const originalConsoleDebug = console.debug; const consoleDebugMock = vi.fn(); @@ -211,7 +220,7 @@ test('should send info of resources in all reachable contexts and nothing in non deployments: [{}, {}, {}, {}, {}], }, } as ContextState); - await new Promise(resolve => setTimeout(resolve, 1200)); + await new Promise(resolve => setTimeout(resolve, 2)); expect(apiSenderSendMock).toHaveBeenCalledWith('kubernetes-contexts-state-update', expectedMap); // => removing contexts, should remving clusters from sent info @@ -282,7 +291,7 @@ test('should send info of resources in all reachable contexts and nothing in non deployments: [{}, {}, {}, {}], }, } as ContextState); - await new Promise(resolve => setTimeout(resolve, 1200)); + await new Promise(resolve => setTimeout(resolve, 2)); expect(apiSenderSendMock).toHaveBeenCalledWith('kubernetes-contexts-state-update', expectedMap); }); diff --git a/packages/main/src/plugin/kubernetes-context-state.ts b/packages/main/src/plugin/kubernetes-context-state.ts index 7cd47a03f2555..b062e801694a9 100644 --- a/packages/main/src/plugin/kubernetes-context-state.ts +++ b/packages/main/src/plugin/kubernetes-context-state.ts @@ -27,6 +27,12 @@ import type { import { AppsV1Api, CoreV1Api, KubeConfig, makeInformer } from '@kubernetes/client-node'; import type { KubeContext } from './kubernetes-context.js'; import type { ApiSenderType } from './api.js'; +import { + backoffInitialValue, + backoffJitter, + backoffLimit, + connectTimeout, +} from './kubernetes-context-state-constants.js'; // ContextInternalState stores informers for a kube context interface ContextInternalState { @@ -201,7 +207,7 @@ export class ContextsManager { return this.createInformer(kc, context, path, listFn, { resource: 'pods', timer: this.podTimer, - backoff: new Backoff(1000, 60_000, 300), + backoff: new Backoff(backoffInitialValue, backoffLimit, backoffJitter), onAdd: obj => this.setStateAndDispatch(context.name, state => state.resources.pods.push(obj)), onDelete: obj => this.setStateAndDispatch( @@ -228,7 +234,7 @@ export class ContextsManager { return this.createInformer(kc, context, path, listFn, { resource: 'deployments', timer: this.deploymentTimer, - backoff: new Backoff(1000, 60_000, 300), + backoff: new Backoff(backoffInitialValue, backoffLimit, backoffJitter), onAdd: obj => this.setStateAndDispatch(context.name, state => state.resources.deployments.push(obj)), onDelete: obj => this.setStateAndDispatch( @@ -319,7 +325,7 @@ export class ContextsManager { clearTimeout(this.timeoutId); this.timeoutId = setTimeout(() => { this.apiSender.send(`kubernetes-contexts-state-update`, this.states.getPublished()); - }, 1000); + }, connectTimeout); } public getContextsState(): Map {