Skip to content

chore: update examples node fetch #1074

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

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
36 changes: 24 additions & 12 deletions FETCH_MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,41 @@ Code will be on the `master` branch.
- [x] Fix errors in /src folder (due to new api)
- [x] migrate src/auth.ts, the dependent implementations (ex: azure_auth, gcp_auth etc) and tests to fetch api from request
- [x] migrate src/log.ts and its tests to fetch api from request
- [x] major remaining work is fixing up async signatures and return piping
- [x] major remaining work is fixing up async signatures and return piping
- [x] migrate src/watch.ts and its tests to fetch api from request
- [x] remove decprecated requestImpl and RequestInterface
- [x] implement queryParams parameter in watch method by injecting them into the fetch call
- [x] update tests in src/watch_test.ts
- [x] remove decprecated requestImpl and RequestInterface
- [x] implement queryParams parameter in watch method by injecting them into the fetch call
- [x] update tests in src/watch_test.ts
- [x] Fix errors in test (due to new api)
- [ ] Test all features
- [ ] Fix examples and validate their param signatures (due to new api)
- [ ] Fix JavaScript examples and validate their param signatures (due to new api)

- [ ] cache-example
- [ ] example
- [ ] follow-logs
- [x] cache-example
- [x] example
- [x] follow-logs
- [ ] in-cluster-create-job-from-cronjob
- [ ] in-cluster
- [ ] ingress
- [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
- [ ] top_pods
- [ ] top
- [x] top_pods
- [x] top
- [ ] yaml-example

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

Choose a reason for hiding this comment

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

We didn't account for the TypeScript examples before.


- [ ] apply-example
- [ ] attach-example
- [ ] cp-example
- [ ] exec-example
- [ ] informer-with-label-selector
- [ ] informer
- [ ] port-forward
- [ ] example
- [ ] watch-example

- [ ] Update docs
- [ ] Update README examples
- [ ] Document breaking changes for users
Expand Down
7 changes: 4 additions & 3 deletions examples/cache-example.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 @@ -8,7 +9,7 @@ const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
const path = '/api/v1/pods';
const watch = new k8s.Watch(kc);

const listFn = () => k8sApi.listPodForAllNamespaces()
const listFn = () => k8sApi.listPodForAllNamespaces();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some of these examples got formatted with prettier.


const cache = new k8s.ListWatch(path, watch, listFn);

Expand All @@ -22,6 +23,6 @@ const looper = () => {
console.log(names.join(','));
}
setTimeout(looper, 2000);
}
};

looper();
12 changes: 8 additions & 4 deletions examples/example.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
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')
k8sApi
.listNamespacedPod({ namespace: 'default' })
.then((res) => {
console.log(res.body);
console.log(res);
})
.catch((err) => {
console.error(err);
});

31 changes: 20 additions & 11 deletions examples/follow-logs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const stream = require('stream');
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,15 +10,23 @@ const log = new k8s.Log(kc);
const logStream = new stream.PassThrough();

logStream.on('data', (chunk) => {
// use write rather than console.log to prevent double line feed
process.stdout.write(chunk);
// use write rather than console.log to prevent double line feed
process.stdout.write(chunk);
});

log.log('default', 'pod1', 'container1', logStream, {follow: true, tailLines: 50, pretty: false, timestamps: false})
.catch(err => {console.log(err)})
.then(req => {
// disconnects after 5 seconds
setTimeout(function(){
req.abort();
}, 5000);
});
log.log('default', 'pod1', 'container1', logStream, {
follow: true,
tailLines: 50,
pretty: false,
timestamps: false,
})
.catch((err) => {
console.error(err);
})
.then((req) => {
// disconnects after 5 seconds
setTimeout(function () {
//Note: You might have to install AbortController if you are using node version < 15.0.0
req.abort();
}, 5000);
});
11 changes: 6 additions & 5 deletions examples/in-cluster.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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.loadFromCluster();

const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

k8sApi.listNamespacedPod('default')
k8sApi
.listNamespacedPod({ namespace: 'default' })
.then((res) => {
console.log(res.body);
console.log(res);
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason you've switched that to the complete res object?

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 didn't. That is what this function is now returning. This example now reflects that.

})
.catch((err) => {
console.log(err);
console.error(err);
});

64 changes: 39 additions & 25 deletions examples/ingress.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
const k8s = require('@kubernetes/client-node')
const kc = new k8s.KubeConfig()
kc.loadFromDefault()
// in a real program use require('@kubernetes/client-node')
const k8s = require('../dist/index');

const k8sApi = kc.makeApiClient(k8s.NetworkingV1beta1Api) // before 1.14 use extensions/v1beta1
const clientIdentifier = 'my-subdomain'
const kc = new k8s.KubeConfig();
kc.loadFromDefault();

k8sApi.createNamespacedIngress('default', {
apiVersions: 'networking.k8s.io/v1beta1',
kind: 'Ingress',
metadata: { name: `production-custom-${clientIdentifier}` },
spec: {
rules: [{
host: `${clientIdentifier}.example.com`,
http: {
paths: [{
backend: {
serviceName: 'production-auto-deploy',
servicePort: 5000
},
path: '/'
}]
}
}],
tls: [{ hosts: [`${clientIdentifier}.example.com`] }]
}
}).catch(e => console.log(e))
const k8sApi = kc.makeApiClient(k8s.NetworkingV1Api);
const clientIdentifier = 'my-subdomain';

k8sApi
.createNamespacedIngress({
namespace: 'default',
body: {
apiVersion: 'networking.k8s.io/v1',
kind: 'Ingress',
metadata: { name: `production-custom-${clientIdentifier}` },
spec: {
rules: [
{
host: `${clientIdentifier}.example.com`,
http: {
paths: [
{
backend: {
service: {
name: 'production-auto-deploy',
port: { number: 5000 },
},
},
path: '/',
pathType: 'ImplementationSpecific',
},
],
},
},
],
tls: [{ hosts: [`${clientIdentifier}.example.com`] }],
},
},
})
.catch((e) => console.error(e));
39 changes: 18 additions & 21 deletions examples/top_pods.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// in a real program use require('@kubernetes/client-node')
const k8s = require('../dist/index');

const kc = new k8s.KubeConfig();
Expand All @@ -6,34 +7,30 @@ kc.loadFromDefault();
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
const metricsClient = new k8s.Metrics(kc);

k8s.topPods(k8sApi, metricsClient, "kube-system")
.then((pods) => {

k8s.topPods(k8sApi, metricsClient, 'kube-system').then((pods) => {
const podsColumns = pods.map((pod) => {
return {
"POD": pod.Pod.metadata.name,
"CPU(cores)": pod.CPU.CurrentUsage,
"MEMORY(bytes)": pod.Memory.CurrentUsage,
}
POD: pod.Pod.metadata?.name,
'CPU(cores)': pod.CPU.CurrentUsage,
'MEMORY(bytes)': pod.Memory.CurrentUsage,
};
});
console.log("TOP PODS")
console.table(podsColumns)
console.log('TOP PODS');
console.table(podsColumns);
});

k8s.topPods(k8sApi, metricsClient, "kube-system")
.then((pods) => {

k8s.topPods(k8sApi, metricsClient, 'kube-system').then((pods) => {
const podsAndContainersColumns = pods.flatMap((pod) => {
return pod.Containers.map(containerUsage => {
return pod.Containers.map((containerUsage) => {
return {
"POD": pod.Pod.metadata.name,
"NAME": containerUsage.Container,
"CPU(cores)": containerUsage.CPUUsage.CurrentUsage,
"MEMORY(bytes)": containerUsage.MemoryUsage.CurrentUsage,
POD: pod.Pod.metadata?.name,
NAME: containerUsage.Container,
'CPU(cores)': containerUsage.CPUUsage.CurrentUsage,
'MEMORY(bytes)': containerUsage.MemoryUsage.CurrentUsage,
};
})
});
});

console.log("TOP CONTAINERS")
console.table(podsAndContainersColumns)
});
console.log('TOP CONTAINERS');
console.table(podsAndContainersColumns);
});
3 changes: 1 addition & 2 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
this.callbackCache[CONNECT].forEach((elt: ErrorCallback) => elt(undefined));
if (!this.resourceVersion) {
const promise = this.listFn();
const result = await promise;
const list = result.body;
const list = await promise;
this.objects = deleteItems(this.objects, list.items, this.callbackCache[DELETE].slice());
Object.keys(this.indexCache).forEach((key) => {
const updateObjects = deleteItems(this.indexCache[key], list.items);
Expand Down
Loading