Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: load config from a file location. #190

Merged
merged 2 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});