Skip to content

Commit d262aff

Browse files
authored
Merge pull request #372 from brendandburns/export
Add an export function for KubeConfigs. Add unit tests too.
2 parents ef592cf + 6da9a88 commit d262aff

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

src/config.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@ import shelljs = require('shelljs');
1010
import * as api from './api';
1111
import { Authenticator } from './auth';
1212
import { CloudAuth } from './cloud_auth';
13-
import { Cluster, Context, newClusters, newContexts, newUsers, User } from './config_types';
13+
import {
14+
Cluster,
15+
Context,
16+
exportCluster,
17+
exportContext,
18+
exportUser,
19+
newClusters,
20+
newContexts,
21+
newUsers,
22+
User,
23+
} from './config_types';
1424
import { ExecAuth } from './exec_auth';
1525
import { FileAuth } from './file_auth';
1626
import { OpenIDConnectAuth } from './oidc_auth';
@@ -335,6 +345,20 @@ export class KubeConfig {
335345
});
336346
}
337347

348+
public exportConfig(): string {
349+
const configObj = {
350+
apiVersion: 'v1',
351+
kind: 'Config',
352+
clusters: this.clusters.map(exportCluster),
353+
users: this.users.map(exportUser),
354+
contexts: this.contexts.map(exportContext),
355+
preferences: {},
356+
'current-context': this.getCurrentContext(),
357+
};
358+
359+
return JSON.stringify(configObj);
360+
}
361+
338362
private getCurrentContextObject() {
339363
return this.getContextObject(this.currentContext);
340364
}

src/config_test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,17 @@ describe('KubeConfig', () => {
192192
});
193193
});
194194

195+
describe('export', () => {
196+
it('should export and re-import correctly', () => {
197+
const kc = new KubeConfig();
198+
kc.loadFromFile(kcFileName);
199+
const output = kc.exportConfig();
200+
const newConfig = new KubeConfig();
201+
newConfig.loadFromString(output);
202+
validateFileLoad(kc);
203+
});
204+
});
205+
195206
describe('loadEmptyUser', () => {
196207
it('should load a kubeconfig with an empty user', () => {
197208
const kc = new KubeConfig();

src/config_types.ts

+36
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ export function newClusters(a: any): Cluster[] {
1414
return u.map(a, clusterIterator());
1515
}
1616

17+
export function exportCluster(cluster: Cluster): any {
18+
return {
19+
name: cluster.name,
20+
cluster: {
21+
server: cluster.server,
22+
'certificate-authority-data': cluster.caData,
23+
'certificate-authority': cluster.caFile,
24+
'insecure-skip-tls-verify': cluster.skipTLSVerify,
25+
},
26+
};
27+
}
28+
1729
function clusterIterator(): u.ListIterator<any, Cluster> {
1830
return (elt: any, i: number, list: u.List<any>): Cluster => {
1931
if (!elt.name) {
@@ -52,6 +64,23 @@ export function newUsers(a: any): User[] {
5264
return u.map(a, userIterator());
5365
}
5466

67+
export function exportUser(user: User): any {
68+
return {
69+
name: user.name,
70+
user: {
71+
'auth-provider': user.authProvider,
72+
'client-certificate-data': user.certData,
73+
'client-certificate': user.certFile,
74+
exec: user.exec,
75+
'client-key-data': user.keyData,
76+
'client-key': user.keyFile,
77+
token: user.token,
78+
password: user.password,
79+
username: user.username,
80+
},
81+
};
82+
}
83+
5584
function userIterator(): u.ListIterator<any, User> {
5685
return (elt: any, i: number, list: u.List<any>): User => {
5786
if (!elt.name) {
@@ -94,6 +123,13 @@ export function newContexts(a: any): Context[] {
94123
return u.map(a, contextIterator());
95124
}
96125

126+
export function exportContext(ctx: Context): any {
127+
return {
128+
name: ctx.name,
129+
context: ctx,
130+
};
131+
}
132+
97133
function contextIterator(): u.ListIterator<any, Context> {
98134
return (elt: any, i: number, list: u.List<any>): Context => {
99135
if (!elt.name) {

0 commit comments

Comments
 (0)