Skip to content

Commit a208bd6

Browse files
committed
examples: fix kubectl typescript for v1
This commit updates the typescript examples to work with the v1 branch. The examples were tested using Node 23 and the --experimental-strip-types flag.
1 parent 4739cd0 commit a208bd6

File tree

7 files changed

+31
-27
lines changed

7 files changed

+31
-27
lines changed

examples/typescript/apply/apply-example.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ const client = k8s.KubernetesObjectApi.makeApiClient(kc);
77

88
// update deployment "my-deployment" in namespace "my-namespace" to 3 replicas
99
const deployment = {
10-
apiVersion: 'apps/v1',
11-
kind: 'Deployment',
12-
metadata: {
13-
name: 'my-deployment',
14-
namespace: 'my-namespace'
15-
},
16-
spec: {
17-
replicas: 3
18-
}
19-
}
10+
apiVersion: 'apps/v1',
11+
kind: 'Deployment',
12+
metadata: {
13+
name: 'my-deployment',
14+
namespace: 'my-namespace',
15+
},
16+
spec: {
17+
replicas: 3,
18+
},
19+
};
2020

21-
client.patch(deployment)
21+
client.patch(deployment);

examples/typescript/apply/apply-from-file-example.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ export async function apply(specPath: string): Promise<k8s.KubernetesObject[]> {
3636
//
3737
// See: https://github.com/kubernetes/kubernetes/issues/97423
3838
const response = await client.patch(spec);
39-
created.push(response.body);
39+
created.push(response);
4040
} catch (err) {
4141
// if the resource doesnt exist then create it
42-
if (err instanceof k8s.HttpError && err.statusCode === 404) {
43-
const response = await client.create(spec);
44-
created.push(response.body);
42+
if (err instanceof k8s.ApiException && err.code === 404) {
43+
const response = await client.create(spec);
44+
created.push(response);
4545
} else {
46-
throw err
46+
throw err;
4747
}
4848
}
4949
}

examples/typescript/attach/attach-example.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,12 @@ const namespace = 'default';
99
const pod = 'nginx-4217019353-9gl4s';
1010
const container = 'nginx';
1111

12-
attach.attach(namespace, pod, container, process.stdout, process.stderr, null /* stdin */, false /* tty */);
12+
await attach.attach(
13+
namespace,
14+
pod,
15+
container,
16+
process.stdout,
17+
process.stderr,
18+
null /* stdin */,
19+
false /* tty */,
20+
);

examples/typescript/cp/cp-example.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const cp = new k8s.Cp(kc);
88
const namespace = 'default';
99
const pod = 'nginx-4217019353-9gl4s';
1010
const container = 'nginx';
11-
const srcPath = '/test.txt';
11+
const srcPath = './test.txt';
1212
const targetPath = '/tmp';
1313

14-
cp.cpFromPod(namespace, pod, container, srcPath, targetPath);
14+
await cp.cpFromPod(namespace, pod, container, srcPath, targetPath);

examples/typescript/informer/informer.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import * as k8s from '@kubernetes/client-node';
22

33
const kc = new k8s.KubeConfig();
4-
54
kc.loadFromDefault();
65

76
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
8-
97
const namespace = 'default';
10-
118
const listFn = () => k8sApi.listNamespacedPod({ namespace });
12-
139
const informer = k8s.makeInformer(kc, `/api/v1/namespaces/${namespace}/pods`, listFn);
1410

1511
informer.on('add', (obj: k8s.V1Pod) => {

examples/typescript/patch/patch-example.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
ResponseContext,
55
KubeConfig,
66
createConfiguration,
7-
Configuration,
7+
type Configuration,
88
ServerConfiguration,
99
} from '@kubernetes/client-node';
1010
import { PromiseMiddlewareWrapper } from '@kubernetes/client-node/dist/gen/middleware.js';
@@ -50,7 +50,7 @@ try {
5050
default: kc,
5151
},
5252
});
53-
const podName = res.items[0].metadata?.name;
53+
const podName = res.items[0]?.metadata?.name;
5454
if (podName === undefined) {
5555
throw new Error('Pod name is undefined');
5656
}
@@ -62,7 +62,7 @@ try {
6262
body: patch,
6363
},
6464
configuration,
65-
)
65+
);
6666

6767
console.log('Patched.');
6868
} catch (err) {

examples/typescript/simple/example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
88
const namespace = 'default';
99

1010
const res = await k8sApi.listNamespacedPod({ namespace });
11-
console.log(res.body);
11+
console.log(res);
1212

1313
// Example of instantiating a Pod object.
1414
const pod = {} as k8s.V1Pod;

0 commit comments

Comments
 (0)