Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Some cleanup #1320

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,35 +277,35 @@ 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);
}

public addUser(user: User): void {
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);
}

public addContext(ctx: Context): void {
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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/exec_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/portforward.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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...
Expand Down
8 changes: 2 additions & 6 deletions src/top.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -90,10 +89,7 @@ export async function topNodes(api: CoreV1Api): Promise<NodeStatus[]> {
export async function topPods(api: CoreV1Api, metrics: Metrics, namespace?: string): Promise<PodStatus[]> {
// Figure out which pod list endpoint to call
const getPodList = async (): Promise<V1PodList> => {
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()]);
Expand Down
34 changes: 15 additions & 19 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down
4 changes: 2 additions & 2 deletions src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/web-socket-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;

Expand Down Expand Up @@ -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();
}
}
Expand Down