Skip to content

Commit 15a3345

Browse files
authored
Merge pull request #1074 from clintonmedbery/clintonmedbery/chore/update-examples-node-fetch
chore: update examples node fetch
2 parents 396489d + 34c6010 commit 15a3345

File tree

10 files changed

+200
-254
lines changed

10 files changed

+200
-254
lines changed

FETCH_MIGRATION.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,41 @@ Code will be on the `master` branch.
5252
- [x] Fix errors in /src folder (due to new api)
5353
- [x] migrate src/auth.ts, the dependent implementations (ex: azure_auth, gcp_auth etc) and tests to fetch api from request
5454
- [x] migrate src/log.ts and its tests to fetch api from request
55-
- [x] major remaining work is fixing up async signatures and return piping
55+
- [x] major remaining work is fixing up async signatures and return piping
5656
- [x] migrate src/watch.ts and its tests to fetch api from request
57-
- [x] remove decprecated requestImpl and RequestInterface
58-
- [x] implement queryParams parameter in watch method by injecting them into the fetch call
59-
- [x] update tests in src/watch_test.ts
57+
- [x] remove decprecated requestImpl and RequestInterface
58+
- [x] implement queryParams parameter in watch method by injecting them into the fetch call
59+
- [x] update tests in src/watch_test.ts
6060
- [x] Fix errors in test (due to new api)
6161
- [ ] Test all features
62-
- [ ] Fix examples and validate their param signatures (due to new api)
62+
- [ ] Fix JavaScript examples and validate their param signatures (due to new api)
6363

64-
- [ ] cache-example
65-
- [ ] example
66-
- [ ] follow-logs
64+
- [x] cache-example
65+
- [x] example
66+
- [x] follow-logs
6767
- [ ] in-cluster-create-job-from-cronjob
68-
- [ ] in-cluster
69-
- [ ] ingress
68+
- [x] in-cluster
69+
- [x] ingress
7070
- [ ] namespace
7171
- [ ] patch-example
7272
- [ ] raw-example (note: uses request lib directly, will require full fetch migration not just client param swap)
7373
- [ ] scale-deployment
74-
- [ ] top_pods
75-
- [ ] top
74+
- [x] top_pods
75+
- [x] top
7676
- [ ] yaml-example
7777

78+
- [ ] Fix TypeScript examples and validate their param signatures (due to new api)
79+
80+
- [ ] apply-example
81+
- [ ] attach-example
82+
- [ ] cp-example
83+
- [ ] exec-example
84+
- [ ] informer-with-label-selector
85+
- [ ] informer
86+
- [ ] port-forward
87+
- [ ] example
88+
- [ ] watch-example
89+
7890
- [ ] Update docs
7991
- [ ] Update README examples
8092
- [ ] Document breaking changes for users

examples/cache-example.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const k8s = require('@kubernetes/client-node');
1+
// in a real program use require('@kubernetes/client-node')
2+
const k8s = require('../dist/index');
23

34
const kc = new k8s.KubeConfig();
45
kc.loadFromDefault();
@@ -8,7 +9,7 @@ const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
89
const path = '/api/v1/pods';
910
const watch = new k8s.Watch(kc);
1011

11-
const listFn = () => k8sApi.listPodForAllNamespaces()
12+
const listFn = () => k8sApi.listPodForAllNamespaces();
1213

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

@@ -22,6 +23,6 @@ const looper = () => {
2223
console.log(names.join(','));
2324
}
2425
setTimeout(looper, 2000);
25-
}
26+
};
2627

2728
looper();

examples/example.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
const k8s = require('@kubernetes/client-node');
1+
// in a real program use require('@kubernetes/client-node')
2+
const k8s = require('../dist/index');
23

34
const kc = new k8s.KubeConfig();
45
kc.loadFromDefault();
56

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

8-
k8sApi.listNamespacedPod('default')
9+
k8sApi
10+
.listNamespacedPod({ namespace: 'default' })
911
.then((res) => {
10-
console.log(res.body);
12+
console.log(res);
13+
})
14+
.catch((err) => {
15+
console.error(err);
1116
});
12-

examples/follow-logs.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const stream = require('stream');
2-
const k8s = require('@kubernetes/client-node');
2+
// in a real program use require('@kubernetes/client-node')
3+
const k8s = require('../dist/index');
34

45
const kc = new k8s.KubeConfig();
56
kc.loadFromDefault();
@@ -9,15 +10,23 @@ const log = new k8s.Log(kc);
910
const logStream = new stream.PassThrough();
1011

1112
logStream.on('data', (chunk) => {
12-
// use write rather than console.log to prevent double line feed
13-
process.stdout.write(chunk);
13+
// use write rather than console.log to prevent double line feed
14+
process.stdout.write(chunk);
1415
});
1516

16-
log.log('default', 'pod1', 'container1', logStream, {follow: true, tailLines: 50, pretty: false, timestamps: false})
17-
.catch(err => {console.log(err)})
18-
.then(req => {
19-
// disconnects after 5 seconds
20-
setTimeout(function(){
21-
req.abort();
22-
}, 5000);
23-
});
17+
log.log('default', 'pod1', 'container1', logStream, {
18+
follow: true,
19+
tailLines: 50,
20+
pretty: false,
21+
timestamps: false,
22+
})
23+
.catch((err) => {
24+
console.error(err);
25+
})
26+
.then((req) => {
27+
// disconnects after 5 seconds
28+
setTimeout(function () {
29+
//Note: You might have to install AbortController if you are using node version < 15.0.0
30+
req.abort();
31+
}, 5000);
32+
});

examples/in-cluster.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
const k8s = require('@kubernetes/client-node');
1+
// in a real program use require('@kubernetes/client-node')
2+
const k8s = require('../dist/index');
23

34
const kc = new k8s.KubeConfig();
45
kc.loadFromCluster();
56

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

8-
k8sApi.listNamespacedPod('default')
9+
k8sApi
10+
.listNamespacedPod({ namespace: 'default' })
911
.then((res) => {
10-
console.log(res.body);
12+
console.log(res);
1113
})
1214
.catch((err) => {
13-
console.log(err);
15+
console.error(err);
1416
});
15-

examples/ingress.js

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
1-
const k8s = require('@kubernetes/client-node')
2-
const kc = new k8s.KubeConfig()
3-
kc.loadFromDefault()
1+
// in a real program use require('@kubernetes/client-node')
2+
const k8s = require('../dist/index');
43

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

8-
k8sApi.createNamespacedIngress('default', {
9-
apiVersions: 'networking.k8s.io/v1beta1',
10-
kind: 'Ingress',
11-
metadata: { name: `production-custom-${clientIdentifier}` },
12-
spec: {
13-
rules: [{
14-
host: `${clientIdentifier}.example.com`,
15-
http: {
16-
paths: [{
17-
backend: {
18-
serviceName: 'production-auto-deploy',
19-
servicePort: 5000
20-
},
21-
path: '/'
22-
}]
23-
}
24-
}],
25-
tls: [{ hosts: [`${clientIdentifier}.example.com`] }]
26-
}
27-
}).catch(e => console.log(e))
7+
const k8sApi = kc.makeApiClient(k8s.NetworkingV1Api);
8+
const clientIdentifier = 'my-subdomain';
9+
10+
k8sApi
11+
.createNamespacedIngress({
12+
namespace: 'default',
13+
body: {
14+
apiVersion: 'networking.k8s.io/v1',
15+
kind: 'Ingress',
16+
metadata: { name: `production-custom-${clientIdentifier}` },
17+
spec: {
18+
rules: [
19+
{
20+
host: `${clientIdentifier}.example.com`,
21+
http: {
22+
paths: [
23+
{
24+
backend: {
25+
service: {
26+
name: 'production-auto-deploy',
27+
port: { number: 5000 },
28+
},
29+
},
30+
path: '/',
31+
pathType: 'ImplementationSpecific',
32+
},
33+
],
34+
},
35+
},
36+
],
37+
tls: [{ hosts: [`${clientIdentifier}.example.com`] }],
38+
},
39+
},
40+
})
41+
.catch((e) => console.error(e));

examples/top_pods.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// in a real program use require('@kubernetes/client-node')
12
const k8s = require('../dist/index');
23

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

9-
k8s.topPods(k8sApi, metricsClient, "kube-system")
10-
.then((pods) => {
11-
10+
k8s.topPods(k8sApi, metricsClient, 'kube-system').then((pods) => {
1211
const podsColumns = pods.map((pod) => {
1312
return {
14-
"POD": pod.Pod.metadata.name,
15-
"CPU(cores)": pod.CPU.CurrentUsage,
16-
"MEMORY(bytes)": pod.Memory.CurrentUsage,
17-
}
13+
POD: pod.Pod.metadata?.name,
14+
'CPU(cores)': pod.CPU.CurrentUsage,
15+
'MEMORY(bytes)': pod.Memory.CurrentUsage,
16+
};
1817
});
19-
console.log("TOP PODS")
20-
console.table(podsColumns)
18+
console.log('TOP PODS');
19+
console.table(podsColumns);
2120
});
2221

23-
k8s.topPods(k8sApi, metricsClient, "kube-system")
24-
.then((pods) => {
25-
22+
k8s.topPods(k8sApi, metricsClient, 'kube-system').then((pods) => {
2623
const podsAndContainersColumns = pods.flatMap((pod) => {
27-
return pod.Containers.map(containerUsage => {
24+
return pod.Containers.map((containerUsage) => {
2825
return {
29-
"POD": pod.Pod.metadata.name,
30-
"NAME": containerUsage.Container,
31-
"CPU(cores)": containerUsage.CPUUsage.CurrentUsage,
32-
"MEMORY(bytes)": containerUsage.MemoryUsage.CurrentUsage,
26+
POD: pod.Pod.metadata?.name,
27+
NAME: containerUsage.Container,
28+
'CPU(cores)': containerUsage.CPUUsage.CurrentUsage,
29+
'MEMORY(bytes)': containerUsage.MemoryUsage.CurrentUsage,
3330
};
34-
})
31+
});
3532
});
3633

37-
console.log("TOP CONTAINERS")
38-
console.table(podsAndContainersColumns)
39-
});
34+
console.log('TOP CONTAINERS');
35+
console.table(podsAndContainersColumns);
36+
});

src/cache.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
132132
this.callbackCache[CONNECT].forEach((elt: ErrorCallback) => elt(undefined));
133133
if (!this.resourceVersion) {
134134
const promise = this.listFn();
135-
const result = await promise;
136-
const list = result.body;
135+
const list = await promise;
137136
this.objects = deleteItems(this.objects, list.items, this.callbackCache[DELETE].slice());
138137
Object.keys(this.indexCache).forEach((key) => {
139138
const updateObjects = deleteItems(this.indexCache[key], list.items);

0 commit comments

Comments
 (0)