Skip to content

Commit

Permalink
Add an export function.
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed Nov 19, 2019
1 parent caa7130 commit 6da9a88
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ import shelljs = require('shelljs');
import * as api from './api';
import { Authenticator } from './auth';
import { CloudAuth } from './cloud_auth';
import { Cluster, Context, newClusters, newContexts, newUsers, User } from './config_types';
import {
Cluster,
Context,
exportCluster,
exportContext,
exportUser,
newClusters,
newContexts,
newUsers,
User,
} from './config_types';
import { ExecAuth } from './exec_auth';
import { FileAuth } from './file_auth';
import { OpenIDConnectAuth } from './oidc_auth';
Expand Down Expand Up @@ -320,6 +330,20 @@ export class KubeConfig {
});
}

public exportConfig(): string {
const configObj = {
apiVersion: 'v1',
kind: 'Config',
clusters: this.clusters.map(exportCluster),
users: this.users.map(exportUser),
contexts: this.contexts.map(exportContext),
preferences: {},
'current-context': this.getCurrentContext(),
};

return JSON.stringify(configObj);
}

private getCurrentContextObject() {
return this.getContextObject(this.currentContext);
}
Expand Down
11 changes: 11 additions & 0 deletions src/config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ describe('KubeConfig', () => {
});
});

describe('export', () => {
it('should export and re-import correctly', () => {
const kc = new KubeConfig();
kc.loadFromFile(kcFileName);
const output = kc.exportConfig();
const newConfig = new KubeConfig();
newConfig.loadFromString(output);
validateFileLoad(kc);
});
});

describe('loadEmptyUser', () => {
it('should load a kubeconfig with an empty user', () => {
const kc = new KubeConfig();
Expand Down
36 changes: 36 additions & 0 deletions src/config_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ export function newClusters(a: any): Cluster[] {
return u.map(a, clusterIterator());
}

export function exportCluster(cluster: Cluster): any {
return {
name: cluster.name,
cluster: {
server: cluster.server,
'certificate-authority-data': cluster.caData,
'certificate-authority': cluster.caFile,
'insecure-skip-tls-verify': cluster.skipTLSVerify,
},
};
}

function clusterIterator(): u.ListIterator<any, Cluster> {
return (elt: any, i: number, list: u.List<any>): Cluster => {
if (!elt.name) {
Expand Down Expand Up @@ -52,6 +64,23 @@ export function newUsers(a: any): User[] {
return u.map(a, userIterator());
}

export function exportUser(user: User): any {
return {
name: user.name,
user: {
'auth-provider': user.authProvider,
'client-certificate-data': user.certData,
'client-certificate': user.certFile,
exec: user.exec,
'client-key-data': user.keyData,
'client-key': user.keyFile,
token: user.token,
password: user.password,
username: user.username,
},
};
}

function userIterator(): u.ListIterator<any, User> {
return (elt: any, i: number, list: u.List<any>): User => {
if (!elt.name) {
Expand Down Expand Up @@ -94,6 +123,13 @@ export function newContexts(a: any): Context[] {
return u.map(a, contextIterator());
}

export function exportContext(ctx: Context): any {
return {
name: ctx.name,
context: ctx,
};
}

function contextIterator(): u.ListIterator<any, Context> {
return (elt: any, i: number, list: u.List<any>): Context => {
if (!elt.name) {
Expand Down

0 comments on commit 6da9a88

Please sign in to comment.