Skip to content

Commit

Permalink
feat: load config from a file location. (#190)
Browse files Browse the repository at this point in the history
* feat: load config from a file location.

This adds the ability to pass in a string value for `settings.config`, which points to the location of a kubernetes/optionshift config

fixes #189
  • Loading branch information
lholmquist authored Jan 21, 2020
1 parent 25b90e2 commit 811a67f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@ The openshift-rest-client exposes the config module from the kubernetes client f
For example, if you want to provide a different path to your configuration, you can do something like this:

const openshiftRestClient = require('openshift-rest-client').OpenshiftClient;
const config = require('openshift-rest-client').config;

const path '~/some/path/config';
const customConfig = config.loadFromFile(path);
const config = '~/some/path/config';

openshiftRestClient({config: customConfig}).then((client) => {
openshiftRestClient({ config }).then((client) => {
// Use the client object to find a list of projects, for example
client.apis['project.openshift.io'].v1.project.get().then((response) => {
console.log(response.body);
Expand Down
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 811a67f

Please sign in to comment.