Skip to content

Commit 895a046

Browse files
authored
Merge pull request #1320 from Ananya2001-an/some-cleanup
chore: Some cleanup
2 parents 46af8cb + 4dbfe9e commit 895a046

File tree

7 files changed

+38
-47
lines changed

7 files changed

+38
-47
lines changed

src/config.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -277,35 +277,35 @@ export class KubeConfig {
277277
if (!this.clusters) {
278278
this.clusters = [];
279279
}
280-
this.clusters.forEach((c: Cluster, ix: number) => {
281-
if (c.name === cluster.name) {
282-
throw new Error(`Duplicate cluster: ${c.name}`);
283-
}
284-
});
280+
281+
if (this.clusters.some((c) => c.name === cluster.name)) {
282+
throw new Error(`Duplicate cluster: ${cluster.name}`);
283+
}
284+
285285
this.clusters.push(cluster);
286286
}
287287

288288
public addUser(user: User): void {
289289
if (!this.users) {
290290
this.users = [];
291291
}
292-
this.users.forEach((c: User, ix: number) => {
293-
if (c.name === user.name) {
294-
throw new Error(`Duplicate user: ${c.name}`);
295-
}
296-
});
292+
293+
if (this.users.some((c) => c.name === user.name)) {
294+
throw new Error(`Duplicate user: ${user.name}`);
295+
}
296+
297297
this.users.push(user);
298298
}
299299

300300
public addContext(ctx: Context): void {
301301
if (!this.contexts) {
302302
this.contexts = [];
303303
}
304-
this.contexts.forEach((c: Context, ix: number) => {
305-
if (c.name === ctx.name) {
306-
throw new Error(`Duplicate context: ${c.name}`);
307-
}
308-
});
304+
305+
if (this.contexts.some((c) => c.name === ctx.name)) {
306+
throw new Error(`Duplicate context: ${ctx.name}`);
307+
}
308+
309309
this.contexts.push(ctx);
310310
}
311311

src/exec_auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class ExecAuth implements Authenticator {
100100
const env = {
101101
...process.env,
102102
};
103-
exec.env.forEach((elt) => (env[elt.name] = elt.value));
103+
exec.env.forEach((elt: { name: string | number; value?: string }) => (env[elt.name] = elt.value));
104104
opts = { ...opts, env };
105105
}
106106
const result = this.execFn(exec.command, exec.args, opts);

src/portforward.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import WebSocket = require('isomorphic-ws');
22
import querystring = require('querystring');
33
import stream = require('stream');
4-
import { isUndefined } from 'util';
54

65
import { KubeConfig } from './config';
76
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler';
@@ -13,7 +12,7 @@ export class PortForward {
1312
// handler is a parameter really only for injecting for testing.
1413
constructor(config: KubeConfig, disconnectOnErr?: boolean, handler?: WebSocketInterface) {
1514
this.handler = handler || new WebSocketHandler(config);
16-
this.disconnectOnErr = isUndefined(disconnectOnErr) ? true : disconnectOnErr;
15+
this.disconnectOnErr = disconnectOnErr === undefined ? true : disconnectOnErr;
1716
}
1817

1918
// TODO: support multiple ports for real...

src/top.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { CoreV1Api, V1Node, V1Pod, V1PodList, V1PodStatus } from './gen/api';
2-
import { ContainerMetric, Metrics, PodMetric } from './metrics';
2+
import { Metrics, PodMetric } from './metrics';
33
import {
44
add,
55
podsForNode,
66
quantityToScalar,
7-
ResourceStatus,
87
totalCPU,
98
totalCPUForContainer,
109
totalMemory,
@@ -90,10 +89,7 @@ export async function topNodes(api: CoreV1Api): Promise<NodeStatus[]> {
9089
export async function topPods(api: CoreV1Api, metrics: Metrics, namespace?: string): Promise<PodStatus[]> {
9190
// Figure out which pod list endpoint to call
9291
const getPodList = async (): Promise<V1PodList> => {
93-
if (namespace) {
94-
return (await api.listNamespacedPod(namespace)).body;
95-
}
96-
return (await api.listPodForAllNamespaces()).body;
92+
return (await (namespace ? api.listNamespacedPod(namespace) : api.listPodForAllNamespaces())).body;
9793
};
9894

9995
const [podMetrics, podList] = await Promise.all([metrics.getPodMetrics(namespace), getPodList()]);

src/util.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,52 +30,48 @@ export function quantityToScalar(quantity: string): number | bigint {
3030
}
3131
switch (suffix) {
3232
case 'n':
33-
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1_000_000_000.0;
33+
return Number(quantity.slice(0, quantity.length - 1)).valueOf() / 1_000_000_000.0;
3434
case 'u':
35-
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1_000_000.0;
35+
return Number(quantity.slice(0, quantity.length - 1)).valueOf() / 1_000_000.0;
3636
case 'm':
37-
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1000.0;
37+
return Number(quantity.slice(0, quantity.length - 1)).valueOf() / 1000.0;
3838
case 'k':
39-
return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000);
39+
return BigInt(quantity.slice(0, quantity.length - 1)) * BigInt(1000);
4040
case 'M':
41-
return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000);
41+
return BigInt(quantity.slice(0, quantity.length - 1)) * BigInt(1000 * 1000);
4242
case 'G':
43-
return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000);
43+
return BigInt(quantity.slice(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000);
4444
case 'T':
45-
return (
46-
BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000) * BigInt(1000)
47-
);
45+
return BigInt(quantity.slice(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000) * BigInt(1000);
4846
case 'P':
4947
return (
50-
BigInt(quantity.substr(0, quantity.length - 1)) *
48+
BigInt(quantity.slice(0, quantity.length - 1)) *
5149
BigInt(1000 * 1000 * 1000) *
5250
BigInt(1000 * 1000)
5351
);
5452
case 'E':
5553
return (
56-
BigInt(quantity.substr(0, quantity.length - 1)) *
54+
BigInt(quantity.slice(0, quantity.length - 1)) *
5755
BigInt(1000 * 1000 * 1000) *
5856
BigInt(1000 * 1000 * 1000)
5957
);
6058
case 'Ki':
61-
return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024);
59+
return BigInt(quantity.slice(0, quantity.length - 2)) * BigInt(1024);
6260
case 'Mi':
63-
return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024);
61+
return BigInt(quantity.slice(0, quantity.length - 2)) * BigInt(1024 * 1024);
6462
case 'Gi':
65-
return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024);
63+
return BigInt(quantity.slice(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024);
6664
case 'Ti':
67-
return (
68-
BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024) * BigInt(1024)
69-
);
65+
return BigInt(quantity.slice(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024) * BigInt(1024);
7066
case 'Pi':
7167
return (
72-
BigInt(quantity.substr(0, quantity.length - 2)) *
68+
BigInt(quantity.slice(0, quantity.length - 2)) *
7369
BigInt(1024 * 1024 * 1024) *
7470
BigInt(1024 * 1024)
7571
);
7672
case 'Ei':
7773
return (
78-
BigInt(quantity.substr(0, quantity.length - 2)) *
74+
BigInt(quantity.slice(0, quantity.length - 2)) *
7975
BigInt(1024 * 1024 * 1024) *
8076
BigInt(1024 * 1024 * 1024)
8177
);

src/watch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class Watch {
103103
};
104104
await this.config.applyToRequest(requestOptions);
105105

106-
let req;
106+
let req: RequestResult;
107107
let doneCalled: boolean = false;
108108
const doneCallOnce = (err: any) => {
109109
if (!doneCalled) {
@@ -122,7 +122,7 @@ export class Watch {
122122
stream.on('error', doneCallOnce);
123123
stream.on('close', () => doneCallOnce(null));
124124
stream.on('data', (line) => {
125-
let data;
125+
let data: { type: string; object: any };
126126

127127
try {
128128
data = JSON.parse(line);

src/web-socket-handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export class WebSocketHandler implements WebSocketInterface {
160160
}
161161
const server = cluster.server;
162162
const ssl = server.startsWith('https://');
163-
const target = ssl ? server.substr(8) : server.substr(7);
163+
const target = ssl ? server.slice(8) : server.slice(7);
164164
const proto = ssl ? 'wss' : 'ws';
165165
const uri = `${proto}://${target}${path}`;
166166

@@ -193,7 +193,7 @@ export class WebSocketHandler implements WebSocketInterface {
193193
}
194194
} else if (data instanceof Buffer) {
195195
const streamNum = data.readInt8(0);
196-
if (binaryHandler && !binaryHandler(streamNum, data.slice(1))) {
196+
if (binaryHandler && !binaryHandler(streamNum, data.subarray(1))) {
197197
client.close();
198198
}
199199
}

0 commit comments

Comments
 (0)