Skip to content
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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions FETCH_MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,28 @@ Code will be on the `master` branch.
- [x] cache-example
- [x] example
- [x] follow-logs
- [ ] in-cluster-create-job-from-cronjob
- [ ] in-cluster-create-job-from-cronjob // done but unable to test with media type problems
Copy link
Contributor Author

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.

- [x] in-cluster
- [x] ingress
- [ ] namespace
- [ ] patch-example
- [ ] raw-example (note: uses request lib directly, will require full fetch migration not just client param swap)
- [ ] scale-deployment
- [ ] namespace // done but unable to test with media type problems
- [ ] patch-example
- [x] raw-example (note: uses request lib directly, will require full fetch migration not just client param swap)
- [ ] scale-deployment // done but unable to test with media type problems
- [x] top_pods
- [x] top
- [ ] yaml-example
- [ ] yaml-example // done but unable to test with media type problems

- [ ] Fix TypeScript examples and validate their param signatures (due to new api)

- [ ] apply-example
- [ ] attach-example
- [ ] cp-example
- [ ] exec-example
- [ ] informer-with-label-selector
- [ ] informer
- [ ] informer-with-label-selector // done but unable to test with media type problems
- [x] informer
- [ ] port-forward
- [ ] example
- [ ] watch-example
- [x] example
- [x] watch-example

- [ ] Update docs
- [ ] Update README examples
Expand Down
17 changes: 10 additions & 7 deletions examples/in-cluster-create-job-from-cronjob.js
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');
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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();
Expand All @@ -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);
Expand Down
33 changes: 16 additions & 17 deletions examples/namespace.js
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);
},
);
44 changes: 27 additions & 17 deletions examples/patch-example.js
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);
});
});
45 changes: 31 additions & 14 deletions examples/raw-example.js
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}`);
});
34 changes: 25 additions & 9 deletions examples/scale-deployment.js
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();
Expand All @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we stick tonamespace and name here instead of deploymentNamespace and deploymentName?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 { name: variableName, namespace: anotherName } rather than using object shorthand property names like { name, namespace }. In general I prefer what you are saying, but as an example I find this more easier to digest.

// 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);
33 changes: 13 additions & 20 deletions examples/typescript/informer/informer-with-label-selector.ts
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();
Expand All @@ -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',
Expand All @@ -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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name, namespace?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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}`);
Expand Down
29 changes: 19 additions & 10 deletions examples/typescript/informer/informer.ts
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();
16 changes: 8 additions & 8 deletions examples/typescript/simple/example.ts
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;
Loading