diff --git a/src/config.ts b/src/config.ts index 5e13ccc354f..f54d8257824 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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'; @@ -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); } diff --git a/src/config_test.ts b/src/config_test.ts index bffe24eabd2..8e42e5327e8 100644 --- a/src/config_test.ts +++ b/src/config_test.ts @@ -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(); diff --git a/src/config_types.ts b/src/config_types.ts index 1790cc2ee30..46c6258ebcb 100644 --- a/src/config_types.ts +++ b/src/config_types.ts @@ -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 { return (elt: any, i: number, list: u.List): Cluster => { if (!elt.name) { @@ -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 { return (elt: any, i: number, list: u.List): User => { if (!elt.name) { @@ -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 { return (elt: any, i: number, list: u.List): Context => { if (!elt.name) {