-
Notifications
You must be signed in to change notification settings - Fork 520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: update js and typescript examples #1082
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
const k8s = require('@kubernetes/client-node'); | ||
// in a real program use require('@kubernetes/client-node') | ||
const k8s = require('../dist/index'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did this to match the previous PR. |
||
|
||
const kc = new k8s.KubeConfig(); | ||
kc.loadFromCluster(); | ||
|
||
const batchV1Api = kc.makeApiClient(k8s.BatchV1Api); | ||
const batchV1beta1Api = kc.makeApiClient(k8s.BatchV1beta1Api); | ||
const cronJobName = 'myCronJob'; | ||
const jobName = 'myJob'; | ||
const cronJobName = 'cronjob'; | ||
const jobName = 'myjob'; | ||
|
||
const job = new k8s.V1Job(); | ||
const metadata = new k8s.V1ObjectMeta(); | ||
|
@@ -18,12 +19,14 @@ metadata.annotations = { | |
}; | ||
job.metadata = metadata; | ||
|
||
batchV1beta1Api.readNamespacedCronJob(cronJobName, 'default') | ||
batchV1beta1Api | ||
.readNamespacedCronJob({ name: cronJobName, namespace: 'default' }) | ||
.then((cronJobRes) => { | ||
job.spec = cronJobRes.body.spec.jobTemplate.spec; | ||
batchV1Api.createNamespacedJob('default', job) | ||
job.spec = cronJobRes?.spec?.jobTemplate.spec; | ||
batchV1Api | ||
.createNamespacedJob({ namespace: 'default', body: job }) | ||
.then((res) => { | ||
console.log(res.body); | ||
console.log(res); | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,27 @@ | ||
const k8s = require('@kubernetes/client-node'); | ||
// in a real program use require('@kubernetes/client-node') | ||
const k8s = require('../dist/index'); | ||
|
||
const kc = new k8s.KubeConfig(); | ||
kc.loadFromDefault(); | ||
|
||
const k8sApi = kc.makeApiClient(k8s.CoreV1Api); | ||
|
||
var namespace = { | ||
metadata: { | ||
name: 'test' | ||
} | ||
metadata: { | ||
name: 'test', | ||
}, | ||
}; | ||
|
||
k8sApi.createNamespace(namespace).then( | ||
(response) => { | ||
console.log('Created namespace'); | ||
console.log(response); | ||
k8sApi.readNamespace(namespace.metadata.name).then( | ||
(response) => { | ||
k8sApi.createNamespace({ body: namespace }).then( | ||
(response) => { | ||
console.log('Created namespace'); | ||
console.log(response); | ||
k8sApi.deleteNamespace( | ||
namespace.metadata.name, {} /* delete options */); | ||
}); | ||
}, | ||
(err) => { | ||
console.log('Error!: ' + err); | ||
} | ||
k8sApi.readNamespace({ name: namespace.metadata.name }).then((response) => { | ||
console.log(response); | ||
k8sApi.deleteNamespace({ name: namespace.metadata.name }); | ||
}); | ||
}, | ||
(err) => { | ||
console.log('Error!: ' + err); | ||
}, | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,33 @@ | ||
const k8s = require('@kubernetes/client-node'); | ||
// in a real program use require('@kubernetes/client-node') | ||
const k8s = require('../dist/index'); | ||
|
||
const kc = new k8s.KubeConfig(); | ||
kc.loadFromDefault(); | ||
|
||
const k8sApi = kc.makeApiClient(k8s.CoreV1Api); | ||
|
||
k8sApi.listNamespacedPod('default') | ||
.then((res) => { | ||
const patch = [ | ||
{ | ||
"op": "replace", | ||
"path":"/metadata/labels", | ||
"value": { | ||
"foo": "bar" | ||
} | ||
} | ||
]; | ||
const options = { "headers": { "Content-type": k8s.PatchUtils.PATCH_FORMAT_JSON_PATCH}}; | ||
k8sApi.patchNamespacedPod(res.body.items[0].metadata.name, 'default', patch, undefined, undefined, undefined, undefined, options) | ||
.then(() => { console.log("Patched.")}) | ||
.catch((err) => { console.log("Error: "); console.log(err)}); | ||
}); | ||
k8sApi.listNamespacedPod({ namespace: 'default' }).then((res) => { | ||
const patch = [ | ||
{ | ||
op: 'replace', | ||
path: '/metadata/labels', | ||
value: { | ||
foo: 'bar', | ||
}, | ||
}, | ||
]; | ||
// TODO this method of passing the content type will change when we figure out a way to properly do this | ||
const options = { headers: { 'Content-type': k8s.PatchUtils.PATCH_FORMAT_JSON_PATCH } }; | ||
k8sApi | ||
.patchNamespacedPod( | ||
{ name: res?.items?.[0].metadata?.name ?? '', namespace: 'default', body: patch }, | ||
options, | ||
) | ||
.then(() => { | ||
console.log('Patched.'); | ||
}) | ||
.catch((err) => { | ||
console.log('Error: '); | ||
console.log(err); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,37 @@ | ||
const k8s = require('@kubernetes/client-node'); | ||
const request = require('request'); | ||
// in a real program use require('@kubernetes/client-node') | ||
const k8s = require('../dist/index'); | ||
const fetch = require('node-fetch'); | ||
const https = require('https'); | ||
|
||
const kc = new k8s.KubeConfig(); | ||
kc.loadFromDefault(); | ||
|
||
const opts = {}; | ||
kc.applyToRequest(opts); | ||
const currentUser = kc.getCurrentUser(); | ||
const currentCluster = kc.getCurrentCluster(); | ||
|
||
request.get(`${kc.getCurrentCluster().server}/api/v1/namespaces/default/pods`, opts, | ||
(error, response, body) => { | ||
if (error) { | ||
console.log(`error: ${error}`); | ||
} | ||
if (response) { | ||
console.log(`statusCode: ${response.statusCode}`); | ||
} | ||
console.log(`body: ${body}`); | ||
}); | ||
const agent = new https.Agent({ | ||
clintonmedbery marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ca: Buffer.from(currentCluster?.caData ?? '', 'base64').toString('utf8'), | ||
cert: Buffer.from(currentUser?.certData ?? '', 'base64').toString('utf8'), | ||
keepAlive: true, | ||
key: Buffer.from(currentUser?.keyData ?? '', 'base64').toString('utf8'), | ||
}); | ||
|
||
const opts = { | ||
headers: {}, | ||
agent: agent, | ||
}; | ||
|
||
kc.applyToHTTPSOptions(opts); | ||
|
||
const url = `${kc?.getCurrentCluster()?.server}/api/v1/namespaces/default/pods`; | ||
fetch(url, opts) | ||
.then((response) => { | ||
console.log(`statusCode: ${response.status}`); | ||
return response.text(); | ||
}) | ||
.then((body) => { | ||
console.log(`body: ${body}`); | ||
}) | ||
.catch((error) => { | ||
console.log(`error: ${error}`); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
const k8s = require('@kubernetes/client-node'); | ||
// in a real program use require('@kubernetes/client-node') | ||
const k8s = require('../dist/index'); | ||
|
||
const kc = new k8s.KubeConfig(); | ||
kc.loadFromDefault(); | ||
|
@@ -9,16 +10,31 @@ const targetNamespaceName = 'default'; | |
const targetDeploymentName = 'docker-test-deployment'; | ||
const numberOfTargetReplicas = 3; | ||
|
||
async function scale(namespace, name, replicas) { | ||
// find the particular deployment | ||
const res = await k8sApi.readNamespacedDeployment(name, namespace); | ||
let deployment = res.body; | ||
async function scale(deploymentNamespace, deploymentName, replicas) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we stick to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason I did this was because as an example, I think it is easier to read the named parameters as |
||
// find the particular deployment | ||
const deployment = await k8sApi.readNamespacedDeployment({ | ||
name: deploymentName, | ||
namespace: deploymentNamespace, | ||
}); | ||
|
||
// edit | ||
deployment.spec.replicas = replicas; | ||
if (!deployment || !deployment.spec) { | ||
throw new Error(`Deployment ${deploymentName} not found in namespace ${deploymentNamespace}`); | ||
} | ||
// edit | ||
const newDeployment = { | ||
...deployment, | ||
spec: { | ||
...deployment.spec, | ||
replicas, | ||
}, | ||
}; | ||
|
||
// replace | ||
await k8sApi.replaceNamespacedDeployment(name, namespace, deployment); | ||
// replace | ||
await k8sApi.replaceNamespacedDeployment({ | ||
name: deploymentName, | ||
namespace: deploymentNamespace, | ||
body: newDeployment, | ||
}); | ||
} | ||
|
||
scale(targetNamespaceName, targetDeploymentName, numberOfTargetReplicas); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// tslint:disable:no-console | ||
import * as k8s from '@kubernetes/client-node'; | ||
// in a real program use require('@kubernetes/client-node') | ||
import * as k8s from '../../../dist/index'; | ||
|
||
const kc = new k8s.KubeConfig(); | ||
kc.loadFromDefault(); | ||
|
@@ -8,16 +9,13 @@ const k8sApi = kc.makeApiClient(k8s.CoreV1Api); | |
|
||
const APP_LABEL_SELECTOR = 'app=foo'; | ||
|
||
const listFn = () => k8sApi.listNamespacedPod( | ||
'default', | ||
undefined, | ||
undefined, | ||
undefined, | ||
undefined, | ||
APP_LABEL_SELECTOR, | ||
); | ||
const listFn = () => | ||
k8sApi.listNamespacedPod({ | ||
namespace: 'default', | ||
labelSelector: APP_LABEL_SELECTOR, | ||
}); | ||
|
||
const createPod = async (name, app) => { | ||
const createPod = async (name: string, app: string) => { | ||
const appPodContainer = { | ||
name: 'nginx', | ||
image: 'nginx:latest', | ||
|
@@ -34,25 +32,20 @@ const createPod = async (name, app) => { | |
containers: [appPodContainer], | ||
}, | ||
} as k8s.V1Pod; | ||
await k8sApi.createNamespacedPod('default', appPod).catch((e) => console.error(e)); | ||
await k8sApi.createNamespacedPod({ namespace: 'default', body: appPod }).catch((e) => console.error(e)); | ||
console.log('create', name); | ||
}; | ||
|
||
const deletePod = async (name, namespace) => { | ||
await k8sApi.deleteNamespacedPod(name, namespace); | ||
const deletePod = async (podName: string, podNamespace: string) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same reason above. |
||
await k8sApi.deleteNamespacedPod({ name: podName, namespace: podNamespace }); | ||
console.log('delete', name); | ||
}; | ||
|
||
const delay = (ms) => { | ||
const delay = (ms: number) => { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
}; | ||
|
||
const informer = k8s.makeInformer( | ||
kc, | ||
'/api/v1/namespaces/default/pods', | ||
listFn, | ||
APP_LABEL_SELECTOR, | ||
); | ||
const informer = k8s.makeInformer(kc, '/api/v1/namespaces/default/pods', listFn, APP_LABEL_SELECTOR); | ||
|
||
informer.on('add', (obj: k8s.V1Pod) => { | ||
console.log(`Added: ${obj.metadata!.name}`); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,33 @@ | ||
// tslint:disable:no-console | ||
import * as k8s from '@kubernetes/client-node'; | ||
|
||
// in a real program use require('@kubernetes/client-node') | ||
import * as k8s from '../../../dist/index'; | ||
|
||
const kc = new k8s.KubeConfig(); | ||
|
||
kc.loadFromDefault(); | ||
|
||
const k8sApi = kc.makeApiClient(k8s.CoreV1Api); | ||
|
||
const listFn = () => k8sApi.listNamespacedPod('default'); | ||
const listFn = () => k8sApi.listNamespacedPod({ namespace: 'default' }); | ||
|
||
const informer = k8s.makeInformer(kc, '/api/v1/namespaces/default/pods', listFn); | ||
|
||
informer.on('add', (obj: k8s.V1Pod) => { console.log(`Added: ${obj.metadata!.name}`); }); | ||
informer.on('update', (obj: k8s.V1Pod) => { console.log(`Updated: ${obj.metadata!.name}`); }); | ||
informer.on('delete', (obj: k8s.V1Pod) => { console.log(`Deleted: ${obj.metadata!.name}`); }); | ||
informer.on('add', (obj: k8s.V1Pod) => { | ||
console.log(`Added: ${obj.metadata!.name}`); | ||
}); | ||
informer.on('update', (obj: k8s.V1Pod) => { | ||
console.log(`Updated: ${obj.metadata!.name}`); | ||
}); | ||
informer.on('delete', (obj: k8s.V1Pod) => { | ||
console.log(`Deleted: ${obj.metadata!.name}`); | ||
}); | ||
informer.on('error', (err: k8s.V1Pod) => { | ||
console.error(err); | ||
// Restart informer after 5sec | ||
setTimeout(() => { | ||
informer.start(); | ||
}, 5000); | ||
console.error(err); | ||
// Restart informer after 5sec | ||
setTimeout(() => { | ||
informer.start(); | ||
}, 5000); | ||
}); | ||
|
||
informer.start(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
import k8s = require('@kubernetes/client-node'); | ||
// in a real program use require('@kubernetes/client-node') | ||
import * as k8s from '../../../dist/index'; | ||
|
||
const kc = new k8s.KubeConfig(); | ||
kc.loadFromDefault(); | ||
|
||
const k8sApi = kc.makeApiClient(k8s.CoreV1Api); | ||
|
||
k8sApi.listNamespacedPod('default') | ||
.then((res) => { | ||
// tslint:disable-next-line:no-console | ||
console.log(res.body); | ||
}); | ||
k8sApi.listNamespacedPod({ namespace: 'default' }).then((res) => { | ||
// tslint:disable-next-line:no-console | ||
console.log(res.body); | ||
}); | ||
|
||
// Example of instantiating a Pod object. | ||
const pod = { | ||
} as k8s.V1Pod; | ||
const pod = {} as k8s.V1Pod; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if there is a better or more preferred way to denote this.