Skip to content

Commit

Permalink
feat: load config from a file location.
Browse files Browse the repository at this point in the history
This adds the ability to pass in a string value for `settings.config`, which points to the location of a kubernetes/optionshift config

fixes nodeshift#189
  • Loading branch information
lholmquist committed Jan 21, 2020
1 parent 25b90e2 commit 2820a69
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/openshift-rest-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const spec = JSON.parse(zlib.gunzipSync(fs.readFileSync(path.join(__dirname, 'sp
* Builds the rest client based on provided or default kubernetes configuration.
*
* @param {object} [settings] - settings object for the openshiftClient function
* @param {object} [settings.config] - custom config object
* @param {object|string} [settings.config] - custom config object. String value will assume a config file location
* @param {string} [settings.config.url] - Openshift cluster url
* @param {string} [settings.config.authUrl] - Openshift Basic auth url
* @param {object} [settings.config.auth] -
Expand All @@ -78,8 +78,13 @@ async function openshiftClient (settings = {}) {
let fullyUserDefined = false;

if (config) {
// A config is being passed in. Check if it is an object
if (typeof config === 'object' && config.auth) {
// A config is being passed in.
if (typeof config === 'string') {
// This is the config location
// load from file
kubeconfig.loadFromFile(config);
clientConfig.backend = new Request({ kubeconfig });
} else if (typeof config === 'object' && config.auth) {
// Check for the auth username password
if ('user' in config.auth || 'username' in config.auth) {
// They are trying the basic auth.
Expand Down
14 changes: 14 additions & 0 deletions test/openshift-client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,17 @@ test('test different config - user defined', async (t) => {
t.pass();
t.end();
});

test('test different config - different location as a string', async (t) => {
const openshiftRestClient = require('../');

const configLocation = `${__dirname}/test-config`;
const settings = {
config: configLocation
};

const client = await openshiftRestClient.OpenshiftClient(settings);
const { kubeconfig } = client;
t.equal(kubeconfig.currentContext, 'for-node-client-testing/192-168-99-100:8443/developer', 'current context is correctly loaded');
t.end();
});

0 comments on commit 2820a69

Please sign in to comment.