Skip to content

Commit 66ddc0b

Browse files
authored
Merge pull request #153 from brendandburns/yaml
Add some simple YAML load/dump helpers and an example.
2 parents d0774f4 + 9ac5a86 commit 66ddc0b

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

examples/yaml-example.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const k8s = require('@kubernetes/client-node');
2+
const fs = require('fs');
3+
4+
const kc = new k8s.KubeConfig();
5+
kc.loadFromDefault();
6+
7+
const k8sApi = kc.makeApiClient(k8s.Core_v1Api);
8+
9+
const yamlString = k8s.dumpYaml({
10+
metadata: {
11+
name: 'test'
12+
}
13+
});
14+
15+
const yamlNamespace = k8s.loadYaml(k8s.dumpYaml(yamlString));
16+
17+
k8sApi.createNamespace(yamlNamespace).then(
18+
(response) => {
19+
console.log('Created namespace');
20+
console.log(response);
21+
k8sApi.readNamespace(namespace.metadata.name).then(
22+
(response) => {
23+
console.log(response);
24+
k8sApi.deleteNamespace(
25+
namespace.metadata.name, {} /* delete options */);
26+
});
27+
},
28+
(err) => {
29+
console.log('Error!: ' + err);
30+
}
31+
);

src/yaml.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as yaml from 'js-yaml';
2+
3+
export function loadYaml<T>(data: string, opts?: yaml.LoadOptions): T {
4+
return yaml.safeLoad(data, opts) as T;
5+
}
6+
7+
export function loadAllYaml(data: string, opts?: yaml.LoadOptions): any[] {
8+
return yaml.safeLoadAll(data, undefined, opts);
9+
}
10+
11+
export function dumpYaml(object: any, opts?: yaml.DumpOptions): string {
12+
return yaml.safeDump(object, opts);
13+
}

src/yaml_test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { expect } from 'chai';
2+
3+
import { V1Namespace } from './api';
4+
import { dumpYaml, loadAllYaml, loadYaml } from './yaml';
5+
6+
describe('yaml', () => {
7+
it('should load safely', () => {
8+
const yaml = 'apiVersion: v1\n' +
9+
'kind: Namespace\n' +
10+
'metadata:\n' +
11+
' name: some-namespace\n';
12+
const ns = loadYaml<V1Namespace>(yaml);
13+
14+
expect(ns.apiVersion).to.equal('v1');
15+
expect(ns.kind).to.equal('Namespace');
16+
expect(ns.metadata.name).to.equal('some-namespace');
17+
});
18+
it('should load all safely', () => {
19+
const yaml = 'apiVersion: v1\n' +
20+
'kind: Namespace\n' +
21+
'metadata:\n' +
22+
' name: some-namespace\n' +
23+
'---\n' +
24+
'apiVersion: v1\n' +
25+
'kind: Pod\n' +
26+
'metadata:\n' +
27+
' name: some-pod\n' +
28+
' namespace: some-ns\n';
29+
const objects = loadAllYaml(yaml);
30+
31+
expect(objects.length).to.equal(2);
32+
expect(objects[0].kind).to.equal('Namespace');
33+
expect(objects[1].kind).to.equal('Pod');
34+
expect(objects[0].metadata.name).to.equal('some-namespace');
35+
expect(objects[1].metadata.name).to.equal('some-pod');
36+
expect(objects[1].metadata.namespace).to.equal('some-ns');
37+
});
38+
it('should round trip successfully', () => {
39+
const expected = {
40+
metadata: {
41+
name: 'test',
42+
},
43+
};
44+
const yamlString = dumpYaml(expected);
45+
const actual = loadYaml(yamlString);
46+
expect(actual).to.deep.equal(expected);
47+
});
48+
});

0 commit comments

Comments
 (0)