Skip to content

Commit 1644400

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

File tree

6 files changed

+136
-42
lines changed

6 files changed

+136
-42
lines changed

examples/kubectl/equivalents/apply.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const k8s = require('@kubernetes/client-node');
2+
3+
const kc = new k8s.KubeConfig();
4+
kc.loadFromDefault();
5+
6+
const client = k8s.KubernetesObjectApi.makeApiClient(kc);
7+
8+
// update deployment "my-deployment" in namespace "my-namespace" to 3 replicas
9+
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+
}
20+
21+
client.patch(deployment)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const k8s = require('@kubernetes/client-node');
2+
3+
const kc = new k8s.KubeConfig();
4+
kc.loadFromDefault();
5+
6+
const client = k8s.KubernetesObjectApi.makeApiClient(kc);
7+
8+
const namespace = {
9+
metadata: {
10+
name: 'test'
11+
}
12+
}
13+
14+
client.create(namespace)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const k8s = require('@kubernetes/client-node');
2+
3+
const kc = new k8s.KubeConfig();
4+
kc.loadFromDefault();
5+
6+
const client = k8s.KubernetesObjectApi.makeApiClient(kc);
7+
8+
const namespace = {
9+
metadata: {
10+
name: 'test'
11+
}
12+
}
13+
14+
client.delete(namespace)

examples/kubectl/equivalents/get.js

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

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

0 commit comments

Comments
 (0)