Skip to content
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
17 changes: 13 additions & 4 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,23 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
private readonly callbackCache: { [key: string]: (ObjectCallback<T> | ErrorCallback)[] } = {};
private request: AbortController | undefined;
private stopped: boolean = false;
private readonly path: string;
private readonly watch: Watch;
private readonly listFn: ListPromise<T>;
private readonly labelSelector?: string;

public constructor(
private readonly path: string,
private readonly watch: Watch,
private readonly listFn: ListPromise<T>,
path: string,
watch: Watch,
listFn: ListPromise<T>,
autoStart: boolean = true,
private readonly labelSelector?: string,
labelSelector?: string,
) {
this.path = path;
this.watch = watch;
this.listFn = listFn;
this.labelSelector = labelSelector;

this.callbackCache[ADD] = [];
this.callbackCache[UPDATE] = [];
this.callbackCache[DELETE] = [];
Expand Down
10 changes: 6 additions & 4 deletions src/config_types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import fs from 'node:fs';

export enum ActionOnInvalid {
THROW = 'throw',
FILTER = 'filter',
}
export const ActionOnInvalid = {
THROW: 'throw',
FILTER: 'filter',
} as const;

export type ActionOnInvalid = (typeof ActionOnInvalid)[keyof typeof ActionOnInvalid];

export interface ConfigOptions {
onInvalidEntry: ActionOnInvalid;
Expand Down
5 changes: 4 additions & 1 deletion src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ export class KubernetesObjectApi {
/** Cache resource API response. */
protected apiVersionResourceCache: Record<string, V1APIResourceList> = {};

constructor(protected configuration: Configuration) {}
protected configuration: Configuration;
constructor(configuration: Configuration) {
this.configuration = configuration;
}

/**
* Create any Kubernetes resource.
Expand Down
5 changes: 4 additions & 1 deletion src/oidc_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ interface Client {
}

class OidcClient implements Client {
public constructor(readonly config: oidc.Configuration) {}
readonly config: oidc.Configuration;
public constructor(c: oidc.Configuration) {
this.config = c;
}

public async refresh(token: string): Promise<Token> {
const newToken = await oidc.refreshTokenGrant(this.config, token);
Expand Down
14 changes: 8 additions & 6 deletions src/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
* Additionally for Server-Side Apply https://kubernetes.io/docs/reference/using-api/server-side-apply/
* and https://kubernetes.io/docs/reference/using-api/server-side-apply/#api-implementation
*/
export enum PatchStrategy {
export const PatchStrategy = {
/** Diff-like JSON format. */
JsonPatch = 'application/json-patch+json',
JsonPatch: 'application/json-patch+json',
/** Simple merge. */
MergePatch = 'application/merge-patch+json',
MergePatch: 'application/merge-patch+json',
/** Merge with different strategies depending on field metadata. */
StrategicMergePatch = 'application/strategic-merge-patch+json',
StrategicMergePatch: 'application/strategic-merge-patch+json',
/** Server-Side Apply */
ServerSideApply = 'application/apply-patch+yaml',
}
ServerSideApply: 'application/apply-patch+yaml',
} as const;

export type PatchStrategy = (typeof PatchStrategy)[keyof typeof PatchStrategy];
76 changes: 51 additions & 25 deletions src/top.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,70 @@ import {
} from './util.js';

export class ResourceUsage {
constructor(
public readonly Capacity: number | bigint,
public readonly RequestTotal: number | bigint,
public readonly LimitTotal: number | bigint,
) {}
public readonly Capacity: number | bigint;
public readonly RequestTotal: number | bigint;
public readonly LimitTotal: number | bigint;

constructor(Capacity: number | bigint, RequestTotal: number | bigint, LimitTotal: number | bigint) {
this.Capacity = Capacity;
this.RequestTotal = RequestTotal;
this.LimitTotal = LimitTotal;
}
}

export class CurrentResourceUsage {
constructor(
public readonly CurrentUsage: number | bigint,
public readonly RequestTotal: number | bigint,
public readonly LimitTotal: number | bigint,
) {}
public readonly CurrentUsage: number | bigint;
public readonly RequestTotal: number | bigint;
public readonly LimitTotal: number | bigint;

constructor(CurrentUsage: number | bigint, RequestTotal: number | bigint, LimitTotal: number | bigint) {
this.CurrentUsage = CurrentUsage;
this.RequestTotal = RequestTotal;
this.LimitTotal = LimitTotal;
}
}

export class NodeStatus {
constructor(
public readonly Node: V1Node,
public readonly CPU: ResourceUsage,
public readonly Memory: ResourceUsage,
) {}
public readonly Node: V1Node;
public readonly CPU: ResourceUsage;
public readonly Memory: ResourceUsage;

constructor(Node: V1Node, CPU: ResourceUsage, Memory: ResourceUsage) {
this.Node = Node;
this.CPU = CPU;
this.Memory = Memory;
}
}

export class ContainerStatus {
constructor(
public readonly Container: string,
public readonly CPUUsage: CurrentResourceUsage,
public readonly MemoryUsage: CurrentResourceUsage,
) {}
public readonly Container: string;
public readonly CPUUsage: CurrentResourceUsage;
public readonly MemoryUsage: CurrentResourceUsage;

constructor(Container: string, CPUUsage: CurrentResourceUsage, MemoryUsage: CurrentResourceUsage) {
this.Container = Container;
this.CPUUsage = CPUUsage;
this.MemoryUsage = MemoryUsage;
}
}

export class PodStatus {
public readonly Pod: V1Pod;
public readonly CPU: CurrentResourceUsage;
public readonly Memory: CurrentResourceUsage;
public readonly Containers: ContainerStatus[];

constructor(
public readonly Pod: V1Pod,
public readonly CPU: CurrentResourceUsage,
public readonly Memory: CurrentResourceUsage,
public readonly Containers: ContainerStatus[],
) {}
Pod: V1Pod,
CPU: CurrentResourceUsage,
Memory: CurrentResourceUsage,
Containers: ContainerStatus[],
) {
this.Pod = Pod;
this.CPU = CPU;
this.Memory = Memory;
this.Containers = Containers;
}
}

export async function topNodes(api: CoreV1Api): Promise<NodeStatus[]> {
Expand Down
14 changes: 9 additions & 5 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,15 @@ export function quantityToScalar(quantity: string): number | bigint {
}

export class ResourceStatus {
constructor(
public readonly request: bigint | number,
public readonly limit: bigint | number,
public readonly resourceType: string,
) {}
public readonly request: bigint | number;
public readonly limit: bigint | number;
public readonly resourceType: string;

constructor(request: bigint | number, limit: bigint | number, resourceType: string) {
this.request = request;
this.limit = limit;
this.resourceType = resourceType;
}
}

export function totalCPUForContainer(container: V1Container): ResourceStatus {
Expand Down
20 changes: 16 additions & 4 deletions src/web-socket-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,32 @@ export class WebSocketHandler implements WebSocketInterface {
return () => ws;
}

readonly config: KubeConfig;
readonly socketFactory?: (
uri: string,
protocols: string[],
opts: WebSocket.ClientOptions,
) => WebSocket.WebSocket;
readonly streams: StreamInterface;

// factory is really just for test injection
public constructor(
readonly config: KubeConfig,
readonly socketFactory?: (
kc: KubeConfig,
socketFactoryFn?: (
uri: string,
protocols: string[],
opts: WebSocket.ClientOptions,
) => WebSocket.WebSocket,
readonly streams: StreamInterface = {
streamsInterface: StreamInterface = {
stdin: process.stdin,
stdout: process.stdout,
stderr: process.stderr,
},
) {}
) {
this.config = kc;
this.socketFactory = socketFactoryFn;
this.streams = streamsInterface;
}

/**
* Connect to a web socket endpoint.
Expand Down