Skip to content

Commit

Permalink
Add several individual examples
Browse files Browse the repository at this point in the history
  • Loading branch information
danopia committed Mar 15, 2023
1 parent 16f2eaa commit dd8cc8c
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/get-resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env -S deno run --unstable --allow-env

import { autoDetectClient } from '../mod.ts';

const client = await autoDetectClient();

// Grab a single resource as JSON
console.log(await client.performRequest({
method: 'GET',
path: `/api/v1/namespaces/default/endpoints`,
expectJson: true,
querystring: new URLSearchParams({
limit: '1',
}),
}));
57 changes: 57 additions & 0 deletions examples/kubectl-port-forward.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env -S deno run --allow-run=kubectl

// This libary does not implement Kubernetes port-forwarding.
// If you would like to serve ports on localhost that proxy to a pod,
// you can launch `kubectl port-forward` as a child process to handle this.
// This example shows how you might run specific kubectl commands.

import { TextLineStream } from '../deps.ts';
import { KubectlRawRestClient } from "../mod.ts";

const client = new KubectlRawRestClient();

// Process arguments
const [ namespace, podNamePrefix, ...portForwardSpec ] = Deno.args;
if (!namespace || !podNamePrefix || !portForwardSpec.length) {
console.error(`Usage:\n\tkubectl-port-forward.ts <namespace> <pod-name-prefix> <...port-forward-spec>`);
console.error(`Example:\n\tkubectl-port-forward.ts default my-deployment- 5000:80`);
Deno.exit(5);
}

// Find the first matching pod
// This would be easier with https://deno.land/x/kubernetes_apis
const podList = await client.performRequest({
method: 'GET',
path: `/api/v1/namespaces/${namespace}/pods`,
expectJson: true,
}) as { items: Array<{ metadata: { name: string } }> };
const podName = podList.items
.map(x => x.metadata.name)
.find(name => name.startsWith(podNamePrefix));
if (!podName) {
console.error(`No pod found in ${namespace} with name prefix ${podNamePrefix}`);
Deno.exit(1);
}

// Spin up kubectl port-forward
console.log('Forwarding to pod', podName);
const [process, status] = await client.runKubectl([
'port-forward',
'-n', namespace,
'--',
podName,
...portForwardSpec,
], {});
status.then(status => {
if (status.code !== 0) {
console.error(`WARN: Failed to call kubectl port-forward: code ${status.code}`);
}
});

// Copy output lines (port-forward progress) to stderr
const stream = process.stdout.readable
for await (const line of stream
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream())) {
console.error('kubectl says:', line);
}
24 changes: 24 additions & 0 deletions examples/stream-logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env -S deno run --unstable --allow-env

import { TextLineStream } from '../deps.ts';
import { autoDetectClient } from '../mod.ts';

const client = await autoDetectClient();

// Stream plaintext log lines from a pod
const lineStream = await client.performRequest({
method: 'GET',
path: `/api/v1/namespaces/default/pods/lambdabot-0/log`,
expectStream: true,
querystring: new URLSearchParams({
timestamps: '1',
tailLines: '15',
}),
}).then(x => x
.pipeThrough(new TextDecoderStream('utf-8'))
.pipeThrough(new TextLineStream()));
for await (const line of lineStream) {
console.log(line);
}

console.log('done');
21 changes: 21 additions & 0 deletions examples/watch-resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env -S deno run --unstable --allow-env

import { autoDetectClient } from '../mod.ts';

const client = await autoDetectClient();

// Stream multiple JSON objects for a Watch operation
for await (const line of await client.performRequest({
method: 'GET',
path: `/api/v1/namespaces/default/endpoints`,
expectStream: true,
expectJson: true,
querystring: new URLSearchParams({
watch: '1',
timeoutSeconds: '5',
}),
})) {
console.log(line);
}

console.log('done');

0 comments on commit dd8cc8c

Please sign in to comment.