-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}), | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |