Skip to content

Commit ee3c7dc

Browse files
vrubezhnydatho7561
authored andcommitted
As a user I initially couldn't find "Change Active Project" #3812
In Application Explorer this: - removes 'Change Active Project' inline action from the Cluster tree item (but it's still shown for the Project tree item); - Adds 'Change Active Project' context menu item to the Cluster and Project tree items context menu. - Disables the k8s Context and Namespace status bar items and adds the similar OpenShift Tools own items that use OpenShift Tools own actions to switch context/change current project Signed-off-by: Victor Rubezhny <[email protected]>
1 parent 57f4a68 commit ee3c7dc

File tree

3 files changed

+117
-9
lines changed

3 files changed

+117
-9
lines changed

package.json

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
"onCommand:openshift.explorer.refresh",
205205
"onCommand:openshift.componentTypesView.refresh",
206206
"onCommand:openshift.project.create",
207+
"onCommand:openshift.project.set",
207208
"onCommand:openshift.project.delete",
208209
"onCommand:openshift.project.delete.palette",
209210
"onCommand:openshift.component.openCreateComponent",
@@ -250,6 +251,22 @@
250251
"onWalkthrough:serverlessFunctionWalkthrough"
251252
],
252253
"contributes": {
254+
"configurationDefaults": {
255+
"vs-kubernetes": {
256+
"vs-kubernetes.disable-context-info-status-bar": true,
257+
"vs-kubernetes.disable-namespace-info-status-bar": true
258+
}
259+
},
260+
"icons": {
261+
"current-context": {
262+
"description": "context",
263+
"default":"selection"
264+
},
265+
"project-node": {
266+
"description": "project node",
267+
"default":"project"
268+
}
269+
},
253270
"commands": [
254271
{
255272
"command": "openshift.about",
@@ -1632,7 +1649,12 @@
16321649
{
16331650
"command": "openshift.project.create",
16341651
"when": "view == openshiftProjectExplorer && viewItem == openshift.k8sContext && canCreateNamespace",
1635-
"group": "c1"
1652+
"group": "c1@1"
1653+
},
1654+
{
1655+
"command": "openshift.project.set",
1656+
"when": "view == openshiftProjectExplorer && viewItem == openshift.k8sContext && canCreateNamespace",
1657+
"group": "c1@2"
16361658
},
16371659
{
16381660
"command": "openshift.explorer.logout",
@@ -1654,10 +1676,20 @@
16541676
"when": "view == openshiftProjectExplorer && viewItem =~ /openshift.project.*/i && isLoggedIn",
16551677
"group": "p2"
16561678
},
1679+
{
1680+
"command": "openshift.project.set",
1681+
"when": "view == openshiftProjectExplorer && viewItem =~ /openshift.project.*/i && canCreateNamespace",
1682+
"group": "p3@1"
1683+
},
16571684
{
16581685
"command": "openshift.project.delete",
16591686
"when": "view == openshiftProjectExplorer && viewItem =~ /openshift.project.*.can-delete/i",
1660-
"group": "p3"
1687+
"group": "p3@2"
1688+
},
1689+
{
1690+
"command": "openshift.project.set",
1691+
"when": "view == openshiftProjectExplorer && viewItem =~ /openshift.project.*/i && canCreateNamespace",
1692+
"group": "inline"
16611693
},
16621694
{
16631695
"command": "openshift.component.describe",
@@ -1775,11 +1807,6 @@
17751807
"when": "view == openshiftProjectExplorer && viewItem == openshift.openConfigFile",
17761808
"group": "inline"
17771809
},
1778-
{
1779-
"command": "openshift.project.set",
1780-
"when": "view == openshiftProjectExplorer && (viewItem == openshift.k8sContext || viewItem =~ /openshift.project.*/i) && canCreateNamespace",
1781-
"group": "inline"
1782-
},
17831810
{
17841811
"command": "openshift.component.revealInExplorer",
17851812
"when": "view == openshiftComponentsView && viewItem =~ /openshift\\.component.*/",
@@ -2087,6 +2114,21 @@
20872114
"order": 1,
20882115
"title": "OpenShift Toolkit",
20892116
"properties": {
2117+
"openshiftToolkit": {
2118+
"type": "object",
2119+
"title": "Additional settings",
2120+
"description": "OpenShift Toolkit configuration",
2121+
"properties": {
2122+
"openshiftToolkit.disable-context-info-status-bar": {
2123+
"type": "boolean",
2124+
"description": "Disable displaying the current Kubernetes context in VS Code's status bar."
2125+
},
2126+
"openshiftToolkit.disable-namespace-info-status-bar": {
2127+
"type": "boolean",
2128+
"description": "Disable displaying the active namespace in VS Code's status bar."
2129+
}
2130+
}
2131+
},
20902132
"openshiftToolkit.showWelcomePage": {
20912133
"type": "boolean",
20922134
"default": true,

src/explorer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
7878
readonly onDidChangeTreeData: Event<ExplorerItem | undefined> = this
7979
.eventEmitter.event;
8080

81+
public onDidChangeContextEmitter = new EventEmitter<string>();
82+
8183
private constructor() {
8284
try {
8385
this.kubeConfig = new KubeConfigUtils();
@@ -100,6 +102,7 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
100102
|| this.kubeContext.user !== newCtx.user
101103
|| this.kubeContext.namespace !== newCtx.namespace)) {
102104
this.refresh();
105+
this.onDidChangeContextEmitter.fire(newCtx.name);
103106
}
104107
this.kubeContext = newCtx;
105108
this.kubeConfig = ku2;
@@ -234,6 +237,7 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
234237
} catch (err) {
235238
// ignore because ether server is not accessible or user is logged out
236239
}
240+
OpenShiftExplorer.getInstance().onDidChangeContextEmitter.fire(new KubeConfigUtils().currentContext);
237241
} else if ('name' in element) { // we are dealing with context here
238242
// user is logged into cluster from current context
239243
// and project should be show as child node of current context

src/extension.ts

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import { ServerlessFunctionView } from './serverlessFunction/view';
2525
import { startTelemetry } from './telemetry';
2626
import { ToolsConfig } from './tools';
2727
import { TokenStore } from './util/credentialManager';
28-
import { setKubeConfig } from './util/kubeUtils';
28+
import { KubeConfigUtils, setKubeConfig } from './util/kubeUtils';
29+
import { Context as KcuContext } from '@kubernetes/client-node/dist/config_types';
2930
import { Platform } from './util/platform';
3031
import { setupWorkspaceDevfileContext } from './util/workspace';
3132
import { registerCommands } from './vscommand';
@@ -34,6 +35,7 @@ import { WelcomePage } from './welcomePage';
3435
import { registerYamlHandlers } from './yaml/yamlDocumentFeatures';
3536

3637
import fsx = require('fs-extra');
38+
import { Oc } from './oc/ocWrapper';
3739

3840
// eslint-disable-next-line @typescript-eslint/no-empty-function
3941
// this method is called when your extension is deactivated
@@ -79,6 +81,13 @@ export async function activate(extensionContext: ExtensionContext): Promise<unkn
7981

8082
const crcStatusItem = window.createStatusBarItem(StatusBarAlignment.Left);
8183
crcStatusItem.command = 'openshift.explorer.stopCluster';
84+
85+
const activeNamespaceStatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left, 1);
86+
activeNamespaceStatusBarItem.command = 'openshift.project.set';
87+
88+
const activeContextStatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left, 2);
89+
activeContextStatusBarItem.command = 'openshift.explorer.switchContext';
90+
8291
const disposable = [
8392
...(await registerCommands(
8493
'./k8s/route',
@@ -99,6 +108,8 @@ export async function activate(extensionContext: ExtensionContext): Promise<unkn
99108
commands.executeCommand('extension.vsKubernetesUseNamespace', context),
100109
),
101110
crcStatusItem,
111+
activeNamespaceStatusBarItem,
112+
activeContextStatusBarItem,
102113
OpenShiftExplorer.getInstance(),
103114
new DebugSessionsView().createTreeView('openshiftDebugView'),
104115
...Component.init(),
@@ -182,7 +193,7 @@ export async function activate(extensionContext: ExtensionContext): Promise<unkn
182193
}
183194

184195
function createStatusBarItem(context: ExtensionContext) {
185-
const item = window.createStatusBarItem(StatusBarAlignment.Left, 1);
196+
const item = window.createStatusBarItem(StatusBarAlignment.Left, 3);
186197
item.command = 'openshift.openStatusBar';
187198
context.subscriptions.push(item);
188199
context.subscriptions.push(statusBarFunctions());
@@ -201,6 +212,57 @@ export async function activate(extensionContext: ExtensionContext): Promise<unkn
201212
statusBarItem.show();
202213
}
203214

215+
// Disable the k8s extension displaying its context/namespace status bar items...
216+
// ...and setup our own context and namespace status bar informer/picker items
217+
218+
// if true will disable displaying the namespace from the status bar
219+
function isNamespaceInfoStatusBarDisabled(): boolean {
220+
// If k8s extension's Namespace Picker is eenabled - do not show the OS Tools's one
221+
if (workspace.getConfiguration('vs-kubernetes').get('vs-kubernetes.disable-namespace-info-status-bar') === false) {
222+
return true;
223+
}
224+
225+
return workspace.getConfiguration('openshiftToolkit').get['openshiftToolkit.disable-namespace-info-status-bar'];
226+
}
227+
228+
// if true will disable displaying the context from the status bar
229+
function isContextInfoStatusBarDisabled(): boolean {
230+
// If k8s extension's Namespace Picker is eenabled - do not show the OS Tools's one
231+
if (workspace.getConfiguration('vs-kubernetes').get('vs-kubernetes.disable-context-info-status-bar') === false) {
232+
return true;
233+
}
234+
235+
return workspace.getConfiguration('openshiftToolkit').get['openshiftToolkit.disable-context-info-status-bar'];
236+
}
237+
238+
function updateContextStatusBarItem(statusBarItem: StatusBarItem, iconId: string, text: string, tooltip: string, show: boolean): void {
239+
statusBarItem.text = `$(${iconId}) ${text}`;
240+
statusBarItem.tooltip = tooltip;
241+
if (show && text) {
242+
statusBarItem.show();
243+
} else {
244+
statusBarItem.hide();
245+
}
246+
}
247+
248+
OpenShiftExplorer.getInstance().onDidChangeContextEmitter.event((context) => {
249+
void Oc.Instance.getActiveProject().then((namespace) =>
250+
updateContextStatusBarItem(activeNamespaceStatusBarItem, 'project-node', namespace, `Current namespace: ${namespace}`,
251+
!isNamespaceInfoStatusBarDisabled()));
252+
253+
const kcu: KubeConfigUtils = new KubeConfigUtils();
254+
const currentContext: KcuContext = kcu.findContext(context);
255+
updateContextStatusBarItem(activeContextStatusBarItem, 'current-context', currentContext?.name, `${currentContext?.name}\nCluster: ${currentContext?.cluster}`,
256+
!isContextInfoStatusBarDisabled());
257+
});
258+
259+
const kcu: KubeConfigUtils = new KubeConfigUtils();
260+
const currentContext: KcuContext = kcu.findContext(kcu.currentContext);
261+
updateContextStatusBarItem(activeNamespaceStatusBarItem, 'project-node', null, 'Current namespace',
262+
!isNamespaceInfoStatusBarDisabled());
263+
updateContextStatusBarItem(activeContextStatusBarItem, 'current-context', currentContext?.name, `${currentContext?.name}\nCluster: ${currentContext?.cluster}`,
264+
!isContextInfoStatusBarDisabled());
265+
204266
updateStatusBarItem(crcStatusItem, 'Stop CRC');
205267
void extendClusterExplorer();
206268

0 commit comments

Comments
 (0)