Comprehensive Node.js client library for interacting with Kubernetes clusters with full API coverage and TypeScript support
npx @tessl/cli install tessl/npm-kubernetes--client-node@1.3.0The Kubernetes Client Node library provides a comprehensive TypeScript/JavaScript client for interacting with Kubernetes clusters from Node.js applications. It offers complete Kubernetes API coverage through auto-generated client code, high-level convenience classes for common operations, and robust authentication support for various cloud providers.
npm install @kubernetes/client-nodeimport {
KubeConfig,
CoreV1Api,
AppsV1Api,
Watch,
Log,
Exec,
Attach,
PortForward,
Metrics
} from '@kubernetes/client-node';For CommonJS:
const {
KubeConfig,
CoreV1Api,
AppsV1Api,
Watch,
Log,
Exec,
Attach,
PortForward,
Metrics
} = require('@kubernetes/client-node');import { KubeConfig, CoreV1Api } from '@kubernetes/client-node';
// Load configuration
const kc = new KubeConfig();
kc.loadFromDefault();
// Create API client
const k8sApi = kc.makeApiClient(CoreV1Api);
// List pods in default namespace
try {
const res = await k8sApi.listNamespacedPod('default');
console.log('Found pods:', res.body.items.map(pod => pod.metadata?.name));
} catch (err) {
console.error('Error:', err);
}The Kubernetes Client Node library is organized around several key components:
KubeConfig class handles cluster connection, authentication, and context managementCore configuration management and authentication for Kubernetes cluster access. Supports multiple authentication methods and flexible configuration loading.
class KubeConfig {
loadFromDefault(options?: ConfigOptions): void;
loadFromFile(file: string): void;
loadFromString(config: string): void;
loadFromCluster(): void;
makeApiClient<T>(apiClientType: ApiType<T>): T;
}Configuration and Authentication
Complete Kubernetes API coverage with type-safe client classes for all API groups. Provides full CRUD operations for all Kubernetes resources.
class CoreV1Api {
listNamespacedPod(namespace: string, options?: object): Promise<KubernetesListObject<V1Pod>>;
createNamespacedPod(namespace: string, body: V1Pod, options?: object): Promise<V1Pod>;
deleteNamespacedPod(name: string, namespace: string, options?: object): Promise<V1Status>;
}
class AppsV1Api {
listNamespacedDeployment(namespace: string, options?: object): Promise<KubernetesListObject<V1Deployment>>;
createNamespacedDeployment(namespace: string, body: V1Deployment, options?: object): Promise<V1Deployment>;
}High-level classes for interactive pod operations including command execution, container attachment, port forwarding, and file transfer.
class Exec {
exec(
namespace: string,
podName: string,
containerName: string,
command: string | string[],
stdout: stream.Writable | null,
stderr: stream.Writable | null,
stdin: stream.Readable | null,
tty: boolean,
statusCallback?: (status: V1Status) => void
): Promise<WebSocket.WebSocket>;
}
class PortForward {
portForward(
namespace: string,
podName: string,
targetPorts: number[],
output: stream.Writable,
err: stream.Writable | null,
input: stream.Readable,
retryCount?: number
): Promise<WebSocket.WebSocket | (() => WebSocket.WebSocket | null)>;
}Real-time monitoring tools for watching resource changes, streaming logs, collecting metrics, and implementing the informer pattern for efficient resource caching.
class Watch {
watch(
path: string,
queryParams: Record<string, string | number | boolean | undefined>,
callback: (phase: string, apiObj: any, watchObj?: any) => void,
done: (err: any) => void
): Promise<AbortController>;
}
class Log {
log(
namespace: string,
podName: string,
containerName: string,
stream: Writable,
options?: LogOptions
): Promise<AbortController>;
}
class Metrics {
getNodeMetrics(): Promise<NodeMetricsList>;
getPodMetrics(namespace?: string): Promise<PodMetricsList>;
}Generic object management, YAML processing, health checking, and utility functions for working with Kubernetes resources and cluster operations.
class KubernetesObjectApi {
static makeApiClient(kc: KubeConfig): KubernetesObjectApi;
create(obj: KubernetesObject, options?: object): Promise<KubernetesObject>;
delete(obj: KubernetesObject, options?: object): Promise<V1Status>;
read(obj: KubernetesObject): Promise<KubernetesObject>;
patch(obj: KubernetesObject, patch: object, options?: object): Promise<KubernetesObject>;
replace(obj: KubernetesObject, options?: object): Promise<KubernetesObject>;
}
function loadYaml<T>(data: string, opts?: yaml.LoadOptions): T;
function dumpYaml(object: any, opts?: yaml.DumpOptions): string;Object Management and Utilities
interface KubernetesObject {
apiVersion?: string;
kind?: string;
metadata?: V1ObjectMeta;
}
interface KubernetesListObject<T> {
apiVersion?: string;
kind?: string;
metadata?: V1ListMeta;
items: T[];
}
interface ConfigOptions {
onInvalidEntry?: ActionOnInvalid;
}
interface LogOptions {
follow?: boolean;
limitBytes?: number;
pretty?: boolean;
previous?: boolean;
sinceSeconds?: number;
sinceTime?: string;
tailLines?: number;
timestamps?: boolean;
}
type IntOrString = number | string;
interface ApiType<T = any> {
new(config?: Configuration): T;
}
type KubernetesApiAction = 'create' | 'delete' | 'patch' | 'read' | 'list' | 'replace';
interface RequestOptions {
/** Request timeout in milliseconds */
timeout?: number;
/** Additional headers */
headers?: Record<string, string>;
}
interface ConfigurationOptions {
/** Base path for API requests */
basePath?: string;
/** HTTP timeout in milliseconds */
timeout?: number;
/** Additional headers */
headers?: Record<string, string>;
/** Request middleware */
middleware?: any[];
}
interface ApiException extends Error {
/** HTTP status code */
code?: number;
/** Response body */
body?: any;
/** Response headers */
headers?: any;
}