Skip to content

Commit 9115860

Browse files
committed
Report kubernetes and openshift version for login command
This PR fixes redhat-developer#2149. Signed-off-by: Denis Golovin [email protected]
1 parent 9d5fedf commit 9115860

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/odo/command.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ export class Command {
165165
return new CommandText('oc version');
166166
}
167167

168+
static printOcVersionJson(): CommandText {
169+
return new CommandText('oc version -ojson');
170+
}
171+
168172
static listServiceInstances(project: string, app: string): CommandText {
169173
return new CommandText('odo service list',
170174
undefined, [

src/openshift/cluster.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright (c) Red Hat, Inc. All rights reserved.
33
* Licensed under the MIT License. See LICENSE file in the project root for license information.
44
*-----------------------------------------------------------------------------------------------*/
5+
/* eslint-disable camelcase */
56

67
import { window, commands, env, QuickPickItem, ExtensionContext, Terminal, Uri, workspace } from 'vscode';
78
import { Command } from '../odo/command';
@@ -16,6 +17,11 @@ import { WindowUtil } from '../util/windowUtils';
1617
import { vsCommand, VsCommandError } from '../vscommand';
1718
import ClusterViewLoader from '../webview/cluster/clusterViewLoader';
1819

20+
interface Versions {
21+
'openshift_version': string;
22+
'kubernetes_version': string;
23+
}
24+
1925
export class Cluster extends OpenShiftItem {
2026
public static extensionContext: ExtensionContext;
2127

@@ -137,6 +143,32 @@ export class Cluster extends OpenShiftItem {
137143
return;
138144
}
139145

146+
public static async getVersions(): Promise<Versions> {
147+
const result = await Cluster.odo.execute(Command.printOcVersionJson(), undefined, false);
148+
const versions: Versions = {
149+
'kubernetes_version': undefined,
150+
'openshift_version': undefined
151+
};
152+
if (!result.error) {
153+
try {
154+
// try to fetch versions for stdout
155+
const versionsJson = JSON.parse(result.stdout);
156+
if (versionsJson?.serverVersion?.major && versionsJson?.serverVersion?.minor) {
157+
// eslint-disable-next-line @typescript-eslint/camelcase
158+
versions.kubernetes_version = `${versionsJson.serverVersion.major}.${versionsJson.serverVersion.minor}`;
159+
}
160+
if (versionsJson?.openshiftVersion) {
161+
// eslint-disable-next-line @typescript-eslint/camelcase
162+
versions.openshift_version = versionsJson.openshiftVersion;
163+
}
164+
165+
} catch(err) {
166+
// ignore and return undefined
167+
}
168+
}
169+
return versions;
170+
}
171+
140172
@vsCommand('openshift.explorer.login')
141173
static async login(context?: any, skipConfirmation = false): Promise<string> {
142174
const response = await Cluster.requestLoginConfirmation(skipConfirmation);
@@ -159,7 +191,16 @@ export class Cluster extends OpenShiftItem {
159191
];
160192
const loginActionSelected = await window.showQuickPick(loginActions, {placeHolder: 'Select a way to log in to the cluster.', ignoreFocusOut: true});
161193
if (!loginActionSelected) return null;
162-
return loginActionSelected.label === 'Credentials' ? await Cluster.credentialsLogin(true, clusterURL) : await Cluster.tokenLogin(clusterURL, true);
194+
let result:any = loginActionSelected.label === 'Credentials' ? await Cluster.credentialsLogin(true, clusterURL) : await Cluster.tokenLogin(clusterURL, true);
195+
if (result) {
196+
const versions = await Cluster.getVersions();
197+
if (versions) {
198+
result = new String(result);
199+
// get cluster information using 'oc version'
200+
result.properties = versions;
201+
}
202+
}
203+
return result;
163204
}
164205

165206
private static async requestLoginConfirmation(skipConfirmation = false): Promise<string> {

0 commit comments

Comments
 (0)