Skip to content

Commit 86fd68c

Browse files
chore: update examples for node fetch
Also fix issue with ListPromise still using body from request.
1 parent 147d11e commit 86fd68c

11 files changed

+204
-267
lines changed

FETCH_MIGRATION.md

+24-12
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
88
const path = '/api/v1/pods';
99
const watch = new k8s.Watch(kc);
1010

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

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

@@ -22,6 +22,6 @@ const looper = () => {
2222
console.log(names.join(','));
2323
}
2424
setTimeout(looper, 2000);
25-
}
25+
};
2626

2727
looper();

examples/example.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ kc.loadFromDefault();
55

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

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

examples/follow-logs.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@ const log = new k8s.Log(kc);
99
const logStream = new stream.PassThrough();
1010

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

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

examples/in-cluster.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ kc.loadFromCluster();
55

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

8-
k8sApi.listNamespacedPod('default')
8+
k8sApi
9+
.listNamespacedPod({ namespace: 'default' })
910
.then((res) => {
10-
console.log(res.body);
11+
console.log(res);
1112
})
1213
.catch((err) => {
13-
console.log(err);
14+
console.error(err);
1415
});
15-

examples/ingress.js

+37-25
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
1-
const k8s = require('@kubernetes/client-node')
2-
const kc = new k8s.KubeConfig()
3-
kc.loadFromDefault()
1+
const k8s = require('@kubernetes/client-node');
2+
const kc = new k8s.KubeConfig();
3+
kc.loadFromDefault();
44

5-
const k8sApi = kc.makeApiClient(k8s.NetworkingV1beta1Api) // before 1.14 use extensions/v1beta1
6-
const clientIdentifier = 'my-subdomain'
5+
const k8sApi = kc.makeApiClient(k8s.NetworkingV1Api); // before 1.14 use extensions/v1beta1
6+
const clientIdentifier = 'my-subdomain';
77

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

examples/namespace.js

+14-16
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,21 @@ kc.loadFromDefault();
66
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
77

88
var namespace = {
9-
metadata: {
10-
name: 'test'
11-
}
9+
metadata: {
10+
name: 'test',
11+
},
1212
};
1313

14-
k8sApi.createNamespace(namespace).then(
15-
(response) => {
16-
console.log('Created namespace');
17-
console.log(response);
18-
k8sApi.readNamespace(namespace.metadata.name).then(
19-
(response) => {
14+
k8sApi.createNamespace({ body: namespace }).then(
15+
(response) => {
16+
console.log('Created namespace');
2017
console.log(response);
21-
k8sApi.deleteNamespace(
22-
namespace.metadata.name, {} /* delete options */);
23-
});
24-
},
25-
(err) => {
26-
console.log('Error!: ' + err);
27-
}
18+
k8sApi.readNamespace({ name: namespace.metadata.name }).then((response) => {
19+
console.log(response);
20+
k8sApi.deleteNamespace({ name: namespace.metadata.name });
21+
});
22+
},
23+
(err) => {
24+
console.log('Error!: ' + err);
25+
},
2826
);

examples/top_pods.js

+18-22
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
1-
const k8s = require('../dist/index');
1+
const k8s = require('@kubernetes/client-node');
22

33
const kc = new k8s.KubeConfig();
44
kc.loadFromDefault();
55

66
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
77
const metricsClient = new k8s.Metrics(kc);
88

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

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

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

src/cache.ts

+1-2
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)