Skip to content

Commit f3d18b5

Browse files
datho7561rgrunber
authored andcommitted
If cluster is not up during login, pick new URL
When the user is attempting to login to a cluster, check if the cluster is accessible before prompting for credentials. To check if the cluster is accessible, send a GET request to the cluster API URL; if the API endpoint cannot be found: - if the URL appears to be a CRC api URL, offer to start CRC - if the URL appears to be a Dev Sandbox URL, link to the information page on Dev Sandbox - otherwise, display a warning message and return to the step where the user inputs the cluster URL. This means that if the user tries to login to a cluster that is not running, they don't have to click through the rest of the inputs before receiving this message. Also, if the cluster is not available since the user mistyped the URL, they have the chance to type it in again. Fixes #709 Signed-off-by: David Thompson <davthomp@redhat.com>
1 parent 8824f77 commit f3d18b5

File tree

1 file changed

+61
-9
lines changed

1 file changed

+61
-9
lines changed

src/openshift/cluster.ts

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
* Licensed under the MIT License. See LICENSE file in the project root for license information.
44
*-----------------------------------------------------------------------------------------------*/
55

6-
import { window, commands, env, QuickPickItem, ExtensionContext, Terminal, Uri, workspace, WebviewPanel, Progress as VProgress, QuickInputButton, ThemeIcon, QuickPickItemButtonEvent } from 'vscode';
6+
import { KubernetesObject } from '@kubernetes/client-node';
7+
import { ExtensionContext, QuickInputButton, QuickPickItem, QuickPickItemButtonEvent, Terminal, ThemeIcon, Uri, Progress as VProgress, WebviewPanel, commands, env, window, workspace } from 'vscode';
8+
import { CliChannel, CliExitData } from '../cli';
79
import { Command } from '../odo/command';
8-
import OpenShiftItem, { clusterRequired } from './openshiftItem';
9-
import { CliExitData, CliChannel } from '../cli';
1010
import { TokenStore } from '../util/credentialManager';
11-
import { KubeConfigUtils } from '../util/kubeUtils';
1211
import { Filters } from '../util/filters';
13-
import { Progress } from '../util/progress';
12+
import { KubeConfigUtils } from '../util/kubeUtils';
1413
import { Platform } from '../util/platform';
14+
import { Progress } from '../util/progress';
1515
import { WindowUtil } from '../util/windowUtils';
16-
import { vsCommand, VsCommandError } from '../vscommand';
16+
import { VsCommandError, vsCommand } from '../vscommand';
1717
import ClusterViewLoader from '../webview/cluster/clusterViewLoader';
18-
import { KubernetesObject } from '@kubernetes/client-node';
18+
import OpenShiftItem, { clusterRequired } from './openshiftItem';
19+
import fetch = require('make-fetch-happen');
1920

2021
interface Versions {
2122
'openshift_version': string;
@@ -275,9 +276,60 @@ export class Cluster extends OpenShiftItem {
275276

276277
if (response !== 'Yes') return null;
277278

278-
const clusterURL = await Cluster.getUrl();
279+
let clusterIsUp = false;
280+
let clusterURL: string;
279281

280-
if (!clusterURL) return null;
282+
do {
283+
clusterURL = await Cluster.getUrl();
284+
285+
if (!clusterURL) return null;
286+
287+
try {
288+
await fetch(`${clusterURL}/api`, {
289+
// disable cert checking. crc uses a self-signed cert,
290+
// which means this request will always fail on crc unless cert checking is disabled
291+
strictSSL: false
292+
});
293+
// since the fetch didn't throw an exception,
294+
// a route to the cluster is available,
295+
// so it's running
296+
clusterIsUp = true;
297+
} catch (e) {
298+
const clusterURLObj = new URL(clusterURL);
299+
if (clusterURLObj.hostname === 'api.crc.testing') {
300+
const startCrc = 'Start OpenShift Local';
301+
const promptResponse = await window.showWarningMessage(
302+
'The cluster appears to be a OpenShift Local cluster, but it isn\'t running',
303+
'Use a different cluster',
304+
startCrc,
305+
);
306+
if (promptResponse === startCrc){
307+
await commands.executeCommand('openshift.explorer.addCluster', 'crc');
308+
// no point in continuing with the wizard,
309+
// it will take the cluster a few minutes to stabilize
310+
return null;
311+
}
312+
} else if (/api\.sandbox-.*openshiftapps\.com/.test(clusterURLObj.hostname)) {
313+
const devSandboxSignup = 'Sign up for OpenShift Dev Sandbox';
314+
const promptResponse = await window.showWarningMessage(
315+
'The cluster appears to be a OpenShift Dev Sandbox cluster, but it isn\'t running',
316+
'Use a different cluster',
317+
devSandboxSignup,
318+
);
319+
if (promptResponse === devSandboxSignup){
320+
await commands.executeCommand('openshift.explorer.addCluster', 'sandbox');
321+
// no point in continuing with the wizard,
322+
// the user needs to sign up for the service
323+
return null;
324+
}
325+
} else {
326+
void window.showWarningMessage(
327+
'Unable to contact the cluster. Is it running and accessible?',
328+
);
329+
}
330+
331+
}
332+
} while (!clusterIsUp);
281333

282334
const loginActions = [
283335
{

0 commit comments

Comments
 (0)