Skip to content

Commit 4b8924e

Browse files
authored
Added switch and delete icons on manage contexts show-pick and done refactoring (#2684)
Fix #2684. * added switch and delete icons on manage contexts show-pick and done refactoring * changed odo initialization * added onDidAccept listener for getting selected value from quick pick * title changed * removed await * addressed the review comments * Add error handling to switch context command execution * Resolve switch context command to null when cancelled by user to send correct telemetry event Signed-off-by: msivasubramaniaan <msivasub@redhat.com> Co-authored-by: Denis Golovin dgolovin@redhat.com
1 parent 8382031 commit 4b8924e

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@
837837
"viewsWelcome": [
838838
{
839839
"view": "openshiftProjectExplorer",
840-
"contents": "Cluster is not accessible or you are not logged in.\n[Login](command:openshift.explorer.login)\n[Select Kubernetes Context ](command:openshift.explorer.switchContext)\n[Add OpenShift Cluster](command:openshift.explorer.addCluster)\n[$(git-branch) Import from Git](command:openshift.component.openImportFromGit)\n[Create Component](command:openshift.component.createFromLocal)",
840+
"contents": "Cluster is not accessible or you are not logged in.\n[Login](command:openshift.explorer.login)\n[Select Kubernetes Context](command:openshift.explorer.switchContext)\n[Add OpenShift Cluster](command:openshift.explorer.addCluster)\n[$(git-branch) Import from Git](command:openshift.component.openImportFromGit)\n[Create Component](command:openshift.component.createFromLocal)",
841841
"enablement": "openshift.app.explorer.init"
842842
},
843843
{

src/odo/command.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ export class Command {
369369
return new CommandText('oc delete', name);
370370
}
371371

372+
static deleteContext(name: string): CommandText {
373+
return new CommandText('oc config delete-context', name);
374+
}
375+
372376
static getServiceTemplate(project: string, service: string): CommandText {
373377
return new CommandText('oc get ServiceInstance',
374378
service, [

src/openshift/cluster.ts

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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 } from 'vscode';
6+
import { window, commands, env, QuickPickItem, ExtensionContext, Terminal, Uri, workspace, WebviewPanel, Progress as VProgress, QuickPickItemButtonEvent, ThemeIcon, QuickInputButton } from 'vscode';
77
import { Command } from '../odo/command';
88
import OpenShiftItem, { clusterRequired } from './openshiftItem';
99
import { CliExitData, CliChannel } from '../cli';
@@ -22,6 +22,10 @@ interface Versions {
2222
'kubernetes_version': string;
2323
}
2424

25+
class quickBtn implements QuickInputButton {
26+
constructor(public iconPath: ThemeIcon, public tooltip: string) { }
27+
}
28+
2529
export class Cluster extends OpenShiftItem {
2630
public static extensionContext: ExtensionContext;
2731

@@ -34,7 +38,7 @@ export class Cluster extends OpenShiftItem {
3438
.then(async (result) => {
3539
if (result.stderr === '') {
3640
Cluster.explorer.refresh();
37-
commands.executeCommand('setContext', 'isLoggedIn', false);
41+
void commands.executeCommand('setContext', 'isLoggedIn', false);
3842
const logoutInfo = await window.showInformationMessage('Successfully logged out. Do you want to login to a new cluster', 'Yes', 'No');
3943
if (logoutInfo === 'Yes') {
4044
return Cluster.login(undefined, true);
@@ -103,20 +107,50 @@ export class Cluster extends OpenShiftItem {
103107

104108
@vsCommand('openshift.explorer.switchContext')
105109
static async switchContext(): Promise<string> {
106-
const k8sConfig = new KubeConfigUtils();
107-
const contexts = k8sConfig.contexts.filter((item) => item.name !== k8sConfig.currentContext);
108-
const contextName: QuickPickItem[] = contexts.map((ctx) => ({ label: `${ctx.name}`}));
109-
if (contextName.length === 0) {
110-
const command = await window.showInformationMessage('You have no Kubernetes contexts yet, please login to a cluster.', 'Login', 'Cancel');
111-
if (command === 'Login') {
112-
return Cluster.login(undefined, true);
110+
return new Promise<string>((resolve, reject) => {
111+
const k8sConfig = new KubeConfigUtils();
112+
const contexts = k8sConfig.contexts.filter((item) => item.name !== k8sConfig.currentContext);
113+
const deleteBtn = new quickBtn(new ThemeIcon('trash'), 'Delete');
114+
const quickPick = window.createQuickPick();
115+
const contextNames: QuickPickItem[] = contexts.map((ctx) => ({ label: `${ctx.name}`, buttons: [deleteBtn] }));
116+
quickPick.items = contextNames;
117+
if (contextNames.length === 0) {
118+
void window.showInformationMessage('You have no Kubernetes contexts yet, please login to a cluster.', 'Login', 'Cancel')
119+
.then((command: string) => {
120+
if (command === 'Login') {
121+
resolve(Cluster.login(undefined, true));
122+
}
123+
resolve(null);
124+
})
125+
} else {
126+
let selection: readonly QuickPickItem[] | undefined;
127+
const hideDisposable = quickPick.onDidHide(() => resolve(null));
128+
quickPick.onDidChangeSelection((selects) => {
129+
selection = selects;
130+
});
131+
quickPick.onDidAccept(() => {
132+
const choice = selection[0];
133+
hideDisposable.dispose();
134+
quickPick.hide();
135+
Cluster.odo.execute(Command.setOpenshiftContext(choice.label))
136+
.then(() => resolve(`Cluster context is changed to: ${choice.label}.`))
137+
.catch(reject);
138+
});
139+
quickPick.onDidTriggerItemButton(async (event: QuickPickItemButtonEvent<QuickPickItem>) => {
140+
const answer = await window.showInformationMessage(`Do you want to delete '${event.item.label}' Context from Kubernetes configuration?`, 'Yes', 'No');
141+
if (answer === 'Yes') {
142+
const context = k8sConfig.getContextObject(event.item.label);
143+
const index = contexts.indexOf(context);
144+
if (index > -1) {
145+
CliChannel.getInstance().executeTool(Command.deleteContext(context.name))
146+
.then(() => resolve(`Context ${context.name} deleted.`))
147+
.catch(reject);
148+
}
149+
}
150+
});
151+
quickPick.show();
113152
}
114-
return null;
115-
}
116-
const choice = await window.showQuickPick(contextName, {placeHolder: 'Select a new OpenShift context'});
117-
if (!choice) return null;
118-
await Cluster.odo.execute(Command.setOpenshiftContext(choice.label));
119-
return `Cluster context is changed to: ${choice.label}`;
153+
});
120154
}
121155

122156
static async getUrl(): Promise<string | null> {

src/util/kubeUtils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function fileExists(file: string): boolean {
1919
}
2020

2121
export class KubeConfigUtils extends KubeConfig {
22+
2223
public readonly loadingError: any;
2324
constructor() {
2425
super();

0 commit comments

Comments
 (0)