From 50b346f5cae9620648119fd52108e982f0eb5e9f Mon Sep 17 00:00:00 2001 From: Victor Rubezhny Date: Wed, 3 Jul 2024 22:47:44 +0200 Subject: [PATCH] Extension is slow to activate #4226 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 Fixes: #4226 Signed-off-by: Victor Rubezhny --- src/extension.ts | 149 ++++++++++++++++++++++------------------------- 1 file changed, 70 insertions(+), 79 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 21d967990..62cdd4655 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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, @@ -11,7 +10,7 @@ import { StatusBarItem, window, workspace } from 'vscode'; -import * as k8s from 'vscode-kubernetes-tools-api'; +import { extension as k8sExtension } from 'vscode-kubernetes-tools-api'; import { REDHAT_CLOUD_PROVIDER } from './cloudProvider/redhatCloudProvider'; import { ComponentsTreeDataProvider } from './componentsView'; import { DebugSessionsView } from './debug'; @@ -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'; @@ -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'), @@ -64,7 +51,7 @@ async function verifyBundledBinaries(): Promise<{ odoPath: string, ocPath: strin } async function registerKubernetesCloudProvider(): Promise { - const cloudProvider = await k8s.extension.cloudExplorer.v1; + const cloudProvider = await k8sExtension.cloudExplorer.v1; if (cloudProvider.available) { cloudProvider.api.registerCloudProvider(REDHAT_CLOUD_PROVIDER); } @@ -72,9 +59,8 @@ async function registerKubernetesCloudProvider(): Promise { export async function activate(extensionContext: ExtensionContext): Promise { void WelcomePage.createOrShow(); - await commands.executeCommand('setContext', 'isVSCode', env.uiKind); - // UIKind.Desktop ==1 & UIKind.Web ==2. These conditions are checked for browser based & electron based IDE. - migrateFromOdo018(); + void commands.executeCommand('setContext', 'isVSCode', env.uiKind); + Cluster.extensionContext = extensionContext; TokenStore.extensionContext = extensionContext; @@ -85,7 +71,7 @@ export async function activate(extensionContext: ExtensionContext): Promise extensionContext.subscriptions.push(value)); + const pushSubscriptions = async function(): Promise { + 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( [ @@ -201,11 +189,10 @@ export async function activate(extensionContext: ExtensionContext): Promise { + const updateContextAndProjectStatusBarItems = async function(context?: string): Promise { const kind = await getNamespaceKind(); - void Oc.Instance.getActiveProject().then((namespace) => + const namespace = await Oc.Instance.getActiveProject(); + if (namespace) { updateContextStatusBarItem(activeNamespaceStatusBarItem, 'project-node', namespace, `Current ${kind}: ${namespace}`, - !isNamespaceInfoStatusBarDisabled())); + !isNamespaceInfoStatusBarDisabled()); + } else { + updateContextStatusBarItem(activeNamespaceStatusBarItem, 'project-node', '', '', false); + } const kcu: KubeConfigUtils = new KubeConfigUtils(); - const currentContext: KcuContext = kcu.findContext(context); + const currentContext: KcuContext = kcu.findContext(context ? context : kcu.currentContext); 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(); + // Wait for finishing Kube Config setup + await setKubeConfigPromise; + return { - verifyBundledBinaries, + verifyBundledBinaries }; }