Kubectl API Reference
Complete method reference for the kubectl plugin.
Run and Read
run(args)
Execute a raw kubectl command and return the output string.
const output = await k.run('get pods -o wide');command(args)
Build a kubectl command string without executing it.
const cmd = k.command('get pods');
// Returns: "kubectl get pods -n default"get(resource, name?, options?)
Get resources as parsed JSON.
// Get a specific resource
const pod = await k.get('pod', 'nginx');
// List resources
const pods = await k.get('pods');
// With options
const filtered = await k.get('pods', undefined, {
selector: 'app=nginx',
fieldSelector: 'status.phase=Running',
allNamespaces: true,
output: 'wide',
});Get Options
| Option | Type | Description |
|---|---|---|
selector | string | Label selector |
fieldSelector | string | Field selector |
allNamespaces | boolean | Search across all namespaces |
jqFilter | string | JQ filter expression |
watch | boolean | Watch for changes (returns WatchProcess) |
output | string | Output format override |
getJsonPath(resource, name, jsonPath, options?)
Get a specific field via JSONPath.
const phase = await k.getJsonPath('pod', 'nginx', '.status.phase');exists(resource, name)
Check if a resource exists in the cluster.
const found = await k.exists('pod', 'nginx'); // booleanclusterInfo()
Get cluster connectivity information.
const info = await k.clusterInfo();
// { reachable, controlPlane?, coreDNS?, serverVersion?, error? }Resource Lifecycle
apply(yaml)
Create or update resources from YAML.
const result = await k.apply(`
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
`);delete(resource, name, options?)
Delete a resource. Treats "not found" as success.
await k.delete('pod', 'nginx');
await k.delete('pod', 'nginx', { force: true });waitForPod(name, status?, timeout?)
Wait for a pod to reach a specific status.
// Default: wait for "Running", timeout 60s
await k.waitForPod('nginx');
// Custom status and timeout
await k.waitForPod('nginx', 'Succeeded', 120_000);| Parameter | Default | Description |
|---|---|---|
status | 'Running' | Target status: Running, Succeeded, Failed, Pending |
timeout | 60000 | Timeout in milliseconds |
wait(resource, name, condition, options?)
Wait for a generic condition.
await k.wait('deployment', 'app', 'Available', { timeout: 120_000 });
await k.wait('pod', 'nginx', 'delete', { forDelete: true });waitForJsonPath(resource, name, jsonPath, value, timeout?)
Wait for a specific field to reach a value.
await k.waitForJsonPath('pod', 'nginx', '.status.phase', 'Running', 60_000);waitForReplicas(resource, name, count, timeout?)
Wait for a deployment/statefulset to reach a replica count.
await k.waitForReplicas('deployment', 'app', 3, 120_000);waitForService(name, timeout?)
Wait for a service to have endpoints.
await k.waitForService('my-svc', 30_000);Operations
logs(pod, options?)
Get pod logs.
const logs = await k.logs('nginx');
const logs = await k.logs('nginx', {
container: 'sidecar',
tail: 100,
since: '5m',
previous: true,
});exec(pod, command, options?)
Execute a command inside a pod.
const result = await k.exec('nginx', 'cat /etc/hostname');
const result = await k.exec('nginx', 'ls', { container: 'sidecar' });describe(resource, name?)
Get resource description.
const desc = await k.describe('pod', 'nginx');scale(resource, name, replicas)
Scale a deployment or statefulset.
await k.scale('deployment', 'app', 5);patch(resource, name, patch, type?)
Patch a resource.
await k.patch('deployment', 'app', { spec: { replicas: 3 } });
await k.patch('deployment', 'app', patch, 'merge'); // "strategic" | "merge" | "json"label(resource, name, labels)
Add or update labels.
await k.label('pod', 'nginx', { env: 'staging', team: 'backend' });annotate(resource, name, annotations)
Add or update annotations.
await k.annotate('pod', 'nginx', { 'description': 'test pod' });rollout
Manage rollouts for deployments and statefulsets.
await k.rollout.status('deployment', 'app');
await k.rollout.restart('deployment', 'app');
await k.rollout.pause('deployment', 'app');
await k.rollout.resume('deployment', 'app');
await k.rollout.undo('deployment', 'app'); // Latest revision
await k.rollout.undo('deployment', 'app', 2); // Specific revision
await k.rollout.history('deployment', 'app');portForward(resource, ports, options?)
Forward ports from a resource.
const pf = await k.portForward('pod/nginx', '8080:80');
console.log('Local port:', pf.localPort);
// Stop forwarding when done
pf.stop();| Option | Default | Description |
|---|---|---|
address | '127.0.0.1' | Local address to bind |
delay | 2000 | Delay in ms after starting to ensure connection |
getEvents(options?)
Get cluster events.
const events = await k.getEvents();
const events = await k.getEvents({ fieldSelector: 'involvedObject.name=nginx' });getNodes(options?)
Get cluster nodes.
const nodes = await k.getNodes();
const nodes = await k.getNodes({ selector: 'node-role.kubernetes.io/worker' });cp(source, dest, options?)
Copy files to/from pods.
await k.cp('/local/file.txt', 'nginx:/tmp/file.txt');
await k.cp('nginx:/tmp/file.txt', '/local/file.txt');Namespace and Cluster
setNamespace(namespace)
Change the namespace for subsequent commands.
k.setNamespace('kube-system');
await k.get('pods'); // Now queries kube-systemgetNamespace()
Get the current namespace.
const ns = k.getNamespace(); // e.g. "default"getKubeconfig()
Get the current kubeconfig path, if set.
const kubeconfig = k.getKubeconfig(); // string | undefinedsetCluster(kubeConfigPath)
Return a shell command to set the KUBECONFIG environment variable.
const exportCmd = k.setCluster('/path/to/kubeconfig');
// Returns: "export KUBECONFIG='/path/to/kubeconfig'"Watch
When using get() with { watch: true }, a WatchProcess is returned instead of data. You must call interrupt() to stop the watch:
const watch = await k.get('pods', undefined, { watch: true, output: 'wide' });
// ... observe changes ...
await watch.interrupt();