diff --git a/src/extension.ts b/src/extension.ts index 8c68b3b85..c798c96f0 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -251,6 +251,8 @@ export async function activate(context): Promise { registerTelemetry(context) ]; + telemetry.invalidateClusterType(undefined, kubectl); + await azureclusterprovider.init(clusterProviderRegistry, { shell: shell, fs: fs }); await minikubeclusterprovider.init(clusterProviderRegistry, { shell: shell, minikube: minikube }); // On save, refresh the Helm YAML preview. @@ -1707,8 +1709,8 @@ async function useContextKubernetes(explorerNode: explorer.KubernetesObject) { const targetContext = contextObj.contextName; const shellResult = await kubectl.invokeAsync(`config use-context ${targetContext}`); if (shellResult.code === 0) { - refreshExplorer(); telemetry.invalidateClusterType(targetContext); + refreshExplorer(); } else { vscode.window.showErrorMessage(`Failed to set '${targetContext}' as current cluster: ${shellResult.stderr}`); } diff --git a/src/telemetry-helper.ts b/src/telemetry-helper.ts index ecdcdda1c..e0da83451 100644 --- a/src/telemetry-helper.ts +++ b/src/telemetry-helper.ts @@ -9,30 +9,35 @@ export function telemetrise(command: string, kubectl: Kubectl, callback: (...arg } export enum ClusterType { + Unknown = 0, Azure, Minikube, Other } let latestContextName: string | null; -let cachedClusterType: ClusterType | null = null; +let cachedClusterType: ClusterType = ClusterType.Unknown; const knownClusters: any = {}; -export function invalidateClusterType(newContext: string): void { +export function invalidateClusterType(newContext: string, kubectl?: Kubectl): void { latestContextName = newContext || null; - cachedClusterType = null; + cachedClusterType = ClusterType.Unknown; + if (kubectl) { + setImmediate(() => { + try { + loadCachedClusterType(kubectl); + } catch { + // swallow it + } + }); + } } async function clusterType(kubectl: Kubectl): Promise { - if (latestContextName && knownClusters[latestContextName]) { - cachedClusterType = knownClusters[latestContextName]; - } - if (!cachedClusterType) { - cachedClusterType = await inferCurrentClusterType(kubectl, latestContextName); - if (latestContextName) { - knownClusters[latestContextName] = cachedClusterType; - } + if (cachedClusterType === ClusterType.Unknown) { + await loadCachedClusterType(kubectl); } + switch (cachedClusterType) { case ClusterType.Azure: return 'azure'; @@ -40,10 +45,24 @@ async function clusterType(kubectl: Kubectl): Promise { return 'minikube'; case ClusterType.Other: return 'other'; + default: + return 'indeterminate'; + } +} + +async function loadCachedClusterType(kubectl: Kubectl) { + if (latestContextName && knownClusters[latestContextName]) { + cachedClusterType = knownClusters[latestContextName]; + } + else { + cachedClusterType = await inferCurrentClusterType(kubectl, latestContextName); + if (latestContextName) { + knownClusters[latestContextName] = cachedClusterType; + } } } -async function inferCurrentClusterType(kubectl: Kubectl, contextNameHint: string | null): Promise { +async function inferCurrentClusterType(kubectl: Kubectl, contextNameHint: string | null): Promise { if (!latestContextName) { const ctxsr = await kubectl.invokeAsync('config current-context'); if (ctxsr.code === 0) { @@ -59,7 +78,7 @@ async function inferCurrentClusterType(kubectl: Kubectl, contextNameHint: string const cisr = await kubectl.invokeAsync('cluster-info'); if (cisr.code !== 0) { - return null; + return ClusterType.Unknown; } const masterInfos = cisr.stdout.split('\n') .filter((s) => s.indexOf('master is running at') >= 0); @@ -69,7 +88,7 @@ async function inferCurrentClusterType(kubectl: Kubectl, contextNameHint: string } const masterInfo = masterInfos[0]; - if (masterInfo.indexOf('azure.com') >= 0) { + if (masterInfo.indexOf('azmk8s.io') >= 0 || masterInfo.indexOf('azure.com') >= 0) { return ClusterType.Azure; }