Skip to content

Commit

Permalink
feat(config): Now accept a manually specified context in fromKubeConf…
Browse files Browse the repository at this point in the history
…ig (#82)
  • Loading branch information
jh2oman authored and jwaterman-godaddy committed Feb 24, 2017
1 parent 5ae1f5b commit 12738ef
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ function getInCluster () {

module.exports.getInCluster = getInCluster;

//
// Accept a manually specified current-context to take precedence over
// `current-context`
//
/* eslint-disable complexity, max-statements */
function fromKubeconfig (kubeconfig) {
function fromKubeconfig (kubeconfig, current) {
if (!kubeconfig) kubeconfig = loadKubeconfig();

const current = kubeconfig['current-context'];
current = current || kubeconfig['current-context'];
const context = kubeconfig.contexts
.find(item => item.name === current).context;
const cluster = kubeconfig.clusters
Expand Down
49 changes: 49 additions & 0 deletions test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,54 @@ describe('Config', () => {
const args = config.fromKubeconfig(kubeconfig);
assume(args.auth.bearer).equals('foo-token');
});

it('handles manually specified current-context', () => {
const kubeconfig = {
apiVersion: 'v1',
kind: 'Config',
preferences: {},
'current-context': 'foo-context-1',
contexts: [
{
name: 'foo-context-1',
context: {
cluster: 'foo-cluster-1',
user: 'foo-user'
}
},
{
name: 'foo-context-2',
context: {
cluster: 'foo-cluster-2',
user: 'foo-user'
}
}
],
clusters: [
{
name: 'foo-cluster-1',
cluster: {
server: 'https://192.168.42.121:8443'
}
},
{
name: 'foo-cluster-2',
cluster: {
server: 'https://192.168.42.122:8443'
}
}
],
users: [
{
name: 'foo-user',
user: {
token: new Buffer('foo-token').toString('base64')
}
}
]
};
const args = config.fromKubeconfig(kubeconfig, 'foo-context-2');
assume(args.url).equals('https://192.168.42.122:8443');
});
});
});

0 comments on commit 12738ef

Please sign in to comment.