Skip to content

Commit

Permalink
squash: warn if the client can't load the remote spec. load the defau…
Browse files Browse the repository at this point in the history
…lt spec instead
  • Loading branch information
lholmquist committed May 21, 2020
1 parent e176dbd commit 4270caa
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/openshift-rest-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,17 @@ async function openshiftClient (settings = {}) {
clientConfig.backend = new Request({ kubeconfig });
}

const client = new Client(clientConfig);
let client = new Client(clientConfig);
if (settings.loadSpecFromCluster) {
await client.loadSpec();
try {
await client.loadSpec();
} catch (err) {
// Warn the user there was an error and loading the other spec
console.warn('Warning: Remote client spec unable to load', err.message);
console.warn('Warning: Loading default spec instead');
clientConfig.spec = spec;
client = new Client(clientConfig);
}
}

// CRD with the service instance stuff, but only to this client, not the cluster
Expand Down
60 changes: 60 additions & 0 deletions test/openshift-client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,63 @@ test('openshift client tests - loadSpecFromCluster', async (t) => {
t.ok(osClient.kubeconfig, 'client should have the kubeconfig object');
t.end();
});

test('openshift client tests - loadSpecFromCluster - Fail to load remote and load default spec', async (t) => {
const openshiftRestClient = require('../');

openshiftRestClient.config.loadFromString(JSON.stringify(userDefinedConfig));
const settings = {
loadSpecFromCluster: true,
config: openshiftRestClient.config
};

nock('https://192.168.99.100:8443')
.matchHeader('authorization', 'Bearer zVBd1ZFeJqEAILJgimm4-gZJauaw3PW4EVqV_peEZ3U')
.get('/openapi/v2')
.reply(404, { message: 'Nope' })
.get('/swagger.json')
.reply(404, { message: 'Nope' });

const osClient = await openshiftRestClient.OpenshiftClient(settings);

t.pass('Failing client load should load default spec');

t.ok(osClient.apis['build.openshift.io'], 'client object should have a build object');
t.ok(osClient.apis.build, 'build object is aliased');

t.ok(osClient.apis['apps.openshift.io'], 'client object should have a apps object');
t.ok(osClient.apis.app, 'apps object is aliased to app');

t.ok(osClient.apis['authorization.openshift.io'], 'client object should have a authorization object');
t.ok(osClient.apis.authorization, 'authorization object is aliased to authorization');

t.ok(osClient.apis['image.openshift.io'], 'client object should have a image object');
t.ok(osClient.apis.image, 'image object is aliased to image');

t.ok(osClient.apis['network.openshift.io'], 'osClient object should have a network object');
t.ok(osClient.apis.network, 'network object is aliased to network');

t.ok(osClient.apis['oauth.openshift.io'], 'osClient object should have a oauth object');
t.ok(osClient.apis.oauth, 'oauth object is aliased to oauth');

t.ok(osClient.apis['project.openshift.io'], 'osClient object should have a project object');
t.ok(osClient.apis.project, 'project object is aliased to project');

t.ok(osClient.apis['quota.openshift.io'], 'osClient object should have a quota object');
t.ok(osClient.apis.quota, 'quota object is aliased to quota');

t.ok(osClient.apis['route.openshift.io'], 'osClient object should have a route object');
t.ok(osClient.apis.route, 'route object is aliased to route');

t.ok(osClient.apis['security.openshift.io'], 'osClient object should have a security object');
t.ok(osClient.apis.security, 'security object is aliased to security');

t.ok(osClient.apis['template.openshift.io'], 'osClient object should have a template object');
t.ok(osClient.apis.template, 'template object is aliased to template');

t.ok(osClient.apis['user.openshift.io'], 'osClient object should have a user object');
t.ok(osClient.apis.user, 'user object is aliased to user');

t.ok(osClient.kubeconfig, 'client should have the kubeconfig object');
t.end();
});

0 comments on commit 4270caa

Please sign in to comment.