-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add in-cluster config helper (#49)
- Loading branch information
Showing
5 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
|
||
const certPath = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'; | ||
const tokenPath = '/var/run/secrets/kubernetes.io/serviceaccount/token'; | ||
const namespacePath = '/var/run/secrets/kubernetes.io/serviceaccount/namespace'; | ||
|
||
/** | ||
* Returns with in cluster config | ||
* Based on: https://github.com/kubernetes/client-go/blob/124670e99da15091e13916f0ad4b2b2df2a39cd5/rest/config.go#L274 | ||
* and http://kubernetes.io/docs/user-guide/accessing-the-cluster/#accessing-the-api-from-a-pod | ||
* | ||
* @function getInCluster | ||
* @returns {Object} { url, cert, auth, namespace } | ||
*/ | ||
function getInCluster () { | ||
const host = process.env.KUBERNETES_SERVICE_HOST; | ||
const port = process.env.KUBERNETES_SERVICE_PORT; | ||
|
||
if (!host || !port) { | ||
throw TypeError('Unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined'); | ||
} | ||
|
||
const cert = fs.readFileSync(certPath, 'utf8'); | ||
const bearer = fs.readFileSync(tokenPath, 'utf8'); | ||
const namespace = fs.readFileSync(namespacePath, 'utf8'); | ||
|
||
return { | ||
url: `https://${host}:${port}`, | ||
cert, | ||
auth: { bearer }, | ||
namespace | ||
}; | ||
} | ||
|
||
module.exports = { | ||
getInCluster | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
const core = require('./core'); | ||
|
||
module.exports = core; | ||
module.exports.Api = require('./api'); | ||
module.exports.Core = core; | ||
module.exports.Extensions = require('./extensions'); | ||
module.exports.config = require('./config') | ||
module.exports.testUtils = { | ||
aliasResources: require('./common').aliasResources | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict'; | ||
|
||
const assume = require('assume'); | ||
const sinon = require('sinon'); | ||
const fs = require('fs'); | ||
|
||
const config = require('../lib/config'); | ||
|
||
describe('Config', () => { | ||
let sandbox | ||
|
||
beforeEach(() => { | ||
sandbox = sinon.sandbox.create(); | ||
}) | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}) | ||
|
||
describe('getInCluster', () => { | ||
it('should return with in-cluster config', () => { | ||
// You cannot stub undefined, so ensure KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT are defined | ||
process.env.KUBERNETES_SERVICE_HOST = process.env.KUBERNETES_SERVICE_HOST || '' | ||
process.env.KUBERNETES_SERVICE_PORT = process.env.KUBERNETES_SERVICE_PORT || '' | ||
|
||
sandbox.stub(process.env, 'KUBERNETES_SERVICE_HOST', 'myhost') | ||
sandbox.stub(process.env, 'KUBERNETES_SERVICE_PORT', 443) | ||
|
||
const fsReadFileSync = sandbox.stub(fs, 'readFileSync') | ||
|
||
fsReadFileSync | ||
.withArgs('/var/run/secrets/kubernetes.io/serviceaccount/ca.crt') | ||
.returns('my-cert') | ||
|
||
fsReadFileSync | ||
.withArgs('/var/run/secrets/kubernetes.io/serviceaccount/token') | ||
.returns('my-token') | ||
|
||
fsReadFileSync | ||
.withArgs('/var/run/secrets/kubernetes.io/serviceaccount/namespace') | ||
.returns('my-namespace') | ||
|
||
const configInCluster = config.getInCluster(); | ||
assume(configInCluster).eqls({ | ||
auth: { bearer: 'my-token' }, | ||
cert: 'my-cert', | ||
namespace: 'my-namespace', | ||
url: 'https://myhost:443' | ||
}); | ||
}); | ||
}); | ||
}); |