Skip to content

Commit d4630fd

Browse files
committed
Detect loggin status using listing projects to support k8s clusters
This PR fixes #2393. Signed-off-by: Denis Golovin [email protected]
1 parent c633f89 commit d4630fd

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/odo.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -507,13 +507,22 @@ export class OdoImpl implements Odo {
507507
const result: cliInstance.CliExitData = await this.execute(
508508
Command.printOdoVersion(), process.cwd(), false
509509
);
510-
commands.executeCommand('setContext', 'isLoggedIn', false);
510+
void commands.executeCommand('setContext', 'isLoggedIn', false);
511511
clusters = result.stdout.trim().split('\n')
512512
.filter((value) => value.includes('Server:'))
513513
.map((value) => {
514-
commands.executeCommand('setContext', 'isLoggedIn', true);
515-
return new OpenShiftCluster(value.substr(value.indexOf(':')+1).trim())
514+
void commands.executeCommand('setContext', 'isLoggedIn', true);
515+
return new OpenShiftCluster(value.substr(value.indexOf(':')+1).trim());
516516
});
517+
if (clusters.length === 0) {
518+
const projects = await this.execute(
519+
Command.listProjects(), process.cwd(), false
520+
);
521+
if (!projects.error) {
522+
clusters.push(new OpenShiftCluster(new KubeConfigUtils().getCurrentCluster().server));
523+
void commands.executeCommand('setContext', 'isLoggedIn', true);
524+
}
525+
}
517526
return clusters;
518527
}
519528

@@ -786,8 +795,10 @@ export class OdoImpl implements Odo {
786795
}
787796

788797
public async requireLogin(): Promise<boolean> {
789-
const result: cliInstance.CliExitData = await this.execute(new CommandText('oc whoami'), process.cwd(), false);
790-
return !!result.error;
798+
return await Promise.any([
799+
this.execute(new CommandText('oc whoami')),
800+
this.execute(Command.listProjects())
801+
]).then(() => false).catch(() => true);
791802
}
792803

793804
private async insertAndReveal(item: OpenShiftObject, notification = true): Promise<OpenShiftObject> {

test/unit/odo.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,12 @@ suite('odo', () => {
174174
test('getProjects returns empty list if no projects present', async () => {
175175
execStub.onFirstCall().resolves({
176176
error: undefined,
177-
stdout: 'Server https://172.17.185.52:8443',
177+
stdout: 'Server: https://172.17.185.52:8443',
178178
stderr: ''
179179
});
180180
execStub.onSecondCall().resolves({
181181
error: undefined,
182-
stdout: 'Server https://172.17.185.52:8443',
182+
stdout: 'Server: https://172.17.185.52:8443',
183183
stderr: ''
184184
});
185185
execStub.onThirdCall().resolves({ stdout: '', stderr: '', error: null });
@@ -576,12 +576,12 @@ suite('odo', () => {
576576
const stub = sandbox.stub(odoCli, 'execute').resolves({ error: null, stdout: 'logged in', stderr: ''});
577577
const result = await odoCli.requireLogin();
578578

579-
expect(stub).calledOnceWith(new CommandText('oc whoami'));
579+
expect(stub).calledWith(new CommandText('oc whoami'));
580580
expect(result).false;
581581
});
582582

583583
test('requireLogin returns true if odo is not logged in to the cluster', async () => {
584-
sandbox.stub(odoCli, 'execute').resolves({ error: new Error('Not logged in!'), stdout: '', stderr: ''});
584+
sandbox.stub(odoCli, 'execute').returns(Promise.reject('Not logged in!'));
585585
const result = await odoCli.requireLogin();
586586

587587
expect(result).true;

0 commit comments

Comments
 (0)