forked from kubernetes-client/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch-example.js
60 lines (55 loc) · 1.83 KB
/
patch-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// in a real program use require('@kubernetes/client-node')
const k8s = require('../dist/index');
const { createConfiguration, ServerConfiguration } = require('../dist');
const { PromiseMiddlewareWrapper } = require('../dist/gen/middleware');
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
const namespace = 'default';
k8sApi.listNamespacedPod({ namespace }).then((res) => {
const patch = [
{
op: 'replace',
path: '/metadata/labels',
value: {
foo: 'bar',
},
},
];
const headerPatchMiddleware = new PromiseMiddlewareWrapper({
pre: async (requestContext) => {
requestContext.setHeaderParam('Content-type', 'application/json-patch+json');
return requestContext;
},
post: async (responseContext) => responseContext,
});
let currentContext = kc.getCurrentContext();
let currentCluster = kc.getCluster(currentContext);
if (currentCluster === undefined || currentCluster === null) {
throw new Error('Cluster is undefined');
}
let server = currentCluster.server;
if (server === undefined) {
throw new Error('Server is undefined');
}
const baseServerConfig = new ServerConfiguration(server, {});
const configuration = createConfiguration({
middleware: [headerPatchMiddleware],
baseServer: baseServerConfig,
authMethods: {
default: kc,
},
});
k8sApi
.patchNamespacedPod(
{ name: res?.items?.[0].metadata?.name ?? '', namespace, body: patch },
configuration,
)
.then(() => {
console.log('Patched.');
})
.catch((err) => {
console.log('Error: ');
console.log(err);
});
});