Skip to content

Commit e9d43da

Browse files
committed
basic generic api examples
Signed-off-by: zahar <zfrhv2010@gmail.com>
1 parent 9f7a02b commit e9d43da

File tree

6 files changed

+140
-40
lines changed

6 files changed

+140
-40
lines changed

examples/kubectl/equivalents/apply.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// in a real program use require('@kubernetes/client-node')
2+
import * as k8s from '../../../dist/index';
3+
4+
const kc = new k8s.KubeConfig();
5+
kc.loadFromDefault();
6+
7+
const client = k8s.KubernetesObjectApi.makeApiClient(kc);
8+
9+
// update deployment "my-deployment" in namespace "my-namespace" to 3 replicas
10+
const deployment = {
11+
apiVersion: 'apps/v1',
12+
kind: 'Deployment',
13+
metadata: {
14+
name: 'my-deployment',
15+
namespace: 'my-namespace'
16+
},
17+
spec: {
18+
replicas: 3
19+
}
20+
}
21+
22+
client.patch(deployment)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// in a real program use require('@kubernetes/client-node')
2+
import * as k8s from '../../../dist/index';
3+
4+
const kc = new k8s.KubeConfig();
5+
kc.loadFromDefault();
6+
7+
const client = k8s.KubernetesObjectApi.makeApiClient(kc);
8+
9+
const namespace = {
10+
metadata: {
11+
name: 'test'
12+
}
13+
}
14+
15+
client.create(namespace)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// in a real program use require('@kubernetes/client-node')
2+
import * as k8s from '../../../dist/index';
3+
4+
const kc = new k8s.KubeConfig();
5+
kc.loadFromDefault();
6+
7+
const client = k8s.KubernetesObjectApi.makeApiClient(kc);
8+
9+
const namespace = {
10+
metadata: {
11+
name: 'test'
12+
}
13+
}
14+
15+
client.delete(namespace)

examples/kubectl/equivalents/get.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// in a real program use require('@kubernetes/client-node')
2+
import * as k8s from '../../../dist/index';
3+
4+
const kc = new k8s.KubeConfig();
5+
kc.loadFromDefault();
6+
7+
const client = k8s.KubernetesObjectApi.makeApiClient(kc);
8+
9+
const namespace = {
10+
metadata: {
11+
name: 'test'
12+
}
13+
}
14+
15+
const live_namespace = (await client.read(namespace)).body
16+
console.log(live_namespace)
Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,22 @@
11
// in a real program use require('@kubernetes/client-node')
22
import * as k8s from '../../../dist';
3-
import * as fs from 'fs';
4-
import * as yaml from 'js-yaml';
5-
import { promisify } from 'util';
63

7-
/**
8-
* Replicate the functionality of `kubectl apply`. That is, create the resources defined in the `specFile` if they do
9-
* not exist, patch them if they do exist.
10-
*
11-
* @param specPath File system path to a YAML Kubernetes spec.
12-
* @return Array of resources created
13-
*/
14-
export async function apply(specPath: string): Promise<k8s.KubernetesObject[]> {
15-
const kc = new k8s.KubeConfig();
16-
kc.loadFromDefault();
17-
const client = k8s.KubernetesObjectApi.makeApiClient(kc);
18-
const fsReadFileP = promisify(fs.readFile);
19-
const specString = await fsReadFileP(specPath, 'utf8');
20-
const specs = yaml.loadAll(specString) as k8s.KubernetesObject[];
21-
const validSpecs = specs.filter((s) => s && s.kind && s.metadata);
22-
const created: k8s.KubernetesObject[] = [];
23-
for (const spec of validSpecs) {
24-
// this is to convince the old version of TypeScript that metadata exists even though we already filtered specs
25-
// without metadata out
26-
spec.metadata = spec.metadata || {};
27-
spec.metadata.annotations = spec.metadata.annotations || {};
28-
delete spec.metadata.annotations['kubectl.kubernetes.io/last-applied-configuration'];
29-
spec.metadata.annotations['kubectl.kubernetes.io/last-applied-configuration'] = JSON.stringify(spec);
30-
try {
31-
// try to get the resource, if it does not exist an error will be thrown and we will end up in the catch
32-
// block.
33-
await client.read(spec);
34-
// we got the resource, so it exists, so patch it
35-
const response = await client.patch(spec);
36-
created.push(response);
37-
} catch (e) {
38-
// we did not get the resource, so it does not exist, so create it
39-
const response = await client.create(spec);
40-
created.push(response);
41-
}
42-
}
43-
return created;
4+
const kc = new k8s.KubeConfig();
5+
kc.loadFromDefault();
6+
7+
const client = k8s.KubernetesObjectApi.makeApiClient(kc);
8+
9+
// update deployment "my-deployment" in namespace "my-namespace" to 3 replicas
10+
const deployment = {
11+
apiVersion: 'apps/v1',
12+
kind: 'Deployment',
13+
metadata: {
14+
name: 'my-deployment',
15+
namespace: 'my-namespace'
16+
},
17+
spec: {
18+
replicas: 3
19+
}
4420
}
21+
22+
client.patch(deployment)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// in a real program use require('@kubernetes/client-node')
2+
import * as k8s from '../../../dist';
3+
import * as yaml from 'js-yaml';
4+
import { promises as fs } from 'fs';
5+
6+
/**
7+
* Replicate the functionality of `kubectl apply`. That is, create the resources defined in the `specFile` if they do
8+
* not exist, patch them if they do exist.
9+
*
10+
* @param specPath File system path to a YAML Kubernetes spec.
11+
* @return Array of resources created
12+
*/
13+
export async function apply(specPath: string): Promise<k8s.KubernetesObject[]> {
14+
const kc = new k8s.KubeConfig();
15+
kc.loadFromDefault();
16+
17+
const client = k8s.KubernetesObjectApi.makeApiClient(kc);
18+
19+
const specString = await fs.readFile(specPath, 'utf8');
20+
const specs: k8s.KubernetesObject[] = yaml.loadAll(specString);
21+
const validSpecs = specs.filter((s) => s && s.kind && s.metadata);
22+
const created: k8s.KubernetesObject[] = [];
23+
for (const spec of validSpecs) {
24+
// this is to convince the old version of TypeScript that metadata exists even though we already filtered specs
25+
// without metadata out
26+
spec.metadata = spec.metadata || {};
27+
spec.metadata.annotations = spec.metadata.annotations || {};
28+
delete spec.metadata.annotations['kubectl.kubernetes.io/last-applied-configuration'];
29+
spec.metadata.annotations['kubectl.kubernetes.io/last-applied-configuration'] = JSON.stringify(spec);
30+
try {
31+
// try to get the resource, if it does not exist an error will be thrown and we will end up in the catch
32+
// block.
33+
await client.read(spec);
34+
// we got the resource, so it exists, so patch it
35+
//
36+
// Note that this could fail if the spec refers to a custom resource. For custom resources you may need
37+
// to specify a different patch merge strategy in the content-type header.
38+
//
39+
// See: https://github.com/kubernetes/kubernetes/issues/97423
40+
const response = await client.patch(spec);
41+
created.push(response.body);
42+
} catch (err) {
43+
// if the resource doesnt exist then create it
44+
if (err instanceof k8s.HttpError && err.statusCode === 404) {
45+
const response = await client.create(spec);
46+
created.push(response.body);
47+
} else {
48+
throw err
49+
}
50+
}
51+
}
52+
53+
return created;
54+
}

0 commit comments

Comments
 (0)