Skip to content

Commit

Permalink
Extension is slow to activate #4226
Browse files Browse the repository at this point in the history
The PR adds some optimization to the extension activation.

Also, the PR removes function 'migrateFromOdo018' which looks over outdated and doesn't make its
work properly after a numerous changes in ODO. See: #4255

Fixes: #4255
Issue: #4226

Signed-off-by: Victor Rubezhny <[email protected]>
  • Loading branch information
vrubezhny committed Jul 3, 2024
1 parent ab93e7e commit b336974
Showing 1 changed file with 70 additions and 80 deletions.
150 changes: 70 additions & 80 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

import * as path from 'path';
import {
authentication,
commands, env, ExtensionContext, languages, QuickPickItemKind,
Expand All @@ -27,14 +26,12 @@ import { ToolsConfig } from './tools';
import { TokenStore } from './util/credentialManager';
import { getNamespaceKind, KubeConfigUtils, setKubeConfig } from './util/kubeUtils';
import { Context as KcuContext } from '@kubernetes/client-node/dist/config_types';
import { Platform } from './util/platform';
import { setupWorkspaceDevfileContext } from './util/workspace';
import { registerCommands } from './vscommand';
import { OpenShiftTerminalManager } from './webview/openshift-terminal/openShiftTerminal';
import { WelcomePage } from './welcomePage';
import { registerYamlHandlers } from './yaml/yamlDocumentFeatures';

import fsx = require('fs-extra');
import { Oc } from './oc/ocWrapper';
import { K8S_RESOURCE_SCHEME, K8S_RESOURCE_SCHEME_READONLY, KubernetesResourceVirtualFileSystemProvider } from './k8s/vfs/kuberesources.virtualfs';
import { KubernetesResourceLinkProvider } from './k8s/vfs/kuberesources.linkprovider';
Expand All @@ -45,16 +42,6 @@ export function deactivate(): void {
// intentionally left blank
}

function migrateFromOdo018(): void {
const newCfgDir = path.join(Platform.getUserHomePath(), '.odo');
const newCfg = path.join(newCfgDir, 'odo-config.yaml');
const oldCfg = path.join(Platform.getUserHomePath(), '.kube', 'odo');
if (!fsx.existsSync(newCfg) && fsx.existsSync(oldCfg)) {
fsx.ensureDirSync(newCfgDir);
fsx.copyFileSync(oldCfg, newCfg);
}
}

async function verifyBundledBinaries(): Promise<{ odoPath: string, ocPath: string, helmPath: string }> {
return {
odoPath: await ToolsConfig.detect('odo'),
Expand All @@ -72,9 +59,9 @@ async function registerKubernetesCloudProvider(): Promise<void> {

export async function activate(extensionContext: ExtensionContext): Promise<unknown> {
void WelcomePage.createOrShow();
await commands.executeCommand('setContext', 'isVSCode', env.uiKind);
void commands.executeCommand('setContext', 'isVSCode', env.uiKind);
// UIKind.Desktop ==1 & UIKind.Web ==2. These conditions are checked for browser based & electron based IDE.
migrateFromOdo018();

Cluster.extensionContext = extensionContext;
TokenStore.extensionContext = extensionContext;

Expand All @@ -96,49 +83,51 @@ export async function activate(extensionContext: ExtensionContext): Promise<unkn
const activeContextStatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left, 2);
activeContextStatusBarItem.command = 'openshift.explorer.switchContext';

const disposable = [
...(await registerCommands(
'./k8s/route',
'./openshift/project',
'./openshift/cluster',
'./openshift/service',
'./openshift/route',
'./k8s/console',
'./yamlFileCommands',
'./registriesView',
'./componentsView',
'./webview/devfile-registry/registryViewLoader',
'./webview/helm-chart/helmChartLoader',
'./webview/helm-manage-repository/manageRepositoryLoader',
'./feedback',
'./deployment'
)),
crcStatusItem,
activeNamespaceStatusBarItem,
activeContextStatusBarItem,
OpenShiftExplorer.getInstance(),
new DebugSessionsView().createTreeView('openshiftDebugView'),
...Component.init(),
ComponentTypesView.instance.createTreeView('openshiftComponentTypesView'),
ServerlessFunctionView.getInstance(),
ComponentsTreeDataProvider.instance.createTreeView('openshiftComponentsView'),
setupWorkspaceDevfileContext(),
window.registerWebviewViewProvider('openShiftTerminalView', OpenShiftTerminalManager.getInstance(), { webviewOptions: { retainContextWhenHidden: true, } }),
...registerYamlHandlers(),
// Temporarily loaded resource providers
workspace.registerFileSystemProvider(K8S_RESOURCE_SCHEME, resourceDocProvider, { /* TODO: case sensitive? */ }),
workspace.registerFileSystemProvider(K8S_RESOURCE_SCHEME_READONLY, resourceDocProvider, { isReadonly: true }),

// Link from resources to referenced resources
languages.registerDocumentLinkProvider({ scheme: K8S_RESOURCE_SCHEME }, resourceLinkProvider),

];
disposable.forEach((value) => extensionContext.subscriptions.push(value));
const pushSubscriptions = async function(): Promise<void> {
const disposable = [
...(await registerCommands(
'./k8s/route',
'./openshift/project',
'./openshift/cluster',
'./openshift/service',
'./openshift/route',
'./k8s/console',
'./yamlFileCommands',
'./registriesView',
'./componentsView',
'./webview/devfile-registry/registryViewLoader',
'./webview/helm-chart/helmChartLoader',
'./webview/helm-manage-repository/manageRepositoryLoader',
'./feedback',
'./deployment'
)),
crcStatusItem,
activeNamespaceStatusBarItem,
activeContextStatusBarItem,
OpenShiftExplorer.getInstance(),
new DebugSessionsView().createTreeView('openshiftDebugView'),
...Component.init(),
ComponentTypesView.instance.createTreeView('openshiftComponentTypesView'),
ServerlessFunctionView.getInstance(),
ComponentsTreeDataProvider.instance.createTreeView('openshiftComponentsView'),
setupWorkspaceDevfileContext(),
window.registerWebviewViewProvider('openShiftTerminalView', OpenShiftTerminalManager.getInstance(), { webviewOptions: { retainContextWhenHidden: true, } }),
...registerYamlHandlers(),
// Temporarily loaded resource providers
workspace.registerFileSystemProvider(K8S_RESOURCE_SCHEME, resourceDocProvider, { /* TODO: case sensitive? */ }),
workspace.registerFileSystemProvider(K8S_RESOURCE_SCHEME_READONLY, resourceDocProvider, { isReadonly: true }),

// Link from resources to referenced resources
languages.registerDocumentLinkProvider({ scheme: K8S_RESOURCE_SCHEME }, resourceLinkProvider),
];
disposable.forEach((value) => extensionContext.subscriptions.push(value));
}
void pushSubscriptions();

// activate "Sign in with Red Hat ..."
void authentication.getSession('redhat-account-auth', ['openid'], { silent: false });

function statusBarFunctions() {
const statusBarFunctions = function() {
return commands.registerCommand('openshift.openStatusBar', async () => {
const selection = await window.showQuickPick(
[
Expand Down Expand Up @@ -201,11 +190,10 @@ export async function activate(extensionContext: ExtensionContext): Promise<unkn
default:
break;
}

});
}

function createStatusBarItem(context: ExtensionContext) {
const createOpenShiftStatusBarItem = function(context: ExtensionContext) {
const item = window.createStatusBarItem(StatusBarAlignment.Left, 3);
item.command = 'openshift.openStatusBar';
context.subscriptions.push(item);
Expand All @@ -214,9 +202,9 @@ export async function activate(extensionContext: ExtensionContext): Promise<unkn
item.show();
}

createStatusBarItem(extensionContext);
createOpenShiftStatusBarItem(extensionContext);

function updateStatusBarItem(statusBarItem: StatusBarItem, text: string): void {
const updateStatusBarItem = function(statusBarItem: StatusBarItem, text: string): void {
if (!workspace.getConfiguration('openshiftToolkit').get('crcBinaryLocation')) {
statusBarItem.hide();
return;
Expand All @@ -229,26 +217,24 @@ export async function activate(extensionContext: ExtensionContext): Promise<unkn
// ...and setup our own context and namespace status bar informer/picker items

// if true will disable displaying the namespace from the status bar
function isNamespaceInfoStatusBarDisabled(): boolean {
const isNamespaceInfoStatusBarDisabled = function(): boolean {
// If k8s extension's Namespace Picker is eenabled - do not show the OS Tools's one
if (workspace.getConfiguration('vs-kubernetes').get('vs-kubernetes.disable-namespace-info-status-bar') === false) {
return true;
}

return workspace.getConfiguration('openshiftToolkit').get['openshiftToolkit.disable-namespace-info-status-bar'];
}

// if true will disable displaying the context from the status bar
function isContextInfoStatusBarDisabled(): boolean {
const isContextInfoStatusBarDisabled = function(): boolean {
// If k8s extension's Namespace Picker is eenabled - do not show the OS Tools's one
if (workspace.getConfiguration('vs-kubernetes').get('vs-kubernetes.disable-context-info-status-bar') === false) {
return true;
}

return workspace.getConfiguration('openshiftToolkit').get['openshiftToolkit.disable-context-info-status-bar'];
}

function updateContextStatusBarItem(statusBarItem: StatusBarItem, iconId: string, text: string, tooltip: string, show: boolean): void {
const updateContextStatusBarItem = function(statusBarItem: StatusBarItem, iconId: string, text: string, tooltip: string, show: boolean): void {
statusBarItem.text = `$(${iconId}) ${text}`;
statusBarItem.tooltip = tooltip;
if (show && text) {
Expand All @@ -258,34 +244,38 @@ export async function activate(extensionContext: ExtensionContext): Promise<unkn
}
}

OpenShiftExplorer.getInstance().onDidChangeContextEmitter.event(async (context) => {
const kind = await getNamespaceKind();
void Oc.Instance.getActiveProject().then((namespace) =>
updateContextStatusBarItem(activeNamespaceStatusBarItem, 'project-node', namespace, `Current ${kind}: ${namespace}`,
!isNamespaceInfoStatusBarDisabled()));
const updateContextAndProjectStatusBarItems = async function(context?: string): Promise<void> {
return getNamespaceKind().then(async (kind) => {
const namespace = await Oc.Instance.getActiveProject();
if (namespace) {
updateContextStatusBarItem(activeNamespaceStatusBarItem, 'project-node', namespace, `Current ${kind}: ${namespace}`,
!isNamespaceInfoStatusBarDisabled());
} else {
updateContextStatusBarItem(activeNamespaceStatusBarItem, 'project-node', '', '', false);
}

const kcu: KubeConfigUtils = new KubeConfigUtils();
const currentContext: KcuContext = kcu.findContext(context ? context : kcu.currentContext);
updateContextStatusBarItem(activeContextStatusBarItem, 'current-context', currentContext?.name, `${currentContext?.name}\nCluster: ${currentContext?.cluster}`,
!isContextInfoStatusBarDisabled());
});
}

const kcu: KubeConfigUtils = new KubeConfigUtils();
const currentContext: KcuContext = kcu.findContext(context);
updateContextStatusBarItem(activeContextStatusBarItem, 'current-context', currentContext?.name, `${currentContext?.name}\nCluster: ${currentContext?.cluster}`,
!isContextInfoStatusBarDisabled());
OpenShiftExplorer.getInstance().onDidChangeContextEmitter.event((context) => {
void updateContextAndProjectStatusBarItems(context);
});

const kcu: KubeConfigUtils = new KubeConfigUtils();
const kind = await getNamespaceKind();
const currentContext: KcuContext = kcu.findContext(kcu.currentContext);
updateContextStatusBarItem(activeNamespaceStatusBarItem, 'project-node', null, `Current ${kind}`,
!isNamespaceInfoStatusBarDisabled());
updateContextStatusBarItem(activeContextStatusBarItem, 'current-context', currentContext?.name, `${currentContext?.name}\nCluster: ${currentContext?.cluster}`,
!isContextInfoStatusBarDisabled());
void updateContextAndProjectStatusBarItems();

updateStatusBarItem(crcStatusItem, 'Stop CRC');

void extendClusterExplorer();

await registerKubernetesCloudProvider();
void startTelemetry(extensionContext);
await verifyBinariesInRemoteContainer();

return {
verifyBundledBinaries,
verifyBundledBinaries
};
}

0 comments on commit b336974

Please sign in to comment.