From 18bfbe32372048658c03319a1653a6c595634f18 Mon Sep 17 00:00:00 2001 From: Ananya Nayak Date: Mon, 9 Oct 2023 19:20:46 +0530 Subject: [PATCH] chore: did some cleanup --- src/config.ts | 30 +++++++++++++++--------------- src/exec_auth.ts | 2 +- src/portforward.ts | 3 +-- src/top.ts | 8 ++------ src/util.ts | 34 +++++++++++++++------------------- src/watch.ts | 4 ++-- src/web-socket-handler.ts | 4 ++-- 7 files changed, 38 insertions(+), 47 deletions(-) diff --git a/src/config.ts b/src/config.ts index f4ea172d0d..88079db014 100644 --- a/src/config.ts +++ b/src/config.ts @@ -277,11 +277,11 @@ export class KubeConfig { if (!this.clusters) { this.clusters = []; } - this.clusters.forEach((c: Cluster, ix: number) => { - if (c.name === cluster.name) { - throw new Error(`Duplicate cluster: ${c.name}`); - } - }); + + if (this.clusters.some((c) => c.name === cluster.name)) { + throw new Error(`Duplicate cluster: ${cluster.name}`); + } + this.clusters.push(cluster); } @@ -289,11 +289,11 @@ export class KubeConfig { if (!this.users) { this.users = []; } - this.users.forEach((c: User, ix: number) => { - if (c.name === user.name) { - throw new Error(`Duplicate user: ${c.name}`); - } - }); + + if (this.users.some((c) => c.name === user.name)) { + throw new Error(`Duplicate user: ${user.name}`); + } + this.users.push(user); } @@ -301,11 +301,11 @@ export class KubeConfig { if (!this.contexts) { this.contexts = []; } - this.contexts.forEach((c: Context, ix: number) => { - if (c.name === ctx.name) { - throw new Error(`Duplicate context: ${c.name}`); - } - }); + + if (this.contexts.some((c) => c.name === ctx.name)) { + throw new Error(`Duplicate context: ${ctx.name}`); + } + this.contexts.push(ctx); } diff --git a/src/exec_auth.ts b/src/exec_auth.ts index ced03a2866..b8b5ee4e6c 100644 --- a/src/exec_auth.ts +++ b/src/exec_auth.ts @@ -100,7 +100,7 @@ export class ExecAuth implements Authenticator { const env = { ...process.env, }; - exec.env.forEach((elt) => (env[elt.name] = elt.value)); + exec.env.forEach((elt: { name: string | number; value?: string }) => (env[elt.name] = elt.value)); opts = { ...opts, env }; } const result = this.execFn(exec.command, exec.args, opts); diff --git a/src/portforward.ts b/src/portforward.ts index 655d7dd6e0..991a110f04 100644 --- a/src/portforward.ts +++ b/src/portforward.ts @@ -1,7 +1,6 @@ import WebSocket = require('isomorphic-ws'); import querystring = require('querystring'); import stream = require('stream'); -import { isUndefined } from 'util'; import { KubeConfig } from './config'; import { WebSocketHandler, WebSocketInterface } from './web-socket-handler'; @@ -13,7 +12,7 @@ export class PortForward { // handler is a parameter really only for injecting for testing. constructor(config: KubeConfig, disconnectOnErr?: boolean, handler?: WebSocketInterface) { this.handler = handler || new WebSocketHandler(config); - this.disconnectOnErr = isUndefined(disconnectOnErr) ? true : disconnectOnErr; + this.disconnectOnErr = disconnectOnErr === undefined ? true : disconnectOnErr; } // TODO: support multiple ports for real... diff --git a/src/top.ts b/src/top.ts index 1530ff1101..9b38ea64fb 100644 --- a/src/top.ts +++ b/src/top.ts @@ -1,10 +1,9 @@ import { CoreV1Api, V1Node, V1Pod, V1PodList, V1PodStatus } from './gen/api'; -import { ContainerMetric, Metrics, PodMetric } from './metrics'; +import { Metrics, PodMetric } from './metrics'; import { add, podsForNode, quantityToScalar, - ResourceStatus, totalCPU, totalCPUForContainer, totalMemory, @@ -90,10 +89,7 @@ export async function topNodes(api: CoreV1Api): Promise { export async function topPods(api: CoreV1Api, metrics: Metrics, namespace?: string): Promise { // Figure out which pod list endpoint to call const getPodList = async (): Promise => { - if (namespace) { - return (await api.listNamespacedPod(namespace)).body; - } - return (await api.listPodForAllNamespaces()).body; + return (await (namespace ? api.listNamespacedPod(namespace) : api.listPodForAllNamespaces())).body; }; const [podMetrics, podList] = await Promise.all([metrics.getPodMetrics(namespace), getPodList()]); diff --git a/src/util.ts b/src/util.ts index 32345add1b..4c1eeac98f 100644 --- a/src/util.ts +++ b/src/util.ts @@ -30,52 +30,48 @@ export function quantityToScalar(quantity: string): number | bigint { } switch (suffix) { case 'n': - return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1_000_000_000.0; + return Number(quantity.slice(0, quantity.length - 1)).valueOf() / 1_000_000_000.0; case 'u': - return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1_000_000.0; + return Number(quantity.slice(0, quantity.length - 1)).valueOf() / 1_000_000.0; case 'm': - return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1000.0; + return Number(quantity.slice(0, quantity.length - 1)).valueOf() / 1000.0; case 'k': - return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000); + return BigInt(quantity.slice(0, quantity.length - 1)) * BigInt(1000); case 'M': - return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000); + return BigInt(quantity.slice(0, quantity.length - 1)) * BigInt(1000 * 1000); case 'G': - return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000); + return BigInt(quantity.slice(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000); case 'T': - return ( - BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000) * BigInt(1000) - ); + return BigInt(quantity.slice(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000) * BigInt(1000); case 'P': return ( - BigInt(quantity.substr(0, quantity.length - 1)) * + BigInt(quantity.slice(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000) * BigInt(1000 * 1000) ); case 'E': return ( - BigInt(quantity.substr(0, quantity.length - 1)) * + BigInt(quantity.slice(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000) * BigInt(1000 * 1000 * 1000) ); case 'Ki': - return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024); + return BigInt(quantity.slice(0, quantity.length - 2)) * BigInt(1024); case 'Mi': - return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024); + return BigInt(quantity.slice(0, quantity.length - 2)) * BigInt(1024 * 1024); case 'Gi': - return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024); + return BigInt(quantity.slice(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024); case 'Ti': - return ( - BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024) * BigInt(1024) - ); + return BigInt(quantity.slice(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024) * BigInt(1024); case 'Pi': return ( - BigInt(quantity.substr(0, quantity.length - 2)) * + BigInt(quantity.slice(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024) * BigInt(1024 * 1024) ); case 'Ei': return ( - BigInt(quantity.substr(0, quantity.length - 2)) * + BigInt(quantity.slice(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024) * BigInt(1024 * 1024 * 1024) ); diff --git a/src/watch.ts b/src/watch.ts index 9dd01492ca..99bc665dc3 100644 --- a/src/watch.ts +++ b/src/watch.ts @@ -103,7 +103,7 @@ export class Watch { }; await this.config.applyToRequest(requestOptions); - let req; + let req: RequestResult; let doneCalled: boolean = false; const doneCallOnce = (err: any) => { if (!doneCalled) { @@ -122,7 +122,7 @@ export class Watch { stream.on('error', doneCallOnce); stream.on('close', () => doneCallOnce(null)); stream.on('data', (line) => { - let data; + let data: { type: string; object: any }; try { data = JSON.parse(line); diff --git a/src/web-socket-handler.ts b/src/web-socket-handler.ts index 64e9ef3e47..057d47b757 100644 --- a/src/web-socket-handler.ts +++ b/src/web-socket-handler.ts @@ -160,7 +160,7 @@ export class WebSocketHandler implements WebSocketInterface { } const server = cluster.server; const ssl = server.startsWith('https://'); - const target = ssl ? server.substr(8) : server.substr(7); + const target = ssl ? server.slice(8) : server.slice(7); const proto = ssl ? 'wss' : 'ws'; const uri = `${proto}://${target}${path}`; @@ -193,7 +193,7 @@ export class WebSocketHandler implements WebSocketInterface { } } else if (data instanceof Buffer) { const streamNum = data.readInt8(0); - if (binaryHandler && !binaryHandler(streamNum, data.slice(1))) { + if (binaryHandler && !binaryHandler(streamNum, data.subarray(1))) { client.close(); } }